Experimental try to move Auth module from Session to redux-saga

This commit is contained in:
Sukchan Lee 2017-06-09 21:43:15 +09:00
parent 3b993a0c52
commit ef96480b4b
8 changed files with 65 additions and 102 deletions

View File

@ -1,5 +1,3 @@
import { createAction } from 'redux-actions';
export const AUTH = {
LOGIN_REQUEST: 'auth/LOGIN_REQUEST',
LOGIN_SUCCESS: 'auth/LOGIN_SUCCESS',
@ -7,26 +5,11 @@ export const AUTH = {
LOGOUT: 'auth/LOGOUT'
}
/*
AuthAction.loginRequest
payload : {
username,
password
}
function action(type, payload = {}) {
return {type, ...payload}
}
AuthAction.loginSuccess
payload : {
username,
role
}
AuthAction.loginFailure
payload : null
AuthAction.logout
payload : null
*/
export const loginRequest = createAction(AUTH.LOGIN_REQUEST);
export const loginSuccess = createAction(AUTH.LOGIN_SUCCESS);
export const loginFailure = createAction(AUTH.LOGIN_FAILURE);
export const logout = createAction(AUTH.LOGOUT);
export const loginRequest = (username, password) => action(AUTH.LOGIN_REQUEST, {username, password})
export const loginSuccess = (username, role) => action(AUTH.LOGIN_SUCCESS, {username, role})
export const loginFailure = () => action(AUTH.LOGIN_FAILURE)
export const logout = () => action(AUTH.LOGOUT)

View File

@ -3,7 +3,8 @@ import { createAction } from 'redux-actions';
export const UI = {
TOGGLE_SIDEBAR: 'ui/TOGGLE_SIDEBAR',
SET_SIDEBAR_VISIBILITY: 'ui/SET_SIDEBAR_VISIBILITY',
SELECT_VIEW: 'ui/SELECT_VIEW'
SELECT_VIEW: 'ui/SELECT_VIEW',
TEST_UI: 'ui/SELECT_VIEW'
}
/*
@ -23,3 +24,4 @@ export const UI = {
export const toggleSidebar = createAction(UI.TOGGLE_SIDEBAR);
export const setSidebarVisibility = createAction(UI.SET_SIDEBAR_VISIBILITY);
export const selectView = createAction(UI.SELECT_VIEW);
export const testUI = ({type:UI.TEST_UI, argu:true})

View File

@ -42,7 +42,7 @@ const Thumbnail = styled.div`
const propTypes = {
onSidebarToggle: PropTypes.func.isRequired,
onLogout: PropTypes.func.isRequired
onLogoutRequest: PropTypes.func.isRequired
}
const Header = ({ onSidebarToggle, onLogoutRequest }) => (

View File

@ -6,10 +6,6 @@ import Router from 'next/router';
import NProgress from 'nprogress';
import Session from 'services/session';
import * as authActions from 'actions/auth';
import * as uiActions from 'actions/ui';
import { Login } from 'components';
class Auth extends Component {
@ -61,6 +57,10 @@ class Auth extends Component {
password
} = this.state.form;
const {
loginRequest
} = this.props;
NProgress.configure({ showSpinner: false });
NProgress.start();
@ -118,11 +118,4 @@ class Auth extends Component {
}
}
Auth = connect(
null,
(dispatch) => ({
UIActions: bindActionCreators(uiActions, dispatch)
})
)(Auth);
export default Auth;

View File

@ -1,4 +1,3 @@
import { handleActions } from 'redux-actions';
import { AUTH } from 'actions/auth';
const initialState = {
@ -10,45 +9,47 @@ const initialState = {
}
}
export default handleActions({
[AUTH.LOGIN_REQUEST]: (state, action) => ({
...state,
isAuthenticating: true,
isAuthenticated: false,
session : {
...state.session,
username: action.payload.username,
role: ''
}
}),
[AUTH.LOGIN_SUCCESS]: (state, action) => ({
...state,
isAuthenticating: false,
isAuthenticated: true,
session : {
...state.session,
username: action.payload.username,
role: action.payload.role
}
}),
[AUTH.LOGIN_FAILURE]: (state, action) => ({
...state,
isAuthenticating: false,
isAuthenticated: false,
session : {
...state.session,
username: '',
role: ''
}
}),
[AUTH.LOGOUT]: (state, action) => ({
...state,
isAuthenticating: false,
isAuthenticated: false,
session : {
...state.session,
username: '',
role: ''
}
})
}, initialState);
function auth(state = initialState, action) {
switch (action.type) {
case AUTH.LOGIN_REQUEST:
return {
...state,
isAuthenticating: true,
isAuthenticated: false,
session : {
...state.session,
username: action.username,
role: ''
}
}
case AUTH.LOGIN_SUCCESS:
return {
...state,
isAuthenticating: false,
isAuthenticated: true,
session : {
...state.session,
username: action.username,
role: action.role
}
}
case AUTH.LOGIN_FAILURE:
return {
...state,
isAuthenticating: false,
isAuthenticated: false,
session : null
}
case AUTH.LOGOUT:
return {
...state,
isAuthenticating: false,
isAuthenticated: false,
session : null
}
default:
return state;
}
}
export default auth;

View File

@ -1,8 +1,7 @@
import { take, fork, cancel, call, put } from 'redux-saga/effects';
import Session from 'services/session';
import { AUTH, loginSucess} from 'actions/auth';
/* import { UI, toggleSidebar } from 'actions/ui'; */
import { AUTH, loginSuccess } from 'actions/auth';
function* authorize(username, password) {
try {
@ -21,9 +20,8 @@ function* authorize(username, password) {
function* loginFlow() {
while(true) {
const { payload } = yield take(AUTH.LOGIN_REQUEST);
const { username, password } = payload;
const task = yield fork(authroize, username, password);
const { username, password } = yield take(AUTH.LOGIN_REQUEST);
const task = yield fork(authorize, username, password);
const action = yield take([AUTH.LOGOUT, AUTH.LOGIN_FAILURE]);
if (action.type === AUTH.LOGOUT)
yield cancel(task);
@ -32,18 +30,4 @@ function* loginFlow() {
export default function* () {
yield loginFlow();
}
/*
function* testCode() {
yield put(toggleSidebar());
}
function* loginFlow() {
yield takeEvery(UI.SELECT_VIEW, testCode)
}
export default function* () {
yield loginFlow();
}
*/
}

View File

@ -143,7 +143,7 @@ export default class Session {
// Update local session data
this._session = await this.getSession(true)
return resolve(this._session)
return resolve(true)
}
}
xhr.onerror = () => {

View File

@ -21,4 +21,4 @@ export const initStore = () => {
sagaMiddleware.run(rootSaga);
return store;
}
}