mirror of
https://gitlab.com/bramw/baserow.git
synced 2025-04-14 17:18:33 +00:00
redirect to login or app depending on if logged in
This commit is contained in:
parent
726484cf2e
commit
64b515d849
6 changed files with 41 additions and 17 deletions
8
backend/src/baserow/api/v0/jwt.py
Normal file
8
backend/src/baserow/api/v0/jwt.py
Normal 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
|
||||||
|
}
|
|
@ -129,7 +129,8 @@ CORS_ORIGIN_WHITELIST = (
|
||||||
)
|
)
|
||||||
|
|
||||||
JWT_AUTH = {
|
JWT_AUTH = {
|
||||||
'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=30),
|
'JWT_EXPIRATION_DELTA': datetime.timedelta(seconds=300),
|
||||||
'JWT_ALLOW_REFRESH': True,
|
'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'
|
||||||
}
|
}
|
||||||
|
|
18
web-frontend/pages/app/index.vue
Normal file
18
web-frontend/pages/app/index.vue
Normal 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>
|
|
@ -1,16 +1,12 @@
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<div></div>
|
||||||
<h1>Baserow</h1>
|
|
||||||
<p>authenticated: {{ isAuthenticated }}</p>
|
|
||||||
<nuxt-link :to="{ name: 'login' }">Login</nuxt-link>
|
|
||||||
</div>
|
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { mapGetters } from 'vuex'
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
middleware: 'authenticated',
|
mounted() {
|
||||||
computed: { ...mapGetters({ isAuthenticated: 'auth/isAuthenticated' }) }
|
const name = this.$store.getters['auth/isAuthenticated'] ? 'app' : 'login'
|
||||||
|
this.$nuxt.$router.replace({ name: name })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -107,7 +107,7 @@ export default {
|
||||||
password: this.credentials.password
|
password: this.credentials.password
|
||||||
})
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
console.log('@TODO navigate to main page')
|
this.$nuxt.$router.replace({ name: 'app' })
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
this.invalid = true
|
this.invalid = true
|
||||||
|
|
|
@ -10,9 +10,10 @@ export const state = () => ({
|
||||||
})
|
})
|
||||||
|
|
||||||
export const mutations = {
|
export const mutations = {
|
||||||
SET_USER_DATA(state, token) {
|
SET_USER_DATA(state, { token, user }) {
|
||||||
state.token = token
|
state.token = token
|
||||||
state.user = jwtDecode(token)
|
state.token_data = jwtDecode(token)
|
||||||
|
state.user = user
|
||||||
},
|
},
|
||||||
CLEAR_USER_DATA(state) {
|
CLEAR_USER_DATA(state) {
|
||||||
state.token = null
|
state.token = null
|
||||||
|
@ -31,7 +32,7 @@ export const actions = {
|
||||||
login({ commit, dispatch }, { email, password }) {
|
login({ commit, dispatch }, { email, password }) {
|
||||||
return AuthService.login(email, password).then(({ data }) => {
|
return AuthService.login(email, password).then(({ data }) => {
|
||||||
setToken(data.token, this.app.$cookies)
|
setToken(data.token, this.app.$cookies)
|
||||||
commit('SET_USER_DATA', data.token)
|
commit('SET_USER_DATA', data)
|
||||||
dispatch('startRefreshTimeout')
|
dispatch('startRefreshTimeout')
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
@ -44,7 +45,7 @@ export const actions = {
|
||||||
return AuthService.refresh(token)
|
return AuthService.refresh(token)
|
||||||
.then(({ data }) => {
|
.then(({ data }) => {
|
||||||
setToken(data.token, this.app.$cookies)
|
setToken(data.token, this.app.$cookies)
|
||||||
commit('SET_USER_DATA', data.token)
|
commit('SET_USER_DATA', data)
|
||||||
dispatch('startRefreshTimeout')
|
dispatch('startRefreshTimeout')
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
|
@ -93,6 +94,6 @@ export const getters = {
|
||||||
*/
|
*/
|
||||||
tokenExpireSeconds(state) {
|
tokenExpireSeconds(state) {
|
||||||
const now = Math.ceil(new Date().getTime() / 1000)
|
const now = Math.ceil(new Date().getTime() / 1000)
|
||||||
return state.user.exp - now
|
return state.token_data.exp - now
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue