Skip to content

Commit

Permalink
fix auth
Browse files Browse the repository at this point in the history
  • Loading branch information
ElPastel committed Nov 26, 2023
1 parent 6c0b2ca commit ae82184
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/components/EntryForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Button, Form, Input } from 'antd';
import { ValidateErrorEntity } from 'rc-field-form/es/interface';
import { Link } from 'react-router-dom';
import { Link, useNavigate } from 'react-router-dom';
import { Routes } from '../constants/routes';
import { Login } from '../store/auth/types';
import { useAppDispatch } from '../store';
Expand All @@ -13,9 +13,9 @@ type FieldType = {

function EntryForm() {
const dispatch = useAppDispatch();
const navigate = useNavigate();
const onFinish = (values: Login) => {
console.log(values);
dispatch(signInUser(values));
dispatch(signInUser(values)).then(() => navigate(Routes.PROFILE));
};

const onFinishFailed = (errorInfo: ValidateErrorEntity<Login>) => {
Expand Down
23 changes: 18 additions & 5 deletions src/store/auth/authActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ export const signInUser = createAsyncThunk(
`${BASE_URL}token/create/`,
body,
);
console.log(response);
const auth_token = response.data.token;
const id = response.data.user_id;
dispatch(setUserId(id));
setToken(auth_token);
return dispatch(getUserById(id));
return dispatch(getUser());
} catch (e) {
return rejectWithValue(e);
}
Expand Down Expand Up @@ -62,14 +61,28 @@ export const logOut = createAsyncThunk(
},
);

export const getUserById = createAsyncThunk(
export const getUser = createAsyncThunk(
'auth/getUser',
async (_, { rejectWithValue }) => {
try {
const token = getToken();
const res = await axios.get<void, User>(`${BASE_URL}users/me`, {
headers: { Authorization: `Token ${token?.slice(1, -1)}` },
});
return res;
} catch (e) {
return rejectWithValue(e);
}
},
);

export const getUserById = createAsyncThunk(
'auth/getUserById',
async (id: string, { rejectWithValue }) => {
try {
const token = getToken();
console.log(token);
const res = await axios.get<void, User>(`${BASE_URL}users/${id}/`, {
headers: { Authorization: `Token ${token}` },
headers: { Authorization: `Token ${token?.slice(1, -1)}` },
});
return res;
} catch (e) {
Expand Down
2 changes: 2 additions & 0 deletions src/store/auth/authSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RootState } from '..';
import { AuthState } from './types';
import {
buildConfirmEmail,
buildGetUser,
buildGetUserById,
buildLogOut,
buildRegistration,
Expand Down Expand Up @@ -62,6 +63,7 @@ const authSlice = createSlice({
buildLogOut(builder);
buildGetUserById(builder);
buildConfirmEmail(builder);
buildGetUser(builder);
},
});

Expand Down
19 changes: 19 additions & 0 deletions src/store/auth/extraReducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
registration,
signInUser,
confirmEmail,
getUser,
} from './authActions';
import { AuthState } from './types';

Expand Down Expand Up @@ -90,3 +91,21 @@ export const buildGetUserById = (builder: ActionReducerMapBuilder<AuthState>) =>
state.user = null;
state.userError = 'Sorry, something went wrong';
});

export const buildGetUser = (builder: ActionReducerMapBuilder<AuthState>) =>
builder
.addCase(getUser.pending, (state) => {
state.userStatus = 'pending';
})
.addCase(getUser.fulfilled, (state, action) => {
state.userStatus = 'success';
state.user = action.payload;
state.isAuth = true;
state.userError = null;
})
.addCase(getUser.rejected, (state) => {
state.userStatus = 'error';
state.isAuth = false;
state.user = null;
state.userError = 'Sorry, something went wrong';
});

0 comments on commit ae82184

Please sign in to comment.