Skip to content

Commit c2db231

Browse files
committed
completed user info persistence
1 parent ebb9490 commit c2db231

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

App.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { View, Text, AsyncStorage } from "react-native";
44
import { compose, applyMiddleware, createStore } from "redux";
55
import logger from "redux-logger";
66
import { persistStore, autoRehydrate } from "redux-persist";
7-
7+
import { initializeAuth } from "./src/actions/authActions";
88
import AppReducer from "./src/reducers";
99
import AppWithNavigationState from "./src/navigators/AppNavigator";
1010

@@ -21,7 +21,10 @@ export default class App extends React.Component {
2121
}
2222

2323
componentWillMount() {
24-
persistStore(store, { storage: AsyncStorage }, () => {
24+
persistStore(store, { storage: AsyncStorage }, async () => {
25+
const user = store.getState().auth.user;
26+
const initializingAction = await initializeAuth(user);
27+
store.dispatch(initializingAction);
2528
this.setState({ rehydrated: true });
2629
});
2730
}

src/actions/authActions.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,31 @@ async function login() {
3030
}
3131
}
3232

33+
async function initializeAuth(user) {
34+
// Case 1: No prior logged in user
35+
if (!user) {
36+
return logoutAction();
37+
}
38+
const response = await fetch(
39+
`https://graph.facebook.com/debug_token?input_token=${user.token}&access_token=${user.token}`
40+
);
41+
tokenInfo = await response.json();
42+
43+
// Case 2: Prior user data exists, but is outdated
44+
if (!tokenInfo || !tokenInfo.data || !tokenInfo.data.is_valid) {
45+
return logoutAction();
46+
}
47+
// Case 3: Prior user data exists, and is valid
48+
return loginAction(user);
49+
}
50+
3351
function logout() {
3452
return logoutAction();
3553
}
3654

3755
module.exports = {
3856
login,
3957
loginAction,
40-
logout
58+
logout,
59+
initializeAuth
4160
};

0 commit comments

Comments
 (0)