Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- handleLoginSubmit = this.handleLoginSubmit.bind(this);
- handleLoginSubmit(e, user) {
- e.preventDefault();
- if (R.isNil(user.email)) return;
- // Log user in via Firebase
- firebase.auth().signInWithEmailAndPassword(user.email, user.password)
- .catch((err) => {
- if (err.code === 'auth/user-not-found') {
- console.log('need to create user')
- return this.createUser(user.email, user.password);
- } else {
- console.log('Incorrect email or password, please try again')
- this.setState({
- errors: true,
- errorMsg: 'Incorrect email or password, please try again'
- }, function () {
- console.log('>>> this.state', this.state);
- });
- }
- console.log('Completed')
- })
- }
- render() {
- return (
- <main>
- { this.state.errors ? <Notification/> : null }
- <Login handleLoginSubmit={ this.handleLoginSubmit }
- email={ this.state.email }
- password={ this.state.password } />
- </main>
- )
- }
- import React, { Component } from 'react'
- import { connect } from 'react-redux'
- import { withRouter } from 'react-router-dom'
- import * as R from 'ramda'
- import * as firebase from 'firebase'
- import { Login } from '../../components'
- import { Notification } from '../../components'
- export class LoginX extends Component {
- constructor(props) {
- super(props);
- this.state = {
- email: '',
- password: '',
- errors: false,
- errorMsg: ''
- }
- }
- componentDidMount() {
- firebase.auth().onAuthStateChanged((user) => {
- this.checkAuth();
- })
- }
- checkAuth() {
- const user = firebase.auth().currentUser;
- // If there is a user and they are either email verified or are exempt we need to redirect to viewer.
- if (!user) {
- return
- }
- else {
- if (!user.emailVerified) {
- // User has signed up, redirect them to verification
- this.props.history.push('/verification');
- return
- }
- }
- // User does not need to be authenticated.
- this.props.history.push('/dashboard');
- }
- handleLoginSubmit = this.handleLoginSubmit.bind(this);
- handleLoginSubmit(e, user) {
- e.preventDefault();
- if (R.isNil(user.email)) return;
- // Log user in via Firebase
- firebase.auth().signInWithEmailAndPassword(user.email, user.password)
- .catch((err) => {
- if (err.code === 'auth/user-not-found') {
- console.log('need to create user')
- return this.createUser(user.email, user.password);
- } else {
- console.log('Incorrect email or password, please try again')
- this.setState({
- errors: true,
- errorMsg: 'Incorrect email or password, please try again'
- }, function () {
- console.log('>>> this.state', this.state);
- });
- }
- console.log('Completed')
- })
- }
- createUser(user, pass) {
- firebase.auth().createUserWithEmailAndPassword(user, pass)
- .then((user) => {
- console.log('verification email sent')
- // user.sendEmailVerification()
- })
- .catch((err) => {
- console.log(err)
- // this.setState({inProgress: false})
- switch (err.code) {
- case "auth/email-already-in-use": {
- console.log('Account already exists, please log in')
- // this.setState({errorMsg: "Account already exists, please log in"});
- break;
- }
- case "auth/invalid-email": {
- console.log('Invalid email address format (domain is automatically included)')
- // this.setState({errorMsg: "Invalid email address format (domain is automatically included)"});
- break;
- }
- case "auth/operation-not-allowed": {
- console.log('Login system in unavailable, please contact the system administrator')
- // this.setState({errorMsg: "Login system in unavailable, please contact the system administrator"});
- break;
- }
- }
- })
- }
- render() {
- return (
- <main>
- { this.state.errors ? <Notification/> : null }
- <Login handleLoginSubmit={ this.handleLoginSubmit }
- email={ this.state.email }
- password={ this.state.password } />
- </main>
- )
- }
- }
- const mapStateToProps = (state) => {
- return {
- state
- }
- }
- const LoginContainer = LoginX;
- export default connect(mapStateToProps)(withRouter(LoginContainer))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement