client/src/sagas/authSaga.js
changeset 89 06f609adfbf8
parent 87 dbcee57de2c6
child 90 990f2c928b15
--- a/client/src/sagas/authSaga.js	Mon Jun 26 16:05:47 2017 +0200
+++ b/client/src/sagas/authSaga.js	Mon Jun 26 15:45:50 2017 +0200
@@ -37,6 +37,45 @@
   }
 }
 
+export function* watchRegisterSubmit() {
+  while (true) {
+    const { username, email, password1, password2 } = yield take(types.AUTH_REGISTER_SUBMIT);
+    yield put({ type: types.AUTH_REGISTER_REQUEST, username, email, password1, password2 });
+  }
+}
+
+function* watchRegisterRequest(context) {
+  while (true) {
+    try {
+
+        const { username, email, password1, password2 } = yield take(types.AUTH_REGISTER_REQUEST);
+
+        const client = context.client;
+        const response = yield client.post('/api/auth/registration/', {
+          username,
+          email,
+          password1,
+          password2
+        });
+
+        const actions = [{
+          type: types.AUTH_STORE_TOKEN_ASYNC,
+          token: response.token,
+        }, {
+          type: types.AUTH_REGISTER_SUCCESS,
+          user: response.user,
+          token: response.token,
+        }];
+
+        yield all(actions.map(action => put(action)));
+        context.history.push('/sessions');
+
+    } catch(e) {
+      yield put({ type: types.AUTH_REGISTER_ERROR, error: e });
+    }
+  }
+}
+
 function* watchStoreToken() {
   while (true) {
     const { token } = yield take(types.AUTH_STORE_TOKEN_ASYNC);
@@ -67,6 +106,8 @@
   yield all([
     watchLoginSubmit(),
     watchLoginRequest(context),
+    watchRegisterSubmit(),
+    watchRegisterRequest(context),
     watchStoreToken(),
     watchUpdateSettings(context),
   ])