|
1 | | -import axios from 'axios' |
2 | 1 | import update from 'immutability-helper'; |
3 | | -import { getCurrentUser, CLEAR_USER } from './user' |
4 | | -import { REQUEST, SUCCESS, FAILURE } from './actionType' |
5 | | -import { SubmissionError } from 'redux-form' |
6 | 2 |
|
7 | | -export const AUTH_USER = 'AUTH_USER' |
8 | | -export const AUTH_USER_2 = 'AUTH_USER_2' |
9 | | -export const UNAUTH_USER = 'UNAUTH_USER' |
| 3 | +export const REQUEST_LOGIN = 'REQUEST_LOGIN' |
| 4 | +export const REQUEST_LOGOUT = 'REQUEST_LOGOUT' |
| 5 | + |
| 6 | +export const SET_TOKEN = 'SET_TOKEN' |
| 7 | +export const SET_AUTHENTICATED = 'SET_AUTHENTICATED' |
| 8 | +export const SET_UNAUTHENTICATED = 'SET_UNAUTHENTICATED' |
10 | 9 |
|
11 | 10 | export default function authReducer(state = {authenticated: false, token: null}, action) { |
12 | 11 | switch (action.type) { |
13 | | - case SUCCESS(AUTH_USER): |
14 | | - return update(state, {authenticated: {$set: true},token: {$set: action.payload.data.token}}) |
15 | | - case AUTH_USER_2: |
| 12 | + case SET_TOKEN: |
| 13 | + return update(state, {token: {$set: action.payload.data.token}}) |
| 14 | + case SET_AUTHENTICATED: |
16 | 15 | return update(state, {authenticated: {$set: true}}) |
17 | | - case UNAUTH_USER: |
| 16 | + case SET_UNAUTHENTICATED: |
18 | 17 | return update(state, {authenticated: {$set: false}, token: {$set: null}}) |
19 | 18 | default: |
20 | 19 | return state |
21 | 20 | } |
22 | 21 | } |
23 | 22 |
|
24 | | -export const authenticated = () => ({ |
25 | | - type: AUTH_USER_2, |
26 | | - payload: null |
27 | | -}) |
28 | | - |
29 | | -export const login = ({username, password}) => dispatch => |
30 | | - dispatch({ |
31 | | - type: AUTH_USER, |
32 | | - payload: axios.post(`/signin`, {username, password}) |
33 | | - }).then(({value, action}) => { |
34 | | - localStorage.setItem('auth-token', value.data.token) |
35 | | - dispatch(getCurrentUser()) |
36 | | - }).catch((error) => { |
37 | | - throw new SubmissionError({_error: 'Bad Login'}); |
38 | | - }) |
39 | | - |
40 | | -export const register = ({username, password}) => dispatch => |
41 | | - dispatch({ |
42 | | - type: AUTH_USER, |
43 | | - payload: axios.post(`/signup`, {username, password}) |
44 | | - }).then(({value, action}) => { |
45 | | - localStorage.setItem('auth-token', value.data.token) |
46 | | - dispatch(getCurrentUser()); |
47 | | - // TODO: init global variables |
48 | | - }) |
49 | | - |
50 | | -export const signoutUser = () => dispatch => { |
51 | | - localStorage.removeItem('auth-token'); |
52 | | - dispatch({type: 'USER_LOGOUT'}); |
| 23 | +export function loginRequest ({username, password}) { |
| 24 | + return {type: REQUEST_LOGIN, payload: {username, password}} |
53 | 25 | } |
54 | 26 |
|
55 | | -export const clearAuthToken = () => { |
56 | | - localStorage.removeItem('auth-token'); |
57 | | -}; |
58 | | - |
| 27 | +export const signoutUser = () => { |
| 28 | + return ({type: REQUEST_LOGOUT}); |
| 29 | +} |
0 commit comments