@@ -4,7 +4,7 @@ import { setState, deleteState } from '../Data';
44import { stopSubmit } from 'redux-form/immutable' ;
55import { ValidationError } from 'joi' ;
66import { push } from 'connected-react-router' ;
7- import { setCredentials } from './storage' ;
7+ import { setCredentials , clearCredentials } from './storage' ;
88import { login } from './actions' ;
99import { login as loginApi } from '../../api' ;
1010
@@ -14,7 +14,7 @@ function* loginSaga(action: any) {
1414 yield put ( setState ( action . payload . credentials , 'credentials' ) ) ;
1515 yield put ( loadDataDone ( data , action . meta ) ) ;
1616 yield put ( setState ( data , 'auth' ) ) ;
17- setCredentials ( { ... action . payload . credentials } ) ;
17+ setCredentials ( { credentials : action . payload . credentials , auth : data } ) ;
1818 if ( action . payload . redirectAfter ) {
1919 yield put ( push ( action . payload . redirectAfter ) ) ;
2020 }
@@ -45,7 +45,7 @@ function* loginSaga(action: any) {
4545
4646function * logoutSaga ( ) {
4747 if ( typeof window !== 'undefined' ) {
48- window . localStorage . removeItem ( 'credentials' ) ;
48+ clearCredentials ( ) ;
4949 }
5050 yield all ( [ put ( resetData ( ) ) , put ( deleteState ( 'auth' ) ) , put ( deleteState ( 'credentials' ) ) ] ) ;
5151}
@@ -56,8 +56,12 @@ export default function* saga() {
5656 // init
5757 const location = yield select ( state => state . get ( 'router' ) . location ) ;
5858 const credentials = yield select ( state => state . getIn ( [ 'data' , 'credentials' ] ) ) ;
59- if ( credentials && ! / ^ \/ l o g i n / g. test ( location . pathname ) ) {
60- yield put ( login ( { credentials : credentials . toJS ( ) } ) ) ;
59+ const auth = yield select ( state => state . getIn ( [ 'data' , 'auth' ] ) ) ;
60+
61+ if ( credentials && auth && ! / ^ \/ l i s t / g. test ( location . pathname ) ) {
62+ yield put ( push ( '/list' ) ) ;
63+ } else if ( ( ! credentials || ! auth ) && ! / ^ \/ l o g i n / g. test ( location . pathname ) ) {
64+ yield put ( push ( '/login' ) ) ;
6165 }
6266
6367 yield takeLatest ( ( action : any ) => action . type === 'LOGOUT' , logoutSaga ) ;
0 commit comments