102 lines
2.9 KiB
TypeScript
102 lines
2.9 KiB
TypeScript
|
|
import { AVATAR_URL } from '@/constants/app.constant'
|
|||
|
|
import dayjs from 'dayjs'
|
|||
|
|
import type { Action, ThunkOn } from 'easy-peasy'
|
|||
|
|
import { action, thunkOn } from 'easy-peasy'
|
|||
|
|
import { Injections, StoreModel } from './store'
|
|||
|
|
|
|||
|
|
export interface AuthStoreModel {
|
|||
|
|
isRefreshing: boolean
|
|||
|
|
session: {
|
|||
|
|
token: string
|
|||
|
|
refreshToken: string
|
|||
|
|
expiresIn: number
|
|||
|
|
expire: number
|
|||
|
|
signedIn: boolean
|
|||
|
|
}
|
|||
|
|
user: {
|
|||
|
|
id: string
|
|||
|
|
userName: string
|
|||
|
|
email: string
|
|||
|
|
authority: string[]
|
|||
|
|
name: string
|
|||
|
|
avatar?: string
|
|||
|
|
}
|
|||
|
|
tenantId?: string
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export interface AuthStoreActions {
|
|||
|
|
/**
|
|||
|
|
* Birden fazla /connect/refresh isteği atılırsa, id server 500 hatası veriyor
|
|||
|
|
* Bunu engellemek için kullanılan kilit
|
|||
|
|
*/
|
|||
|
|
setIsRefreshing: Action<AuthStoreModel, boolean>
|
|||
|
|
signIn: Action<AuthStoreModel, Omit<AuthStoreModel, 'isRefreshing'>>
|
|||
|
|
signOut: Action<AuthStoreModel>
|
|||
|
|
onSignInAndOut: ThunkOn<AuthStoreModel, Injections, StoreModel>
|
|||
|
|
user: {
|
|||
|
|
setUser: Action<AuthStoreModel['user'], AuthStoreModel['user']>
|
|||
|
|
}
|
|||
|
|
setTenantId: Action<AuthStoreModel, string | undefined>
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export type AuthModel = AuthStoreModel & AuthStoreActions
|
|||
|
|
|
|||
|
|
export const initialState: AuthStoreModel = {
|
|||
|
|
isRefreshing: false,
|
|||
|
|
session: { token: '', refreshToken: '', expiresIn: 0, expire: 0, signedIn: false },
|
|||
|
|
user: {
|
|||
|
|
id: '',
|
|||
|
|
userName: '',
|
|||
|
|
email: '',
|
|||
|
|
authority: [],
|
|||
|
|
name: '',
|
|||
|
|
avatar: '',
|
|||
|
|
},
|
|||
|
|
tenantId: undefined,
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export const authModel: AuthModel = {
|
|||
|
|
setIsRefreshing: action((state, payload) => {
|
|||
|
|
state.isRefreshing = payload
|
|||
|
|
}),
|
|||
|
|
signIn: action((state, payload) => {
|
|||
|
|
state.session.signedIn = true
|
|||
|
|
state.session.token = payload.session.token
|
|||
|
|
state.session.refreshToken = payload.session.refreshToken
|
|||
|
|
state.session.expiresIn = payload.session.expiresIn
|
|||
|
|
state.session.expire = payload.session.expire
|
|||
|
|
state.user.id = payload.user.id
|
|||
|
|
state.user.name = payload.user.name
|
|||
|
|
state.user.userName = payload.user.userName
|
|||
|
|
state.user.authority = payload.user.authority
|
|||
|
|
state.user.email = payload.user.email
|
|||
|
|
state.user.avatar = AVATAR_URL(payload.user.id, state.tenantId)+ `?${dayjs().unix()}`
|
|||
|
|
}),
|
|||
|
|
signOut: action((state) => ({ ...initialState, tenantId: state.tenantId })),
|
|||
|
|
onSignInAndOut: thunkOn(
|
|||
|
|
(actions, storeActions) => [storeActions.auth.signIn, storeActions.auth.signOut],
|
|||
|
|
async (actions, payload, { getStoreActions }) => {
|
|||
|
|
getStoreActions().abpConfig.getConfig(false)
|
|||
|
|
},
|
|||
|
|
),
|
|||
|
|
isRefreshing: initialState.isRefreshing,
|
|||
|
|
session: {
|
|||
|
|
...initialState.session,
|
|||
|
|
},
|
|||
|
|
user: {
|
|||
|
|
...initialState.user,
|
|||
|
|
setUser: action((state, payload) => {
|
|||
|
|
state.id = payload.id
|
|||
|
|
state.name = payload.name
|
|||
|
|
state.userName = payload.userName
|
|||
|
|
state.authority = payload.authority
|
|||
|
|
state.email = payload.email
|
|||
|
|
state.avatar = payload.avatar
|
|||
|
|
}),
|
|||
|
|
},
|
|||
|
|
tenantId: initialState.tenantId,
|
|||
|
|
setTenantId: action((state, payload) => {
|
|||
|
|
state.tenantId = payload
|
|||
|
|
}),
|
|||
|
|
}
|