candidat

Форк
0
/
mag 
80 строк · 2.5 Кб
1
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
2
import Tokens from './api/tokensApi';
3
import AuthApi from './api/userApi';
4
import type { User, UserWithoutIdwithPassword, UserWithoutName } from './types/userTypes';
5

6
type StateAuth = {
7
  user: User | undefined;
8
  accessToken: string | undefined;
9
  error: string | undefined;
10
  loading: boolean;
11
};
12

13
const initialState: StateAuth = {
14
  user: undefined,
15
  accessToken: undefined,
16
  error: undefined,
17
  loading: true,
18
};
19

20
export const registrationThunk = createAsyncThunk(
21
  'registration/user',
22
  (body: UserWithoutIdwithPassword) => AuthApi.registartion(body),
23
);
24
export const refreshTokens = createAsyncThunk('refreshTokens/user', () => Tokens.refreshTokens());
25
export const logoutThunk = createAsyncThunk('logout/user', () => AuthApi.logout());
26
export const authorizationThunk = createAsyncThunk('authorization/user', (body: UserWithoutName) =>
27
  AuthApi.authrozation(body),
28
);
29

30
const authSlice = createSlice({
31
  name: 'auth',
32
  initialState,
33
  reducers: {},
34
  extraReducers: (builder) => {
35
    builder
36
      .addCase(registrationThunk.fulfilled, (state, action) => {
37
        state.user = action.payload.user;
38
        state.accessToken = action.payload.accessToken;
39
        state.loading = false;
40
        state.error = undefined;
41
      })
42
      .addCase(registrationThunk.pending, (state) => {
43
        state.loading = true;
44
      })
45
      .addCase(registrationThunk.rejected, (state, action) => {
46
        state.error = action.error.message;
47
        state.loading = false;
48
      })
49
      .addCase(refreshTokens.fulfilled, (state, action) => {
50
        state.user = action.payload.user;
51
        state.accessToken = action.payload.accessToken;
52
      })
53
      .addCase(refreshTokens.pending, (state) => {
54
        state.loading = true;
55
      })
56
      .addCase(refreshTokens.rejected, (state, action) => {
57
        state.error = action.error.message;
58
        state.loading = false;
59
      })
60
      .addCase(authorizationThunk.fulfilled, (state, action) => {
61
        state.user = action.payload.user;
62
        state.accessToken = action.payload.accessToken;
63
        state.loading = false;
64
        state.error = undefined;
65
      })
66
      .addCase(authorizationThunk.pending, (state) => {
67
        state.loading = true;
68
      })
69
      .addCase(authorizationThunk.rejected, (state, action) => {
70
        state.error = action.error.message;
71
        state.loading = false;
72
      })
73
      .addCase(logoutThunk.fulfilled, (state) => {
74
        state.user = undefined;
75
        state.accessToken = undefined;
76
      });
77
  },
78
});
79

80
export default authSlice;
81

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.