update it
This commit is contained in:
parent
239b6790ac
commit
3b993a0c52
|
@ -7,7 +7,26 @@ export const AUTH = {
|
|||
LOGOUT: 'auth/LOGOUT'
|
||||
}
|
||||
|
||||
export const loginRequest = createAction(AUTH.LOGIN_REQUEST); // { username, password }
|
||||
/*
|
||||
AuthAction.loginRequest
|
||||
payload : {
|
||||
username,
|
||||
password
|
||||
}
|
||||
|
||||
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);
|
|
@ -6,6 +6,20 @@ export const UI = {
|
|||
SELECT_VIEW: 'ui/SELECT_VIEW'
|
||||
}
|
||||
|
||||
export const toggleSidebar = createAction(UI.TOGGLE_SIDEBAR); // No payload
|
||||
export const setSidebarVisibility = createAction(UI.SET_SIDEBAR_VISIBILITY); // isOpen
|
||||
export const selectView = createAction(UI.SELECT_VIEW); // view
|
||||
/*
|
||||
UIAction.toggleSidebar
|
||||
payload : null
|
||||
|
||||
UIAction.setSidebarVisibiliy
|
||||
payload : {
|
||||
isOpen
|
||||
}
|
||||
|
||||
UIAction.selectView
|
||||
payload : {
|
||||
view
|
||||
}
|
||||
*/
|
||||
export const toggleSidebar = createAction(UI.TOGGLE_SIDEBAR);
|
||||
export const setSidebarVisibility = createAction(UI.SET_SIDEBAR_VISIBILITY);
|
||||
export const selectView = createAction(UI.SELECT_VIEW);
|
||||
|
|
|
@ -4,7 +4,7 @@ import PropTypes from 'prop-types';
|
|||
import styled from 'styled-components';
|
||||
import oc from 'open-color';
|
||||
|
||||
import { Modal } from '../../components';
|
||||
import { Modal } from 'components';
|
||||
|
||||
const TitleWrapper = styled.div`
|
||||
padding-left: 1rem;
|
||||
|
|
|
@ -2,7 +2,8 @@ import { handleActions } from 'redux-actions';
|
|||
import { AUTH } from 'actions/auth';
|
||||
|
||||
const initialState = {
|
||||
isLoggedIn: false,
|
||||
isAuthenticating: false,
|
||||
isAuthenticated: false,
|
||||
session : {
|
||||
username : '',
|
||||
role: ''
|
||||
|
@ -10,13 +11,44 @@ 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,
|
||||
isLoggedIn: true,
|
||||
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);
|
|
@ -1,7 +1,9 @@
|
|||
import { combineReducers } from 'redux';
|
||||
|
||||
import auth from './auth';
|
||||
import ui from './ui';
|
||||
|
||||
export default combineReducers({
|
||||
auth,
|
||||
ui
|
||||
});
|
|
@ -1,8 +1,40 @@
|
|||
import { takeEvery, put, call, take } from 'redux-saga/effects';
|
||||
import { take, fork, cancel, call, put } from 'redux-saga/effects';
|
||||
|
||||
import { AUTH } from 'actions/auth';
|
||||
import { UI, toggleSidebar } from 'actions/ui';
|
||||
import Session from 'services/session';
|
||||
import { AUTH, loginSucess} from 'actions/auth';
|
||||
/* import { UI, toggleSidebar } from 'actions/ui'; */
|
||||
|
||||
function* authorize(username, password) {
|
||||
try {
|
||||
const session = new Session();
|
||||
const sessionData = yield call(session.signin, username, password);
|
||||
const { role } = sessionData.user;
|
||||
yield put(loginSuccess, username, role);
|
||||
} catch (error) {
|
||||
yield put(loginFailure);
|
||||
} finally {
|
||||
if (yield cancelled()) {
|
||||
// .. put special cancellation handling code here
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function* loginFlow() {
|
||||
while(true) {
|
||||
const { payload } = yield take(AUTH.LOGIN_REQUEST);
|
||||
const { username, password } = payload;
|
||||
const task = yield fork(authroize, username, password);
|
||||
const action = yield take([AUTH.LOGOUT, AUTH.LOGIN_FAILURE]);
|
||||
if (action.type === AUTH.LOGOUT)
|
||||
yield cancel(task);
|
||||
}
|
||||
}
|
||||
|
||||
export default function* () {
|
||||
yield loginFlow();
|
||||
}
|
||||
|
||||
/*
|
||||
function* testCode() {
|
||||
yield put(toggleSidebar());
|
||||
}
|
||||
|
@ -13,4 +45,5 @@ function* loginFlow() {
|
|||
|
||||
export default function* () {
|
||||
yield loginFlow();
|
||||
}
|
||||
}
|
||||
*/
|
|
@ -143,7 +143,7 @@ export default class Session {
|
|||
// Update local session data
|
||||
this._session = await this.getSession(true)
|
||||
|
||||
return resolve(true)
|
||||
return resolve(this._session)
|
||||
}
|
||||
}
|
||||
xhr.onerror = () => {
|
||||
|
|
Loading…
Reference in New Issue