set token on app resume (#66)

This commit is contained in:
Diego Mello 2017-11-13 11:19:24 -02:00 committed by Guilherme Gazzo
parent 7027656f4b
commit 1ac5d07761
4 changed files with 21 additions and 1 deletions

View File

@ -12,6 +12,7 @@ function createRequestTypes(base, types = defaultTypes) {
export const LOGIN = createRequestTypes('LOGIN', [
...defaultTypes,
'SET_TOKEN',
'RESTORE_TOKEN',
'SUBMIT',
'REGISTER_SUBMIT',
'REGISTER_REQUEST',

View File

@ -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

View File

@ -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,

View File

@ -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);
}