client/src/reducers/authReducer.js
author Alexandre Segura <mex.zktk@gmail.com>
Mon, 26 Jun 2017 16:43:22 +0200
changeset 90 990f2c928b15
parent 89 06f609adfbf8
child 91 143ff08ec2cc
permissions -rw-r--r--
Improve login errors.
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) {
88
2a861fed6bde Deauthenticate user if refresh was not possible (session expired or total delta reached)
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
     7
    case types.AUTH_DEAUTHENTICATE:
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
     8
    case types.AUTH_LOGOUT:
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
     9
      return false;
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    10
    case types.AUTH_LOGIN_SUCCESS:
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    11
      return true;
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    12
    default:
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    13
      return state;
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    14
  }
44
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
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    17
export const currentUser = (state = null, action) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    18
  switch (action.type) {
88
2a861fed6bde Deauthenticate user if refresh was not possible (session expired or total delta reached)
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
    19
    case types.AUTH_DEAUTHENTICATE:
52
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    20
    case types.AUTH_LOGOUT:
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    21
      return null;
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    22
    case types.AUTH_LOGIN_SUCCESS:
67
9206af01f5e5 Change to UserRecord
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    23
      return new UserRecord(action.user);
53
d8588379529e Add settings page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 52
diff changeset
    24
    case types.USER_UPDATE_SETTINGS:
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    25
      return state.merge({
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    26
        first_name: action.firstname,
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    27
        last_name: action.lastname
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    28
      });
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    29
    default:
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    30
      return state;
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
}
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    33
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    34
export const token = (state = null, action) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    35
  switch (action.type) {
88
2a861fed6bde Deauthenticate user if refresh was not possible (session expired or total delta reached)
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
    36
    case types.AUTH_DEAUTHENTICATE:
52
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    37
    case types.AUTH_LOGOUT:
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    38
      return null;
47
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    39
    case types.AUTH_STORE_TOKEN:
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    40
      return action.token;
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    41
    default:
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    42
      return state;
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    43
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    44
}
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    45
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    46
const loginState = Immutable.Map({
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    47
  loading: false,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    48
  success: false,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    49
  error: false,
90
990f2c928b15 Improve login errors.
Alexandre Segura <mex.zktk@gmail.com>
parents: 89
diff changeset
    50
  errorMessages: Immutable.Map({})
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    51
});
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    52
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    53
export const login = (state = loginState, action) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    54
  switch (action.type) {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    55
    case types.AUTH_LOGIN_REQUEST:
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    56
      return Immutable.Map({
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    57
        loading: true,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    58
        success: false,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    59
        error: false,
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    60
      })
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    61
    case types.AUTH_LOGIN_SUCCESS:
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    62
    case types.AUTH_LOGIN_ERROR:
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    63
      return Immutable.Map({
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    64
        loading: false,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    65
        success: action.type === types.AUTH_LOGIN_SUCCESS,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    66
        error: action.type === types.AUTH_LOGIN_ERROR,
90
990f2c928b15 Improve login errors.
Alexandre Segura <mex.zktk@gmail.com>
parents: 89
diff changeset
    67
        errorMessages: action.type === types.AUTH_LOGIN_ERROR ? Immutable.Map(action.error) : Immutable.Map({})
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    68
      })
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    69
    default:
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    70
      return state
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    71
  }
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    72
}
89
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    73
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    74
const registerState = Immutable.Map({
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    75
  loading: false,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    76
  success: false,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    77
  error: false,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    78
  errorMessages: Immutable.Map({})
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    79
});
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    80
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    81
export const register = (state = registerState, action) => {
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    82
  switch (action.type) {
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    83
    case types.AUTH_REGISTER_REQUEST:
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    84
      return Immutable.Map({
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    85
        loading: true,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    86
        success: false,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    87
        error: false,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    88
      })
90
990f2c928b15 Improve login errors.
Alexandre Segura <mex.zktk@gmail.com>
parents: 89
diff changeset
    89
    case types.AUTH_LOGIN_SUCCESS:
89
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    90
    case types.AUTH_REGISTER_ERROR:
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    91
      return Immutable.Map({
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    92
        loading: false,
90
990f2c928b15 Improve login errors.
Alexandre Segura <mex.zktk@gmail.com>
parents: 89
diff changeset
    93
        success: action.type === types.AUTH_LOGIN_SUCCESS,
89
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    94
        error: action.type === types.AUTH_REGISTER_ERROR,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    95
        errorMessages: action.type === types.AUTH_REGISTER_ERROR ? Immutable.Map(action.error) : Immutable.Map({})
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    96
      })
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    97
    default:
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    98
      return state
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    99
  }
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
   100
}