Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. handleLoginSubmit = this.handleLoginSubmit.bind(this);
  2. handleLoginSubmit(e, user) {
  3. e.preventDefault();
  4. if (R.isNil(user.email)) return;
  5.  
  6. // Log user in via Firebase
  7. firebase.auth().signInWithEmailAndPassword(user.email, user.password)
  8. .catch((err) => {
  9. if (err.code === 'auth/user-not-found') {
  10. console.log('need to create user')
  11. return this.createUser(user.email, user.password);
  12. } else {
  13. console.log('Incorrect email or password, please try again')
  14.  
  15. this.setState({
  16. errors: true,
  17. errorMsg: 'Incorrect email or password, please try again'
  18. }, function () {
  19. console.log('>>> this.state', this.state);
  20. });
  21. }
  22.  
  23. console.log('Completed')
  24. })
  25. }
  26.  
  27. render() {
  28. return (
  29. <main>
  30. { this.state.errors ? <Notification/> : null }
  31. <Login handleLoginSubmit={ this.handleLoginSubmit }
  32. email={ this.state.email }
  33. password={ this.state.password } />
  34. </main>
  35. )
  36. }
  37.  
  38. import React, { Component } from 'react'
  39. import { connect } from 'react-redux'
  40. import { withRouter } from 'react-router-dom'
  41. import * as R from 'ramda'
  42. import * as firebase from 'firebase'
  43.  
  44. import { Login } from '../../components'
  45. import { Notification } from '../../components'
  46.  
  47. export class LoginX extends Component {
  48. constructor(props) {
  49. super(props);
  50. this.state = {
  51. email: '',
  52. password: '',
  53. errors: false,
  54. errorMsg: ''
  55. }
  56. }
  57.  
  58. componentDidMount() {
  59. firebase.auth().onAuthStateChanged((user) => {
  60. this.checkAuth();
  61. })
  62. }
  63.  
  64. checkAuth() {
  65. const user = firebase.auth().currentUser;
  66.  
  67. // If there is a user and they are either email verified or are exempt we need to redirect to viewer.
  68. if (!user) {
  69. return
  70. }
  71. else {
  72. if (!user.emailVerified) {
  73. // User has signed up, redirect them to verification
  74. this.props.history.push('/verification');
  75. return
  76. }
  77. }
  78.  
  79. // User does not need to be authenticated.
  80. this.props.history.push('/dashboard');
  81. }
  82.  
  83. handleLoginSubmit = this.handleLoginSubmit.bind(this);
  84. handleLoginSubmit(e, user) {
  85. e.preventDefault();
  86. if (R.isNil(user.email)) return;
  87.  
  88. // Log user in via Firebase
  89. firebase.auth().signInWithEmailAndPassword(user.email, user.password)
  90. .catch((err) => {
  91. if (err.code === 'auth/user-not-found') {
  92. console.log('need to create user')
  93. return this.createUser(user.email, user.password);
  94. } else {
  95. console.log('Incorrect email or password, please try again')
  96. this.setState({
  97. errors: true,
  98. errorMsg: 'Incorrect email or password, please try again'
  99. }, function () {
  100. console.log('>>> this.state', this.state);
  101. });
  102. }
  103.  
  104. console.log('Completed')
  105. })
  106. }
  107.  
  108. createUser(user, pass) {
  109. firebase.auth().createUserWithEmailAndPassword(user, pass)
  110. .then((user) => {
  111. console.log('verification email sent')
  112. // user.sendEmailVerification()
  113. })
  114. .catch((err) => {
  115. console.log(err)
  116. // this.setState({inProgress: false})
  117. switch (err.code) {
  118. case "auth/email-already-in-use": {
  119. console.log('Account already exists, please log in')
  120. // this.setState({errorMsg: "Account already exists, please log in"});
  121. break;
  122. }
  123. case "auth/invalid-email": {
  124. console.log('Invalid email address format (domain is automatically included)')
  125. // this.setState({errorMsg: "Invalid email address format (domain is automatically included)"});
  126. break;
  127. }
  128. case "auth/operation-not-allowed": {
  129. console.log('Login system in unavailable, please contact the system administrator')
  130. // this.setState({errorMsg: "Login system in unavailable, please contact the system administrator"});
  131. break;
  132. }
  133. }
  134. })
  135. }
  136.  
  137. render() {
  138. return (
  139. <main>
  140. { this.state.errors ? <Notification/> : null }
  141. <Login handleLoginSubmit={ this.handleLoginSubmit }
  142. email={ this.state.email }
  143. password={ this.state.password } />
  144. </main>
  145. )
  146. }
  147. }
  148.  
  149. const mapStateToProps = (state) => {
  150. return {
  151. state
  152. }
  153. }
  154.  
  155. const LoginContainer = LoginX;
  156. export default connect(mapStateToProps)(withRouter(LoginContainer))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement