client/src/sagas/authSaga.js
author ymh <ymh.work@gmail.com>
Tue, 18 Dec 2018 02:27:22 +0100
changeset 199 c78d579f4b55
parent 134 be36eed5e6e0
permissions -rw-r--r--
Correct the registration screen. First version of a password reset screen.
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) {
134
be36eed5e6e0 add menu to change current group and create a new group
ymh <ymh.work@gmail.com>
parents: 90
diff changeset
    98
      //TODO: handle error
87
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
199
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   103
export function* watchResetSubmit() {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   104
  while (true) {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   105
    const { email } = yield take(types.AUTH_RESET_SUBMIT);
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   106
    yield put({ type: types.AUTH_RESET_REQUEST, email });
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   107
  }
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   108
}
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   109
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   110
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   111
function* watchResetRequest(context) {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   112
  while (true) {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   113
    try {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   114
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   115
        const { email } = yield take(types.AUTH_RESET_REQUEST);
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   116
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   117
        const client = context.client;
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   118
        yield client.post('/api/auth/password/reset/', { email });
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   119
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   120
        context.history.push('/sessions');
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   121
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   122
    } catch(e) {
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   123
      yield put({ type: types.AUTH_RESET_ERROR, error: e });
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   124
    }
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   125
  }
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   126
}
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   127
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   128
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   129
87
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
// ---
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
export default function* rootSaga(context) {
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
  yield all([
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
    watchLoginSubmit(),
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
    watchLoginRequest(context),
89
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
   136
    watchRegisterSubmit(),
06f609adfbf8 Add registration page.
Alexandre Segura <mex.zktk@gmail.com>
parents: 87
diff changeset
   137
    watchRegisterRequest(context),
87
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
    watchStoreToken(),
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
    watchUpdateSettings(context),
199
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   140
    watchResetSubmit(),
c78d579f4b55 Correct the registration screen.
ymh <ymh.work@gmail.com>
parents: 134
diff changeset
   141
    watchResetRequest(context),
87
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
  ])
dbcee57de2c6 Add first implementation of network monitor
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
}