client/src/sagas/authSaga.js
author Alexandre Segura <mex.zktk@gmail.com>
Mon, 26 Jun 2017 16:43:22 +0200
changeset 90 990f2c928b15
parent 89 06f609adfbf8
child 134 be36eed5e6e0
permissions -rw-r--r--
Improve login errors.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
87
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
import { put, take, all } from 'redux-saga/effects'
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
import * as types from '../constants/actionTypes';
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
// ---
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
function* watchLoginSubmit() {
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
  while (true) {
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
    const { username, password } = yield take(types.AUTH_LOGIN_SUBMIT);
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
    yield put({ type: types.AUTH_LOGIN_REQUEST, username, password });
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
  }
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
}
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
function* watchLoginRequest(context) {
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
  while (true) {
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
    try {
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
        const { username, password } = yield take(types.AUTH_LOGIN_REQUEST);
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
        const client = context.client;
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
        const response = yield client.post('/api/auth/login/', { username, password });
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
        const actions = [{
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
          type: types.AUTH_STORE_TOKEN_ASYNC,
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
          token: response.token,
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
        },
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
        {
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
          type: types.AUTH_LOGIN_SUCCESS,
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
          user: response.user,
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
          token: response.token,
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
        }];
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
        yield all(actions.map(action => put(action)));
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
        context.history.push('/sessions');
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    } catch(e) {
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
        yield put({ type: types.AUTH_LOGIN_ERROR, error: e });
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    }
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
  }
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
}
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
89
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    40
export function* watchRegisterSubmit() {
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    41
  while (true) {
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    42
    const { username, email, password1, password2 } = yield take(types.AUTH_REGISTER_SUBMIT);
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    43
    yield put({ type: types.AUTH_REGISTER_REQUEST, username, email, password1, password2 });
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    44
  }
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    45
}
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    46
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    47
function* watchRegisterRequest(context) {
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    48
  while (true) {
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    49
    try {
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    50
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    51
        const { username, email, password1, password2 } = yield take(types.AUTH_REGISTER_REQUEST);
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    52
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    53
        const client = context.client;
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    54
        const response = yield client.post('/api/auth/registration/', {
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    55
          username,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    56
          email,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    57
          password1,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    58
          password2
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    59
        });
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    60
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    61
        const actions = [{
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    62
          type: types.AUTH_STORE_TOKEN_ASYNC,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    63
          token: response.token,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    64
        }, {
90
990f2c928b15 Improve login errors.
Alexandre Segura <mex.zktk@gmail.com>
parents: 89
diff changeset
    65
          type: types.AUTH_LOGIN_SUCCESS,
89
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    66
          user: response.user,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    67
          token: response.token,
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    68
        }];
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    69
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    70
        yield all(actions.map(action => put(action)));
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    71
        context.history.push('/sessions');
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    72
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    73
    } catch(e) {
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    74
      yield put({ type: types.AUTH_REGISTER_ERROR, error: e });
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    75
    }
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    76
  }
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    77
}
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
    78
87
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
function* watchStoreToken() {
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
  while (true) {
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
    const { token } = yield take(types.AUTH_STORE_TOKEN_ASYNC);
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
    yield put({ type: types.AUTH_STORE_TOKEN, token });
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
  }
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
}
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
function* watchUpdateSettings(context) {
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
  while (true) {
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
    const { username, firstname, lastname } = yield take(types.USER_UPDATE_SETTINGS_ASYNC);
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
    const client = context.client;
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
    try {
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
      yield client.put('/api/auth/user/', {
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
        username,
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
        first_name: firstname,
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
        last_name: lastname
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
      });
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
      yield put({ type: types.USER_UPDATE_SETTINGS, firstname, lastname });
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
    } catch (e) {
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
    }
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
  }
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
}
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
// ---
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
export default function* rootSaga(context) {
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
  yield all([
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
    watchLoginSubmit(),
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
    watchLoginRequest(context),
89
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
   109
    watchRegisterSubmit(),
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
   110
    watchRegisterRequest(context),
87
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
    watchStoreToken(),
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
    watchUpdateSettings(context),
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
  ])
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
}