set token on app resume (#66)
This commit is contained in:
parent
7027656f4b
commit
1ac5d07761
|
@ -12,6 +12,7 @@ function createRequestTypes(base, types = defaultTypes) {
|
|||
export const LOGIN = createRequestTypes('LOGIN', [
|
||||
...defaultTypes,
|
||||
'SET_TOKEN',
|
||||
'RESTORE_TOKEN',
|
||||
'SUBMIT',
|
||||
'REGISTER_SUBMIT',
|
||||
'REGISTER_REQUEST',
|
||||
|
|
|
@ -76,6 +76,13 @@ export function setToken(user = {}) {
|
|||
};
|
||||
}
|
||||
|
||||
export function restoreToken(token) {
|
||||
return {
|
||||
type: types.LOGIN.RESTORE_TOKEN,
|
||||
token
|
||||
};
|
||||
}
|
||||
|
||||
export function logout() {
|
||||
return {
|
||||
type: types.LOGOUT
|
||||
|
|
|
@ -41,6 +41,11 @@ export default function login(state = initialState, action) {
|
|||
token: action.token,
|
||||
user: action.user
|
||||
};
|
||||
case types.LOGIN.RESTORE_TOKEN:
|
||||
return {
|
||||
...state,
|
||||
token: action.token
|
||||
};
|
||||
case types.LOGIN.REGISTER_SUBMIT:
|
||||
return {
|
||||
...state,
|
||||
|
|
|
@ -2,16 +2,23 @@ import { AsyncStorage } from 'react-native';
|
|||
import { call, put, take } from 'redux-saga/effects';
|
||||
import * as actions from '../actions';
|
||||
import { setServer } from '../actions/server';
|
||||
import { restoreToken } from '../actions/login';
|
||||
import { APP } from '../actions/actionsTypes';
|
||||
|
||||
const restore = function* restore() {
|
||||
try {
|
||||
yield take(APP.INIT);
|
||||
const token = yield call([AsyncStorage, 'getItem'], 'reactnativemeteor_usertoken');
|
||||
if (token) {
|
||||
yield put(restoreToken(token));
|
||||
}
|
||||
|
||||
const currentServer = yield call([AsyncStorage, 'getItem'], 'currentServer');
|
||||
yield put(actions.appReady({}));
|
||||
if (currentServer) {
|
||||
yield put(setServer(currentServer));
|
||||
}
|
||||
|
||||
yield put(actions.appReady({}));
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue