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