1
0
Fork 0
mirror of https://gitlab.com/bramw/baserow.git synced 2025-04-04 21:25:24 +00:00

redirect to login or app depending on if logged in

This commit is contained in:
Bram Wiepjes 2019-06-28 14:42:42 +02:00
parent 726484cf2e
commit 64b515d849
6 changed files with 41 additions and 17 deletions
backend/src/baserow
api/v0
config/settings
web-frontend

View file

@ -0,0 +1,8 @@
from .serializers.user import UserSerializer
def jwt_response_payload_handler(token, user=None, request=None):
return {
'token': token,
'user': UserSerializer(user, context={'request': request}).data
}

View file

@ -129,7 +129,8 @@ CORS_ORIGIN_WHITELIST = (
)
JWT_AUTH = {
'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=30),
'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=300),
'JWT_ALLOW_REFRESH': True,
'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=7)
'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=7),
'JWT_RESPONSE_PAYLOAD_HANDLER': 'baserow.api.v0.jwt.jwt_response_payload_handler'
}

View file

@ -0,0 +1,18 @@
<template>
<div>
<h1>Welcome {{ user }}</h1>
</div>
</template>
<script>
import { mapState } from 'vuex'
export default {
middleware: 'authenticated',
computed: {
...mapState({
user: state => state.auth.user
})
}
}
</script>

View file

@ -1,16 +1,12 @@
<template>
<div>
<h1>Baserow</h1>
<p>authenticated: {{ isAuthenticated }}</p>
<nuxt-link :to="{ name: 'login' }">Login</nuxt-link>
</div>
<div></div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
middleware: 'authenticated',
computed: { ...mapGetters({ isAuthenticated: 'auth/isAuthenticated' }) }
mounted() {
const name = this.$store.getters['auth/isAuthenticated'] ? 'app' : 'login'
this.$nuxt.$router.replace({ name: name })
}
}
</script>

View file

@ -107,7 +107,7 @@ export default {
password: this.credentials.password
})
.then(() => {
console.log('@TODO navigate to main page')
this.$nuxt.$router.replace({ name: 'app' })
})
.catch(() => {
this.invalid = true

View file

@ -10,9 +10,10 @@ export const state = () => ({
})
export const mutations = {
SET_USER_DATA(state, token) {
SET_USER_DATA(state, { token, user }) {
state.token = token
state.user = jwtDecode(token)
state.token_data = jwtDecode(token)
state.user = user
},
CLEAR_USER_DATA(state) {
state.token = null
@ -31,7 +32,7 @@ export const actions = {
login({ commit, dispatch }, { email, password }) {
return AuthService.login(email, password).then(({ data }) => {
setToken(data.token, this.app.$cookies)
commit('SET_USER_DATA', data.token)
commit('SET_USER_DATA', data)
dispatch('startRefreshTimeout')
})
},
@ -44,7 +45,7 @@ export const actions = {
return AuthService.refresh(token)
.then(({ data }) => {
setToken(data.token, this.app.$cookies)
commit('SET_USER_DATA', data.token)
commit('SET_USER_DATA', data)
dispatch('startRefreshTimeout')
})
.catch(() => {
@ -93,6 +94,6 @@ export const getters = {
*/
tokenExpireSeconds(state) {
const now = Math.ceil(new Date().getTime() / 1000)
return state.user.exp - now
return state.token_data.exp - now
}
}