Skip to content

Instantly share code, notes, and snippets.

@rileybracken
Created February 8, 2016 23:14
Show Gist options
  • Save rileybracken/96b42be71520a6b4c342 to your computer and use it in GitHub Desktop.
Save rileybracken/96b42be71520a6b4c342 to your computer and use it in GitHub Desktop.
Example of how I do logged in user session
class App extends Component {
componentDidMount() {
this._loadSession().done();
}
componentWillReceiveProps(nextProps) {
if (!this.props.userState.loggedIn && nextProps.userState.currentUser.sessionToken) {
this._setSession(nextProps.userState.currentUser.sessionToken).done();
}
}
async _loadSession() {
try {
let value = await AsyncStorage.getItem(SESSION_KEY);
let { dispatch, userState } = this.props;
if (value !== null) {
dispatch(userActions.logInWithSession(value));
console.log(value);
}
}
// I don't think I care if this is successfull or errors,
// because on initial load if it isn't there it isn't there
// and the user has to log back in, but maybe I am crazy.
finally {}
}
async _setSession(sessionValue) {
try {
await AsyncStorage.setItem(SESSION_KEY, sessionValue);
} catch(error) {
// TODO: Throw a proper error probably via the error messaging
// if you cannot get the session.
console.log('error setting session: ', error)
}
}
render() {
return <View></View>;
}
};
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment