2026-04-14 11:17:31 +08:00
|
|
|
import { createSlice, PayloadAction } from "@reduxjs/toolkit"
|
|
|
|
|
import { User } from "@/types"
|
|
|
|
|
|
2026-06-23 14:02:37 +08:00
|
|
|
export interface AuthUser extends User {
|
|
|
|
|
expireAt: number; // 转换 expiration 得到的时间戳
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2026-04-14 11:17:31 +08:00
|
|
|
interface AuthState {
|
|
|
|
|
user: User | null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const initialState: AuthState = {
|
|
|
|
|
user: null,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const authSlice = createSlice({
|
|
|
|
|
name: "auth",
|
|
|
|
|
initialState,
|
|
|
|
|
reducers: {
|
|
|
|
|
setCurrentUser(state, action: PayloadAction<User>) {
|
|
|
|
|
state.user = action.payload
|
2026-06-23 14:02:37 +08:00
|
|
|
localStorage.setItem('auth_user', JSON.stringify(action.payload));
|
2026-04-14 11:17:31 +08:00
|
|
|
},
|
|
|
|
|
clearCurrentUser(state) {
|
|
|
|
|
state.user = null
|
2026-06-23 14:02:37 +08:00
|
|
|
localStorage.removeItem('auth_user');
|
2026-04-14 11:17:31 +08:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
export const { setCurrentUser, clearCurrentUser } = authSlice.actions
|
|
|
|
|
export const authReducer = authSlice.reducer
|