2025-05-06 06:45:49 +00:00
|
|
|
|
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
|
|
|
|
|
|
}
|
2025-06-24 13:21:19 +00:00
|
|
|
|
tenant?: {
|
|
|
|
|
|
tenantId?: string
|
|
|
|
|
|
tenantName?: string
|
|
|
|
|
|
}
|
2025-05-06 06:45:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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']>
|
|
|
|
|
|
}
|
2025-06-24 13:21:19 +00:00
|
|
|
|
tenant: {
|
|
|
|
|
|
setTenant: Action<NonNullable<AuthStoreModel['tenant']>, AuthStoreModel['tenant']>
|
|
|
|
|
|
}
|
2025-05-06 06:45:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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: '',
|
|
|
|
|
|
},
|
2025-06-24 13:21:19 +00:00
|
|
|
|
tenant: {
|
|
|
|
|
|
tenantId: '',
|
|
|
|
|
|
tenantName: '',
|
|
|
|
|
|
},
|
2025-05-06 06:45:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2025-06-24 13:21:19 +00:00
|
|
|
|
state.user.avatar = AVATAR_URL(payload.user.id, state.tenant?.tenantId) + `?${dayjs().unix()}`
|
2025-05-06 06:45:49 +00:00
|
|
|
|
}),
|
2025-06-24 13:21:19 +00:00
|
|
|
|
signOut: action(() => ({ ...initialState })),
|
|
|
|
|
|
// signOut: action((state) => ({ ...initialState, tenantId: state.tenant?.tenantId })),
|
2025-05-06 06:45:49 +00:00
|
|
|
|
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
|
|
|
|
|
|
}),
|
|
|
|
|
|
},
|
2025-06-24 13:21:19 +00:00
|
|
|
|
tenant: {
|
|
|
|
|
|
...initialState.tenant,
|
|
|
|
|
|
setTenant: action((state, payload) => {
|
|
|
|
|
|
state.tenantId = payload?.tenantId
|
|
|
|
|
|
state.tenantName = payload?.tenantName
|
|
|
|
|
|
}),
|
|
|
|
|
|
},
|
2025-05-06 06:45:49 +00:00
|
|
|
|
}
|