Skip to content

Commit

Permalink
Better login process
Browse files Browse the repository at this point in the history
  • Loading branch information
tonioo committed Apr 15, 2024
1 parent 452ee13 commit 6217da3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion frontend/src/api/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const _axios = axios.create()
_axios.interceptors.request.use(
function (config) {
const authStore = useAuthStore()
// Do something before request is sent
if (authStore.isAuthenticated) {
config.headers['Accept-Language'] = authStore.authUser.language
config.headers['Authorization'] = `Bearer ${authStore.getAccessToken()}`
}
return config
},
Expand Down
15 changes: 13 additions & 2 deletions frontend/src/stores/auth.store.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ export const useAuthStore = defineStore('auth', () => {
})
}

function getAccessToken() {
return manager.getUser().then((user) => {
if (!user) {
return null
}
return user.access_token
})
}

async function initialize() {
if (isAuthenticated.value) {
return null
Expand All @@ -46,16 +55,17 @@ export const useAuthStore = defineStore('auth', () => {
if (!user) {
return null
}
repository.defaults.headers.common.Authorization = `Bearer ${user.access_token}`
repository.defaults.headers.post['Content-Type'] = 'application/json'
return fetchUser()
}

async function validateAccess() {
const user = await manager.getUser()
if (!user || user.expired) {
manager.signinRedirect()
return
}
repository.defaults.headers.common.Authorization = `Bearer ${user.access_token}`
repository.defaults.headers.post['Content-Type'] = 'application/json'
}

async function login() {
Expand Down Expand Up @@ -127,6 +137,7 @@ export const useAuthStore = defineStore('auth', () => {
userHasMailbox,
validateAccess,
fetchUser,
getAccessToken,
initialize,
login,
$reset,
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/views/login/LoginCallbackView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { onMounted } from 'vue'
import { useAuthStore } from '@/stores'
const authStore = useAuthStore()
onMounted(() => authStore.completeLogin())
onMounted(() => {
authStore.completeLogin()
})
</script>

<template>
Expand Down

0 comments on commit 6217da3

Please sign in to comment.