mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2026-03-10 21:25:04 +00:00
Before finishing authentication
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import {SET_USER, SET_ERRORS, CLEAR_ERRORS, LOADING_UI} from '../types';
|
||||
import {SET_USER, SET_ERRORS, CLEAR_ERRORS, LOADING_UI, SET_AUTHENTICATED, SET_UNAUTHENTICATED} from '../types';
|
||||
import axios from 'axios';
|
||||
|
||||
|
||||
@@ -19,9 +19,7 @@ export const loginUser = (loginData, history) => (dispatch) => {
|
||||
.post("/login", loginData)
|
||||
.then((res) => {
|
||||
// Save the login token
|
||||
const FBIdToken = `Bearer ${res.data.token}`;
|
||||
localStorage.setItem('FBIdToken', FBIdToken);
|
||||
axios.defaults.headers.common['Authorization'] = FBIdToken;
|
||||
setAuthorizationHeader(res.data.token);
|
||||
dispatch(getUserData());
|
||||
dispatch({ type: CLEAR_ERRORS })
|
||||
// Redirects to home page
|
||||
@@ -33,4 +31,36 @@ export const loginUser = (loginData, history) => (dispatch) => {
|
||||
payload: err.response.data,
|
||||
})
|
||||
});
|
||||
};
|
||||
|
||||
export const signupUser = (newUserData, history) => (dispatch) => {
|
||||
dispatch({ type: LOADING_UI });
|
||||
axios
|
||||
.post("/signup", newUserData)
|
||||
.then((res) => {
|
||||
// Save the signup token
|
||||
setAuthorizationHeader(res.data.token);
|
||||
dispatch(getUserData());
|
||||
dispatch({ type: CLEAR_ERRORS })
|
||||
// Redirects to home page
|
||||
history.push('/home');
|
||||
})
|
||||
.catch((err) => {
|
||||
dispatch ({
|
||||
type: SET_ERRORS,
|
||||
payload: err.response.data,
|
||||
})
|
||||
});
|
||||
};
|
||||
|
||||
export const logoutUser = () => (dispatch) => {
|
||||
localStorage.removeItem('FBIdToken');
|
||||
delete axios.defaults.headers.common['Authorization'];
|
||||
dispatch({ type: SET_UNAUTHENTICATED });
|
||||
}
|
||||
|
||||
const setAuthorizationHeader = (token) => {
|
||||
const FBIdToken = `Bearer ${token}`;
|
||||
localStorage.setItem('FBIdToken', FBIdToken);
|
||||
axios.defaults.headers.common['Authorization'] = FBIdToken;
|
||||
}
|
||||
Reference in New Issue
Block a user