Skip to content

Commit 17282e5

Browse files
committed
signin and signup test
relates to #59
1 parent 79d9cf2 commit 17282e5

File tree

9 files changed

+628
-12
lines changed

9 files changed

+628
-12
lines changed

client/src/actions/index.js

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import axios from 'axios';
2+
import history from './history';
3+
import { AUTH_USER, UNAUTH_USER, AUTH_ERROR } from './types'
4+
5+
export const getUser = () => {
6+
return(dispatch) => {
7+
axios.get('/getuser', {
8+
headers: {authorization: localStorage.getItem('token')}
9+
})
10+
.then(response => {
11+
dispatch({
12+
type: AUTH_USER
13+
})
14+
})
15+
.catch(err => {
16+
dispatch({
17+
type: UNAUTH_USER
18+
})
19+
})
20+
}
21+
}
22+
23+
export const signupUser = values => {
24+
return dispatch => {
25+
axios.post('/signup', values)
26+
.then(response => {
27+
dispatch({
28+
type: AUTH_USER
29+
})
30+
localStorage.setItem('token', response.data.token)
31+
history.push('/recipes')
32+
})
33+
.catch(error => {
34+
dispatch(authError(error.response.data.error));
35+
})
36+
}
37+
}
38+
39+
export const signinUser = values => {
40+
return dispatch => {
41+
axios.post('/signin', values)
42+
.then(response => {
43+
dispatch({
44+
type: AUTH_USER
45+
})
46+
localStorage.setItem('token', response.data.token)
47+
history.push('/recipes')
48+
})
49+
.catch(error => {
50+
dispatch(authError('Email or password was incorrect'));
51+
})
52+
}
53+
}
54+
55+
export const authError = (error) => {
56+
return {
57+
type: AUTH_ERROR,
58+
payload: error
59+
};
60+
}

0 commit comments

Comments
 (0)