41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import { type PayloadAction, createSlice, isAnyOf } from "@reduxjs/toolkit";
|
||
import { auth, authInitialState } from "../../constants/auth";
|
||
import { authLogin, authRe, authRegister } from "./aysnc-thunks";
|
||
import { AuthStage, AuthStatus } from "../../interfaces/auth";
|
||
|
||
export const authSlice = createSlice({
|
||
name: auth,
|
||
initialState: authInitialState,
|
||
reducers: {
|
||
changeAuthStage: (draft, { payload }: PayloadAction<AuthStage>) => {
|
||
draft.stage = payload;
|
||
},
|
||
changeAuthStatus: (draft, { payload }: PayloadAction<AuthStatus>) => {
|
||
draft.authStatus = payload;
|
||
},
|
||
},
|
||
|
||
extraReducers: ({ addCase, addMatcher }) => {
|
||
addCase(authLogin.rejected, (draft) => {
|
||
draft.authStatus = AuthStatus.NotAuthorized;
|
||
draft.error = "Неверный логин или пароль";
|
||
});
|
||
|
||
addCase(authRegister.rejected, (draft) => {
|
||
draft.authStatus = AuthStatus.NotAuthorized;
|
||
draft.error = "Не удалось зарегистрироваться";
|
||
});
|
||
|
||
addCase(authRe.rejected, (draft) => {
|
||
draft.authStatus = AuthStatus.NotAuthorized;
|
||
});
|
||
|
||
addMatcher(
|
||
isAnyOf(authLogin.fulfilled, authRe.fulfilled, authRegister.fulfilled),
|
||
(draft) => {
|
||
draft.authStatus = AuthStatus.Authorized;
|
||
},
|
||
);
|
||
},
|
||
});
|