-
Notifications
You must be signed in to change notification settings - Fork 2
/
authOperations.js
140 lines (132 loc) · 3.73 KB
/
authOperations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import axios from 'axios';
import { createAsyncThunk } from '@reduxjs/toolkit';
import {
signIn,
signOut,
getUser,
getRefreshToken,
updateBalance,
} from '../services';
import {
fetchIncomeCategories,
fetchExpenseCategories,
fetchIncome,
fetchExpense,
} from '../redux/transactionsOperations';
import { showErrorMessage } from '../components/Toasters';
const token = {
set(token) {
axios.defaults.headers.common.Authorization = `Bearer ${token}`;
},
unset() {
axios.defaults.headers.common.Authorization = '';
},
};
export const logIn = createAsyncThunk(
'auth/logIn',
async function (credentials, { rejectWithValue, dispatch }) {
try {
const { data } = await signIn(credentials);
token.set(data.accessToken);
sessionStorage.setItem('token', data.accessToken);
sessionStorage.setItem('refreshToken', data.refreshToken);
sessionStorage.setItem('sid', data.sid);
dispatch(fetchIncomeCategories());
dispatch(fetchExpenseCategories());
return data;
} catch (error) {
showErrorMessage(error.message);
return rejectWithValue(error.message);
}
},
);
export const logInGoogle = createAsyncThunk(
'auth/logIn',
async function (response, { rejectWithValue, dispatch }) {
try {
token.set(response.accessToken);
sessionStorage.setItem('token', response.accessToken);
sessionStorage.setItem('refreshToken', response.refreshToken);
sessionStorage.setItem('sid', response.sid);
await dispatch(getCurrentUser());
return response;
} catch (error) {
showErrorMessage(error.message);
return rejectWithValue(error.message);
}
},
);
export const logOut = createAsyncThunk(
'auth/logOut',
async function (_, { rejectWithValue, dispatch }) {
try {
sessionStorage.removeItem('token');
sessionStorage.removeItem('sid');
sessionStorage.removeItem('refreshToken');
await signOut();
token.unset();
} catch (error) {
showErrorMessage(error.message);
return rejectWithValue(error.message);
}
},
);
export const getCurrentUser = createAsyncThunk(
'auth/getCurrentUser',
async function (_, { rejectWithValue, dispatch }) {
try {
const accessToken = sessionStorage.getItem('token');
token.set(accessToken);
const response = await getUser();
if (response.data) {
dispatch(fetchIncome());
dispatch(fetchExpense());
}
return response.data;
} catch (error) {
showErrorMessage(error.message);
return rejectWithValue(error.message);
}
},
);
export const updateUserBalance = createAsyncThunk(
'auth/updateUserBalance',
async function (balance, { rejectWithValue, dispatch }) {
try {
const response = await updateBalance(balance);
return response.data;
} catch (error) {
showErrorMessage(error.message);
return rejectWithValue(error.message);
}
},
);
export const refresh = createAsyncThunk(
'auth/refresh',
async function (_, { rejectWithValue, dispatch, getState }) {
const sid = sessionStorage.getItem('sid');
const refreshToken = sessionStorage.getItem('refreshToken');
try {
const response = await getRefreshToken({ refreshToken, sid });
token.set(response.data.newAccessToken);
dispatch(getCurrentUser());
return response.data;
} catch (error) {
sessionStorage.removeItem('token');
sessionStorage.removeItem('sid');
sessionStorage.removeItem('refreshToken');
token.unset();
showErrorMessage(error.message);
return rejectWithValue(error.message);
}
},
);
const authOperations = {
logIn,
logInGoogle,
logOut,
getCurrentUser,
updateUserBalance,
refresh,
};
export default authOperations;