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', [
|
export const LOGIN = createRequestTypes('LOGIN', [
|
||||||
...defaultTypes,
|
...defaultTypes,
|
||||||
'SET_TOKEN',
|
'SET_TOKEN',
|
||||||
|
'RESTORE_TOKEN',
|
||||||
'SUBMIT',
|
'SUBMIT',
|
||||||
'REGISTER_SUBMIT',
|
'REGISTER_SUBMIT',
|
||||||
'REGISTER_REQUEST',
|
'REGISTER_REQUEST',
|
||||||
|
|
|
@ -76,6 +76,13 @@ export function setToken(user = {}) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function restoreToken(token) {
|
||||||
|
return {
|
||||||
|
type: types.LOGIN.RESTORE_TOKEN,
|
||||||
|
token
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function logout() {
|
export function logout() {
|
||||||
return {
|
return {
|
||||||
type: types.LOGOUT
|
type: types.LOGOUT
|
||||||
|
|
|
@ -41,6 +41,11 @@ export default function login(state = initialState, action) {
|
||||||
token: action.token,
|
token: action.token,
|
||||||
user: action.user
|
user: action.user
|
||||||
};
|
};
|
||||||
|
case types.LOGIN.RESTORE_TOKEN:
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
token: action.token
|
||||||
|
};
|
||||||
case types.LOGIN.REGISTER_SUBMIT:
|
case types.LOGIN.REGISTER_SUBMIT:
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
|
|
|
@ -2,16 +2,23 @@ import { AsyncStorage } from 'react-native';
|
||||||
import { call, put, take } from 'redux-saga/effects';
|
import { call, put, take } from 'redux-saga/effects';
|
||||||
import * as actions from '../actions';
|
import * as actions from '../actions';
|
||||||
import { setServer } from '../actions/server';
|
import { setServer } from '../actions/server';
|
||||||
|
import { restoreToken } from '../actions/login';
|
||||||
import { APP } from '../actions/actionsTypes';
|
import { APP } from '../actions/actionsTypes';
|
||||||
|
|
||||||
const restore = function* restore() {
|
const restore = function* restore() {
|
||||||
try {
|
try {
|
||||||
yield take(APP.INIT);
|
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');
|
const currentServer = yield call([AsyncStorage, 'getItem'], 'currentServer');
|
||||||
yield put(actions.appReady({}));
|
|
||||||
if (currentServer) {
|
if (currentServer) {
|
||||||
yield put(setServer(currentServer));
|
yield put(setServer(currentServer));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
yield put(actions.appReady({}));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue