From 1ac5d077613f3c4d3adda0a712888c41e1fea0d2 Mon Sep 17 00:00:00 2001 From: Diego Mello Date: Mon, 13 Nov 2017 11:19:24 -0200 Subject: [PATCH] set token on app resume (#66) --- app/actions/actionsTypes.js | 1 + app/actions/login.js | 7 +++++++ app/reducers/login.js | 5 +++++ app/sagas/init.js | 9 ++++++++- 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/app/actions/actionsTypes.js b/app/actions/actionsTypes.js index 72bc1ffbb..7e937e97a 100644 --- a/app/actions/actionsTypes.js +++ b/app/actions/actionsTypes.js @@ -12,6 +12,7 @@ function createRequestTypes(base, types = defaultTypes) { export const LOGIN = createRequestTypes('LOGIN', [ ...defaultTypes, 'SET_TOKEN', + 'RESTORE_TOKEN', 'SUBMIT', 'REGISTER_SUBMIT', 'REGISTER_REQUEST', diff --git a/app/actions/login.js b/app/actions/login.js index 4a67acf84..f8b12eeb0 100644 --- a/app/actions/login.js +++ b/app/actions/login.js @@ -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 diff --git a/app/reducers/login.js b/app/reducers/login.js index fc0d94227..4964ccba3 100644 --- a/app/reducers/login.js +++ b/app/reducers/login.js @@ -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, diff --git a/app/sagas/init.js b/app/sagas/init.js index cb53af7de..c86b5caad 100644 --- a/app/sagas/init.js +++ b/app/sagas/init.js @@ -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); }