Migrate auth flow module to pinia store
This commit is contained in:
parent
60e5c3b042
commit
4ee26c6041
8 changed files with 91 additions and 110 deletions
69
src/stores/auth_flow.js
Normal file
69
src/stores/auth_flow.js
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
import { useOAuthStore } from 'src/stores/oauth.js'
|
||||
import { defineStore } from 'pinia'
|
||||
|
||||
const PASSWORD_STRATEGY = 'password'
|
||||
const TOKEN_STRATEGY = 'token'
|
||||
|
||||
// MFA strategies
|
||||
const TOTP_STRATEGY = 'totp'
|
||||
const RECOVERY_STRATEGY = 'recovery'
|
||||
|
||||
export const useAuthFlowStore = defineStore('authFlow', {
|
||||
// initial state
|
||||
state: () => ({
|
||||
settings: {},
|
||||
strategy: PASSWORD_STRATEGY,
|
||||
initStrategy: PASSWORD_STRATEGY // default strategy from config
|
||||
}),
|
||||
// getters
|
||||
getters: {
|
||||
requiredPassword: (state) => {
|
||||
return state.strategy === PASSWORD_STRATEGY
|
||||
},
|
||||
requiredToken: (state) => {
|
||||
return state.strategy === TOKEN_STRATEGY
|
||||
},
|
||||
requiredTOTP: (state) => {
|
||||
return state.strategy === TOTP_STRATEGY
|
||||
},
|
||||
requiredRecovery: (state) => {
|
||||
return state.strategy === RECOVERY_STRATEGY
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
setInitialStrategy (strategy) {
|
||||
if (strategy) {
|
||||
this.initStrategy = strategy
|
||||
this.strategy = strategy
|
||||
}
|
||||
},
|
||||
requirePassword () {
|
||||
this.strategy = PASSWORD_STRATEGY
|
||||
},
|
||||
requireToken () {
|
||||
this.strategy = TOKEN_STRATEGY
|
||||
},
|
||||
requireMFA ({ settings }) {
|
||||
this.settings = settings
|
||||
this.strategy = TOTP_STRATEGY // default strategy of MFA
|
||||
},
|
||||
requireRecovery () {
|
||||
this.strategy = RECOVERY_STRATEGY
|
||||
},
|
||||
requireTOTP () {
|
||||
this.strategy = TOTP_STRATEGY
|
||||
},
|
||||
abortMFA () {
|
||||
this.resetState()
|
||||
},
|
||||
resetState () {
|
||||
this.strategy = this.initStrategy
|
||||
this.settings = {}
|
||||
},
|
||||
async login ({ access_token: accessToken }) {
|
||||
useOAuthStore().setToken(accessToken)
|
||||
await window.vuex.dispatch('loginUser', accessToken, { root: true })
|
||||
this.resetState()
|
||||
}
|
||||
}
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue