Skip to content

Commit 0c372b2

Browse files
Merge pull request #1 from naji247/rehydrate
Rehydrate
2 parents 1cdbe2b + 04ea659 commit 0c372b2

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

App.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
11
import React from "react";
22
import { Provider } from "react-redux";
3-
import { View, Text } from "react-native";
4-
import { applyMiddleware, createStore } from "redux";
3+
import { View, Text, AsyncStorage } from "react-native";
4+
import { compose, applyMiddleware, createStore } from "redux";
55
import logger from "redux-logger";
6-
6+
import { persistStore, autoRehydrate } from "redux-persist";
7+
import { initializeAuth } from "./src/actions/authActions";
78
import AppReducer from "./src/reducers";
89
import AppWithNavigationState from "./src/navigators/AppNavigator";
910

11+
const store = createStore(
12+
AppReducer,
13+
undefined,
14+
compose(applyMiddleware(logger), autoRehydrate())
15+
);
16+
1017
export default class App extends React.Component {
11-
store = createStore(AppReducer, applyMiddleware(logger));
18+
constructor() {
19+
super();
20+
this.state = { rehydrated: false };
21+
}
22+
23+
componentWillMount() {
24+
persistStore(store, { storage: AsyncStorage }, async () => {
25+
const user = store.getState().auth.user;
26+
const initializingAction = await initializeAuth(user);
27+
store.dispatch(initializingAction);
28+
this.setState({ rehydrated: true });
29+
});
30+
}
31+
1232
render() {
33+
if (!this.state.rehydrated) {
34+
return <Text>Loading...</Text>;
35+
}
1336
return (
14-
<Provider store={this.store}>
37+
<Provider store={store}>
1538
<AppWithNavigationState />
1639
</Provider>
1740
);

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"react": "16.0.0-alpha.6",
2424
"react-native": "^0.44.0",
2525
"react-navigation": "^1.0.0-beta.11",
26-
"redux-logger": "^3.0.6"
26+
"redux-logger": "^3.0.6",
27+
"redux-persist": "^4.8.1"
2728
}
2829
}

src/actions/authActions.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { LOGIN_SUCCESS, LOGOUT_SUCCESS } from "./actionTypes";
22
import Expo from "expo";
33
const getFBToken = Expo.Facebook.logInWithReadPermissionsAsync;
4+
const fbURL = "https://graph.facebook.com/";
45

56
const loginAction = user => {
67
return {
@@ -21,20 +22,38 @@ async function login() {
2122
});
2223
if (type === "success") {
2324
// Get the user's name using Facebook's Graph API
24-
const response = await fetch(
25-
`https://graph.facebook.com/me?access_token=${token}`
26-
);
25+
const response = await fetch(`${fbURL}me?access_token=${token}`);
2726
user = await response.json();
27+
user.token = token;
2828
return loginAction(user);
2929
}
3030
}
3131

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

3654
module.exports = {
3755
login,
3856
loginAction,
39-
logout
57+
logout,
58+
initializeAuth
4059
};

0 commit comments

Comments
 (0)