client/src/reducers/authReducer.js
author ymh <ymh.work@gmail.com>
Thu, 22 Jun 2017 12:53:00 +0200
changeset 77 cd27d9fc1c73
parent 67 9206af01f5e5
child 88 2a861fed6bde
permissions -rw-r--r--
some linting
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
     1
import Immutable from 'immutable';
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
     2
import * as types from '../constants/actionTypes';
67
9206af01f5e5 Change to UserRecord
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
     3
import UserRecord from '../store/userRecord';
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
     4
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     5
export const isAuthenticated = (state = false, action) => {
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     6
  switch (action.type) {
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
     7
    case types.AUTH_LOGOUT:
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
     8
      return false;
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
     9
    case types.AUTH_LOGIN_SUCCESS:
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    10
      return true;
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    11
    default:
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    12
      return state;
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    13
  }
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    14
}
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    15
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    16
export const currentUser = (state = null, action) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    17
  switch (action.type) {
52
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    18
    case types.AUTH_LOGOUT:
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    19
      return null;
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    20
    case types.AUTH_LOGIN_SUCCESS:
67
9206af01f5e5 Change to UserRecord
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    21
      return new UserRecord(action.user);
53
d8588379529e Add settings page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 52
diff changeset
    22
    case types.USER_UPDATE_SETTINGS:
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    23
      return state.merge({
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    24
        first_name: action.firstname,
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    25
        last_name: action.lastname
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    26
      });
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    27
    default:
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    28
      return state;
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    29
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    30
}
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    31
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    32
export const token = (state = null, action) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    33
  switch (action.type) {
52
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    34
    case types.AUTH_LOGOUT:
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    35
      return null;
47
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    36
    case types.AUTH_STORE_TOKEN:
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    37
      return action.token;
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    38
    default:
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    39
      return state;
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    40
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    41
}
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    42
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    43
const loginState = Immutable.Map({
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    44
  loading: false,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    45
  success: false,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    46
  error: false,
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    47
});
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    48
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    49
export const login = (state = loginState, action) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    50
  switch (action.type) {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    51
    case types.AUTH_LOGIN_REQUEST:
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    52
      return Immutable.Map({
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    53
        loading: true,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    54
        success: false,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    55
        error: false,
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    56
      })
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    57
    case types.AUTH_LOGIN_SUCCESS:
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    58
    case types.AUTH_LOGIN_ERROR:
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    59
      return Immutable.Map({
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    60
        loading: false,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    61
        success: action.type === types.AUTH_LOGIN_SUCCESS,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    62
        error: action.type === types.AUTH_LOGIN_ERROR,
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    63
      })
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    64
    default:
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    65
      return state
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    66
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    67
}