Created
October 6, 2017 21:57
-
-
Save DavidLazic/b87d2489cb857ba530eba070c515f16e to your computer and use it in GitHub Desktop.
React + Firebase - Persistent Auth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import firebase from 'firebase'; | |
import { browserHistory } from 'react-router'; | |
import { routeCodes } from 'routes'; | |
import FormLogin from 'components/form/Form.login'; | |
export default class Login extends Component { | |
constructor (props) { | |
super(props); | |
this.state = { error: {} }; | |
this.onLogin = this.onLogin.bind(this); | |
} | |
reset () { | |
return new Promise(resolve => this.setState({ error: {} }, () => resolve())); | |
} | |
onLogin ({ email, password }) { | |
this.reset() | |
.then(() => firebase.auth().signInWithEmailAndPassword(email, password)) | |
.then(() => browserHistory.push(routeCodes.ADMIN)) | |
.catch(error => this.setState({ error })); | |
} | |
render () { | |
return ( | |
<article> | |
<FormLogin | |
onSubmit={ this.onLogin } | |
error={ this.state.error } /> | |
</article> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment