forked from vikunja/frontend
Add support for identity-aware proxy login
This commit is contained in:
parent
0127ccf395
commit
8b68f453c5
|
@ -98,6 +98,10 @@ export default {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!localStorage.getItem('token')) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const expiresIn = (this.userInfo !== null ? this.userInfo.exp : 0) - +new Date() / 1000
|
const expiresIn = (this.userInfo !== null ? this.userInfo.exp : 0) - +new Date() / 1000
|
||||||
|
|
||||||
// If the token expiry is negative, it is already expired and we have no choice but to redirect
|
// If the token expiry is negative, it is already expired and we have no choice but to redirect
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import {HTTPFactory} from '@/http-common'
|
import {HTTPFactory} from '@/http-common'
|
||||||
import {ERROR_MESSAGE, LOADING} from '../mutation-types'
|
import {ERROR_MESSAGE, LOADING} from '../mutation-types'
|
||||||
import UserModel from '../../models/user'
|
import UserModel from '../../models/user'
|
||||||
|
import authTypes from '../../models/authTypes'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
namespaced: true,
|
namespaced: true,
|
||||||
|
@ -173,47 +174,49 @@ export default {
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const jwt = localStorage.getItem('token')
|
const jwt = localStorage.getItem('token')
|
||||||
let authenticated = false
|
let authenticated = false
|
||||||
|
let info = null
|
||||||
if (jwt) {
|
if (jwt) {
|
||||||
const base64 = jwt
|
const base64 = jwt
|
||||||
.split('.')[1]
|
.split('.')[1]
|
||||||
.replace('-', '+')
|
.replace('-', '+')
|
||||||
.replace('_', '/')
|
.replace('_', '/')
|
||||||
const info = new UserModel(JSON.parse(window.atob(base64)))
|
info = new UserModel(JSON.parse(window.atob(base64)))
|
||||||
const ts = Math.round((new Date()).getTime() / 1000)
|
const ts = Math.round((new Date()).getTime() / 1000)
|
||||||
authenticated = info.exp >= ts
|
authenticated = info.exp >= ts
|
||||||
ctx.commit('info', info)
|
|
||||||
|
|
||||||
if (authenticated) {
|
|
||||||
const HTTP = HTTPFactory()
|
|
||||||
// We're not returning the promise here to prevent blocking the initial ui render if the user is
|
|
||||||
// accessing the site with a token in local storage
|
|
||||||
HTTP.get('user', {
|
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${jwt}`,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.then(r => {
|
|
||||||
const info = new UserModel(r.data)
|
|
||||||
info.type = ctx.state.info.type
|
|
||||||
info.email = ctx.state.info.email
|
|
||||||
info.exp = ctx.state.info.exp
|
|
||||||
|
|
||||||
ctx.commit('info', info)
|
|
||||||
ctx.commit('authenticated', authenticated)
|
|
||||||
ctx.commit('lastUserRefresh')
|
|
||||||
})
|
|
||||||
.catch(e => {
|
|
||||||
console.error('Error while refreshing user info:', e)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.commit('authenticated', authenticated)
|
|
||||||
if (!authenticated) {
|
if (!authenticated) {
|
||||||
ctx.commit('info', null)
|
info = null;
|
||||||
}
|
}
|
||||||
|
ctx.commit('authenticated', authenticated)
|
||||||
|
ctx.commit('info', info)
|
||||||
|
|
||||||
|
// Always call the user endpoint, as there may be external auth
|
||||||
|
// even if a jwt is not present
|
||||||
|
const HTTP = HTTPFactory()
|
||||||
|
HTTP.get('user', {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${jwt}`,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.then(r => {
|
||||||
|
const info = new UserModel(r.data)
|
||||||
|
// Externally authed users might not have any of these set from a JWT,
|
||||||
|
// so default to sane values
|
||||||
|
info.type = ctx.state.info ? ctx.state.info.type : authTypes.USER
|
||||||
|
info.email = ctx.state.info ? ctx.state.info.email : null
|
||||||
|
info.exp = ctx.state.info ? ctx.state.info.exp : 0
|
||||||
|
|
||||||
|
ctx.commit('info', info)
|
||||||
|
ctx.commit('authenticated', true)
|
||||||
|
ctx.commit('lastUserRefresh')
|
||||||
|
})
|
||||||
|
.catch(e => {
|
||||||
|
console.error('Error while refreshing user info:', e)
|
||||||
|
})
|
||||||
|
|
||||||
return Promise.resolve()
|
return Promise.resolve()
|
||||||
},
|
},
|
||||||
|
@ -224,6 +227,10 @@ export default {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!localStorage.getItem('token')) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
HTTP.post('user/token', null, {
|
HTTP.post('user/token', null, {
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: 'Bearer ' + localStorage.getItem('token'),
|
Authorization: 'Bearer ' + localStorage.getItem('token'),
|
||||||
|
|
Loading…
Reference in New Issue