client/src/sagas/authSaga.js
changeset 89 06f609adfbf8
parent 87 dbcee57de2c6
child 90 990f2c928b15
equal deleted inserted replaced
88:2a861fed6bde 89:06f609adfbf8
    35         yield put({ type: types.AUTH_LOGIN_ERROR, error: e });
    35         yield put({ type: types.AUTH_LOGIN_ERROR, error: e });
    36     }
    36     }
    37   }
    37   }
    38 }
    38 }
    39 
    39 
       
    40 export function* watchRegisterSubmit() {
       
    41   while (true) {
       
    42     const { username, email, password1, password2 } = yield take(types.AUTH_REGISTER_SUBMIT);
       
    43     yield put({ type: types.AUTH_REGISTER_REQUEST, username, email, password1, password2 });
       
    44   }
       
    45 }
       
    46 
       
    47 function* watchRegisterRequest(context) {
       
    48   while (true) {
       
    49     try {
       
    50 
       
    51         const { username, email, password1, password2 } = yield take(types.AUTH_REGISTER_REQUEST);
       
    52 
       
    53         const client = context.client;
       
    54         const response = yield client.post('/api/auth/registration/', {
       
    55           username,
       
    56           email,
       
    57           password1,
       
    58           password2
       
    59         });
       
    60 
       
    61         const actions = [{
       
    62           type: types.AUTH_STORE_TOKEN_ASYNC,
       
    63           token: response.token,
       
    64         }, {
       
    65           type: types.AUTH_REGISTER_SUCCESS,
       
    66           user: response.user,
       
    67           token: response.token,
       
    68         }];
       
    69 
       
    70         yield all(actions.map(action => put(action)));
       
    71         context.history.push('/sessions');
       
    72 
       
    73     } catch(e) {
       
    74       yield put({ type: types.AUTH_REGISTER_ERROR, error: e });
       
    75     }
       
    76   }
       
    77 }
       
    78 
    40 function* watchStoreToken() {
    79 function* watchStoreToken() {
    41   while (true) {
    80   while (true) {
    42     const { token } = yield take(types.AUTH_STORE_TOKEN_ASYNC);
    81     const { token } = yield take(types.AUTH_STORE_TOKEN_ASYNC);
    43     yield put({ type: types.AUTH_STORE_TOKEN, token });
    82     yield put({ type: types.AUTH_STORE_TOKEN, token });
    44   }
    83   }
    65 
   104 
    66 export default function* rootSaga(context) {
   105 export default function* rootSaga(context) {
    67   yield all([
   106   yield all([
    68     watchLoginSubmit(),
   107     watchLoginSubmit(),
    69     watchLoginRequest(context),
   108     watchLoginRequest(context),
       
   109     watchRegisterSubmit(),
       
   110     watchRegisterRequest(context),
    70     watchStoreToken(),
   111     watchStoreToken(),
    71     watchUpdateSettings(context),
   112     watchUpdateSettings(context),
    72   ])
   113   ])
    73 }
   114 }