generated from goitacademy/react-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e8fef69
commit 39dc38e
Showing
3 changed files
with
69 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters