Skip to content

Commit

Permalink
auth/operations
Browse files Browse the repository at this point in the history
  • Loading branch information
KaratSergio committed Jan 25, 2024
1 parent e8fef69 commit 39dc38e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/components/Navigation/Navigation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const Navigation = () => {
<NavLink to="/contacts">Contacts</NavLink>
) : (
<div>
{!isLoggedIn && <Nav to="/register">Register</Nav>}
{!isLoggedIn && <Nav to="/login">Log In</Nav>}
{!isLoggedIn && <NavLink to="/register">Register</NavLink>}
{!isLoggedIn && <NavLink to="/login">Log In</NavLink>}
</div>
)}
</div>
Expand Down
66 changes: 66 additions & 0 deletions src/redux/auth/operations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { createAsyncThunk } from '@reduxjs/toolkit';
import axios from 'axios';

axios.defaults.baseURL = 'https://connections-api.herokuapp.com';

const setAuthHeader = token => {
axios.defaults.headers.common.Authorization = `Bearer ${token}`;
};

const clearAuthHeader = () => {
axios.defaults.headers.common.Authorization = '';
};

export const register = createAsyncThunk(
'auth/register',
async (credentials, thunkAPI) => {
try {
const response = await axios.post('/users/signup', credentials);
setAuthHeader(response.data.token);
return response.data;
} catch (error) {
return thunkAPI.rejectWithValue(error.message);
}
}
);

export const logIn = createAsyncThunk(
'auth/login',
async (credentials, thunkAPI) => {
try {
const response = await axios.post('/users/login', credentials);
setAuthHeader(response.data.token);
return response.data;
} catch (error) {
return thunkAPI.rejectWithValue(error.message);
}
}
);

export const logOut = createAsyncThunk('auth/logout', async (_, thunkAPI) => {
try {
await axios.post('/users/logout');
clearAuthHeader();
} catch (error) {
return thunkAPI.rejectWithValue(error.message);
}
});

export const refreshUser = createAsyncThunk(
'auth/refresh',
async (_, thunkAPI) => {
const state = thunkAPI.getState();
const persistedToken = state.auth.token;

if (!persistedToken) {
return thunkAPI.rejectWithValue('Unable to fetch user');
}
setAuthHeader(persistedToken);
try {
const userDataResponse = await axios.get('/users/current');
return userDataResponse.data;
} catch (error) {
return thunkAPI.rejectWithValue(error.message);
}
}
);
2 changes: 1 addition & 1 deletion src/redux/contacts/operations.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createAsyncThunk } from '@reduxjs/toolkit';
import axios from 'axios';

axios.defaults.baseURL = 'https://65ae13ba1dfbae409a73e083.mockapi.io';
axios.defaults.baseURL = 'https://connections-api.herokuapp.com';

export const fetchContacts = createAsyncThunk(
'contacts/fetchAll',
Expand Down

0 comments on commit 39dc38e

Please sign in to comment.