client/src/reducers/authReducer.js
author Alexandre Segura <mex.zktk@gmail.com>
Tue, 27 Jun 2017 18:12:10 +0200
changeset 100 6fd752d98933
parent 91 143ff08ec2cc
child 101 e165aa89ac82
permissions -rw-r--r--
Introduce group creation.
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';
91
Alexandre Segura <mex.zktk@gmail.com>
parents: 90
diff changeset
     4
import asyncRequest from '../constants/asyncRequest';
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
     5
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     6
export const isAuthenticated = (state = false, action) => {
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
     7
  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
     8
    case types.AUTH_DEAUTHENTICATE:
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
     9
    case types.AUTH_LOGOUT:
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    10
      return false;
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    11
    case types.AUTH_LOGIN_SUCCESS:
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    12
      return true;
12
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    13
    default:
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    14
      return state;
48ddaa42b810 Draft implementation of sessions.
Alexandre Segura <mex.zktk@gmail.com>
parents:
diff changeset
    15
  }
44
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
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    18
export const currentUser = (state = null, action) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    19
  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
    20
    case types.AUTH_DEAUTHENTICATE:
52
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    21
    case types.AUTH_LOGOUT:
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    22
      return null;
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    23
    case types.AUTH_LOGIN_SUCCESS:
67
9206af01f5e5 Change to UserRecord
ymh <ymh.work@gmail.com>
parents: 62
diff changeset
    24
      return new UserRecord(action.user);
53
d8588379529e Add settings page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 52
diff changeset
    25
    case types.USER_UPDATE_SETTINGS:
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    26
      return state.merge({
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    27
        first_name: action.firstname,
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    28
        last_name: action.lastname
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    29
      });
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    30
    default:
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    31
      return state;
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
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    35
export const token = (state = null, action) => {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    36
  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
    37
    case types.AUTH_DEAUTHENTICATE:
52
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    38
    case types.AUTH_LOGOUT:
96f8a4a59bd9 Implement logout.
Alexandre Segura <mex.zktk@gmail.com>
parents: 47
diff changeset
    39
      return null;
47
64428c7ebc19 Store token & user.
Alexandre Segura <mex.zktk@gmail.com>
parents: 44
diff changeset
    40
    case types.AUTH_STORE_TOKEN:
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    41
      return action.token;
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    42
    default:
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    43
      return state;
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
}
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    46
91
Alexandre Segura <mex.zktk@gmail.com>
parents: 90
diff changeset
    47
export const login = (state = asyncRequest, action) => {
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    48
  switch (action.type) {
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    49
    case types.AUTH_LOGIN_REQUEST:
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    50
      return Immutable.Map({
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    51
        loading: true,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    52
        success: false,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    53
        error: false,
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    54
      })
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    55
    case types.AUTH_LOGIN_SUCCESS:
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    56
    case types.AUTH_LOGIN_ERROR:
62
b2514a9bcd49 migrate to redux-offline + various optimisation
ymh <ymh.work@gmail.com>
parents: 53
diff changeset
    57
      return Immutable.Map({
44
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    58
        loading: false,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    59
        success: action.type === types.AUTH_LOGIN_SUCCESS,
3b20e2b584fe Introduce authentication through API.
Alexandre Segura <mex.zktk@gmail.com>
parents: 12
diff changeset
    60
        error: action.type === types.AUTH_LOGIN_ERROR,
90
990f2c928b15 Improve login errors.
Alexandre Segura <mex.zktk@gmail.com>
parents: 89
diff changeset
    61
        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
    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
}
89
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    67
91
Alexandre Segura <mex.zktk@gmail.com>
parents: 90
diff changeset
    68
export const register = (state = asyncRequest, action) => {
89
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    69
  switch (action.type) {
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    70
    case types.AUTH_REGISTER_REQUEST:
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    71
      return Immutable.Map({
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    72
        loading: true,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    73
        success: false,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    74
        error: false,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    75
      })
90
990f2c928b15 Improve login errors.
Alexandre Segura <mex.zktk@gmail.com>
parents: 89
diff changeset
    76
    case types.AUTH_LOGIN_SUCCESS:
89
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    77
    case types.AUTH_REGISTER_ERROR:
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    78
      return Immutable.Map({
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    79
        loading: false,
90
990f2c928b15 Improve login errors.
Alexandre Segura <mex.zktk@gmail.com>
parents: 89
diff changeset
    80
        success: action.type === types.AUTH_LOGIN_SUCCESS,
89
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    81
        error: action.type === types.AUTH_REGISTER_ERROR,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    82
        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
    83
      })
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    84
    default:
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    85
      return state
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    86
  }
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 88
diff changeset
    87
}
100
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
    88
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
    89
export const groups = (state = Immutable.List([]), action) => {
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
    90
  switch (action.type) {
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
    91
    default:
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
    92
      return state
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
    93
  }
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
    94
}
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
    95
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
    96
export const createGroup = (state = asyncRequest, action) => {
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
    97
  switch (action.type) {
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
    98
    case types.GROUP_CREATE_ASYNC:
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
    99
      return Immutable.Map({
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
   100
        loading: true,
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
   101
        success: false,
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
   102
        error: false,
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
   103
      })
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
   104
    case types.GROUP_CREATE_SUCCESS:
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
   105
    case types.GROUP_CREATE_ERROR:
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
   106
      return Immutable.Map({
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
   107
        loading: false,
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
   108
        success: action.type === types.GROUP_CREATE_SUCCESS,
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
   109
        error: action.type === types.GROUP_CREATE_ERROR,
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
   110
        errorMessages: action.type === types.GROUP_CREATE_ERROR ? Immutable.Map(action.error) : Immutable.Map({})
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
   111
      })
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
   112
    default:
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
   113
      return state
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
   114
  }
6fd752d98933 Introduce group creation.
Alexandre Segura <mex.zktk@gmail.com>
parents: 91
diff changeset
   115
}