Skip to content

Instantly share code, notes, and snippets.

@svnlto
Last active July 18, 2017 02:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svnlto/9bd3a07f94995416f0af8720acc5ebbc to your computer and use it in GitHub Desktop.
Save svnlto/9bd3a07f94995416f0af8720acc5ebbc to your computer and use it in GitHub Desktop.
import { take, call, put, race } from 'redux-saga/effects';
import { forwardTo } from '../utils';
import AuthService from '../services/auth';
const auth = new AuthService();
import {
SENDING_REQUEST,
LOGIN_REQUEST,
REGISTER_REQUEST,
SET_AUTH,
LOGOUT,
CHANGE_FORM,
REQUEST_ERROR,
USER_PROFILE
} from '../constants/ActionTypes';
/**
* Effect to handle authorization
* @param {string} username The username of the user
* @param {string} password The password of the user
* @param {string} email The email of the user
* @param {boolean} options.isRegistering Is this a register request?
*/
export function* authorize ({username, password, email, isRegistering}) {
yield put({ type: SENDING_REQUEST, sending: true });
// try to register or log in the user, depending on the request
try {
let response;
if (isRegistering) {
response = yield call(auth.signup , {username, password, email});
} else {
response = yield call(auth.login, {username, password});
}
return response;
} catch (error) {
yield put({ type: REQUEST_ERROR, error });
return false;
} finally {
yield put({ type: SENDING_REQUEST, sending: false });
}
}
/**
* Effect to handle logging out
*/
export function* logout () {
yield put({ type: SENDING_REQUEST, sending: true });
try {
let response = yield call(auth.logout);
yield put({ type: SENDING_REQUEST, sending: false });
return response;
} catch (error) {
yield put({ type: REQUEST_ERROR, error });
}
}
export function* getProfile () {
const profile = yield call(auth.getProfile);
yield put({ type: SET_AUTH, isAuthenticated: true });
yield put({ type: USER_PROFILE, profile });
yield put({
type: CHANGE_FORM,
newFormState: {
username: '',
password: '',
email: ''
}
});
}
/**
* Log in saga
*/
export function* loginFlow () {
while (true) {
let request = yield take(LOGIN_REQUEST);
let { username, password } = request.data;
let winner = yield race({
auth: call(authorize, { username, password, isRegistering: false }),
logout: take(LOGOUT)
});
if (winner.auth) {
yield call(getProfile);
forwardTo('/dashboard');
} else if (winner.logout) {
yield put({ type: SET_AUTH, isAuthenticated: false });
yield call(logout);
forwardTo('/');
}
}
}
/**
* Log out saga
* This is basically the same as the `if (winner.logout)` of above, just written
* as a saga that is always listening to `LOGOUT` actions
*/
export function* logoutFlow () {
while (true) {
yield take(LOGOUT);
yield put({ type: SET_AUTH, isAuthenticated: false });
yield call(logout);
forwardTo('/');
}
}
/**
* Register saga
* Very similar to log in saga!
*/
export function* registerFlow () {
while (true) {
let request = yield take(REGISTER_REQUEST);
let { username, password, name, email } = request.data;
let wasSuccessful = yield call(authorize, {
username, password, name, email, isRegistering: true
});
if (wasSuccessful) {
yield put({ type: SET_AUTH, isAuthenticated: true });
yield put({
type: CHANGE_FORM,
newFormState: {
username: '',
password: '',
email: ''
}
});
forwardTo('/dashboard');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment