Skip to content

Instantly share code, notes, and snippets.

@mauricedb
Created November 11, 2016 17:20
Show Gist options
  • Save mauricedb/491c33efd678a80628464a7162cab7d7 to your computer and use it in GitHub Desktop.
Save mauricedb/491c33efd678a80628464a7162cab7d7 to your computer and use it in GitHub Desktop.
Remember me
import React, { Component } from 'react';
import AppPresentation from './app-presentation';
class AppContainer extends Component {
constructor(props) {
super(props);
this.state = { user: null };
this.loginAsUser = this.loginAsUser.bind(this);
}
componentWillMount() {
if (localStorage.user) {
try {
const user = JSON.parse(localStorage.user);
this.setState({ user });
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
}
}
}
loginAsUser(user) {
if (user.rememberMe) {
localStorage.user = JSON.stringify(user);
}
this.setState({ user });
}
render() {
const { user } = this.state;
return (
<AppPresentation
user={user}
loginAsUser={this.loginAsUser}
/>
);
}
}
export default AppContainer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment