pleroma-fe/src/stores/auth_flow.js

69 lines
1.7 KiB
JavaScript
Raw Normal View History

import { defineStore } from 'pinia'
2026-01-06 16:23:17 +02:00
import { useOAuthStore } from 'src/stores/oauth.js'
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,
2026-01-06 16:22:52 +02:00
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: {
2026-01-06 16:22:52 +02:00
setInitialStrategy(strategy) {
if (strategy) {
this.initStrategy = strategy
this.strategy = strategy
}
},
2026-01-06 16:22:52 +02:00
requirePassword() {
this.strategy = PASSWORD_STRATEGY
},
2026-01-06 16:22:52 +02:00
requireToken() {
this.strategy = TOKEN_STRATEGY
},
2026-01-06 16:22:52 +02:00
requireMFA({ settings }) {
this.settings = settings
this.strategy = TOTP_STRATEGY // default strategy of MFA
},
2026-01-06 16:22:52 +02:00
requireRecovery() {
this.strategy = RECOVERY_STRATEGY
},
2026-01-06 16:22:52 +02:00
requireTOTP() {
this.strategy = TOTP_STRATEGY
},
2026-01-06 16:22:52 +02:00
abortMFA() {
this.resetState()
},
2026-01-06 16:22:52 +02:00
resetState() {
this.strategy = this.initStrategy
this.settings = {}
},
2026-01-06 16:22:52 +02:00
async login({ access_token: accessToken }) {
useOAuthStore().setToken(accessToken)
await window.vuex.dispatch('loginUser', accessToken, { root: true })
this.resetState()
2026-01-06 16:22:52 +02:00
},
},
})