Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.43 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import fire from './config/Fire';
  3.  
  4. import './App.css';
  5. import { firestore } from 'firebase';
  6.  
  7. class LoginPage extends Component{
  8. constructor(props){
  9. super(props);
  10. this.state = {
  11. isLogin: true,
  12. }
  13. }
  14.  
  15. setLogin = () => {
  16. this.setState({ isLogin: !this.state.isLogin });
  17. }
  18.  
  19. render() {
  20. console.log(this.props);
  21. const { changeUsername } = this.props;
  22. return (
  23. <div>
  24. {this.state.isLogin ?
  25. (<Login changeUsername={changeUsername}
  26. isLogin = {this.state.isLogin}
  27. setLogin={this.setLogin} />)
  28. :
  29. (<CreateAccount setLogin = {this.setLogin}/>)}
  30. </div>
  31. );
  32. }
  33.  
  34.  
  35. }
  36.  
  37.  
  38. class Login extends Component{
  39. constructor(props){
  40. super(props);
  41. this.state = {
  42. username: "",
  43. email: "",
  44. password:"",
  45. };
  46. this.login = this.login.bind(this);
  47. this.handleChange = this.handleChange.bind(this);
  48. this.updateLogin = this.updateLogin.bind(this);
  49. this.handleClick = this.handleClick.bind(this);
  50. }
  51.  
  52. login(e){
  53. e.preventDefault();
  54. if (this.state.email != "" && this.state.password!=""){
  55.  
  56. fire.auth().signInWithEmailAndPassword(this.state.email.trim(), this.state.password).then((u)=>{
  57. let docRef = fire.firestore().collection('users').doc(this.state.email);
  58. let x = this;
  59. docRef.get().then(function(doc){
  60. if (doc.exists){
  61. // x.setState({username: doc.data().username});
  62. // x.state.username = doc.data().username;
  63.  
  64. if (doc.data().username!=""){
  65. x.updateLogin(doc.data().username);
  66. }
  67. console.log("Document data: ", doc.data().username);
  68. }
  69. else{
  70. console.log("no data :(");
  71. }
  72. }).catch(function(error){
  73. console.log("Error getting data...", error);
  74. });
  75.  
  76. }).catch((error) =>{
  77. let errorMessage = error;
  78. if (error == "Error: The password is invalid or the user does not have a password."|| error == "Error: There is no user record corresponding to this identifier. The user may have been deleted."){
  79. errorMessage = "Invalid Username/Password";
  80. }
  81. else if(error == "Error: The email address is badly formatted."){
  82. errorMessage = "Input formatted incorrectly.";
  83. }
  84. document.getElementById('error_message').innerHTML = errorMessage;
  85. });
  86.  
  87. }
  88. else{
  89. document.getElementById('error_message').innerHTML = "Please fill in all the inputs.";
  90. }
  91. }
  92.  
  93. loginGuest = () =>{
  94. this.setState({
  95. username: "username",
  96. })
  97.  
  98. fire.auth().signInWithEmailAndPassword("guest@guest.com", "guestpassword").then((u)=>{
  99. this.updateLoginGuest(); //Change the display name to be guest
  100. }).catch((error) =>{
  101. let errorMessage = error;
  102. if (error == "Error: The password is invalid or the user does not have a password."|| error == "Error: There is no user record corresponding to this identifier. The user may have been deleted."){
  103. errorMessage = "Invalid Username/Password";
  104. }
  105. else if(error == "Error: The email address is badly formatted."){
  106. errorMessage = "Input formatted incorrectly.";
  107. }
  108. document.getElementById('error_message').innerHTML = errorMessage;
  109. });
  110.  
  111. }
  112.  
  113.  
  114.  
  115. handleChange(e){
  116. this.setState({[e.target.name]: e.target.value});
  117. }
  118.  
  119. updateLogin(firebaseUsername){
  120. // this.setState({
  121. // username: firebaseUsername,
  122. // })
  123. console.log("UPDATE LOGIN" + firebaseUsername);
  124. // this.props.changeUsername(this.state.username);
  125. this.props.changeUsername(firebaseUsername);
  126. }
  127.  
  128. updateLoginGuest = () =>{
  129. this.props.changeUsername("guest");
  130. }
  131.  
  132. handleClick (){
  133. console.log(this.props);
  134. this.props.setLogin();
  135. }
  136.  
  137.  
  138. render(){
  139. return(
  140. <div>
  141. <Title/>
  142. <form>
  143. <fieldset>
  144. <legend>Log In:</legend>
  145. {/* <label>Username
  146. <input type = "text" name = "username" id = "username_input" value = {this.state.username} onChange = {this.handleChange}/>
  147. </label>
  148. <br/> */}
  149. <label>Email
  150. <input type = "email" name = "email" id = "email_input" value = {this.state.email} onChange = {this.handleChange}/>
  151. </label>
  152. <br/>
  153. <label htmlFor = "password_input">Password</label>
  154. <input type = "password" name = "password" id = "password_input" value = {this.state.password} onChange = {this.handleChange}/>
  155. <br/>
  156. <p id = "error_message"></p>
  157. <br/>
  158. <a onClick ={this.handleClick}>New? Create an account!</a>
  159. <br/>
  160. <a onClick={this.loginGuest}>Continue as guest</a>
  161. <br/>
  162. <button type = "submit" onClick = {this.login}>Login</button>
  163. {/* <button type = "submit" onClick = {this.signup}>Signup</button> */}
  164. </fieldset>
  165. <img id = "login_pic" src = "https://cdn.vox-cdn.com/uploads/chorus_image/image/50365629/usa-today-9450036.0.jpg"/>
  166. <img id = "login_pic2" src = "https://cdn.vox-cdn.com/uploads/chorus_image/image/50365629/usa-today-9450036.0.jpg"/>
  167.  
  168. </form>
  169. </div>
  170. );
  171.  
  172. }
  173. }
  174.  
  175. class Title extends Component{
  176. render(){
  177. return(
  178. <div>
  179. <h1>Welcome to SwimOverflow!</h1>
  180. <p>A community for all fans of swimming</p>
  181. </div>
  182. );
  183. }
  184. }
  185.  
  186. class CreateAccount extends Component{
  187. constructor(props){
  188. super(props);
  189. this.state = {
  190. username: "",
  191. email: "",
  192. password:"",
  193. password2: "",
  194. };
  195.  
  196. this.handleChange = this.handleChange.bind(this);
  197. this.updateLogin = this.updateLogin.bind(this);
  198. }
  199.  
  200. handleChange(e){
  201. this.setState({[e.target.name]: e.target.value});
  202. }
  203.  
  204. handleClick = () =>{
  205. this.props.setLogin();
  206. }
  207.  
  208. updateLogin(firebaseUsername){
  209. this.state.username = firebaseUsername;
  210. console.log("UPDATE LOGIN" + firebaseUsername);
  211. this.props.props.changeUsername(this.state.username);
  212. }
  213.  
  214. CreateAccount = (e) =>{
  215. e.preventDefault();
  216. if (this.state.password != this.state.password2){
  217. document.getElementById('error_message2').innerHTML = "Passwords do not match.";
  218. }
  219. else{
  220. if (this.state.email != "" && this.state.password!="" && this.state.username!="" && this.state.password2!=""){
  221. fire.auth().createUserWithEmailAndPassword(this.state.email.trim(), this.state.password).then((u)=>{
  222. let x= this;
  223. fire.firestore().collection("users").doc(this.state.email).set({
  224. username: this.state.username,
  225. }).then (function(){
  226. if (x.state.username!=""){
  227. x.updateLogin(x.state.username);
  228. }
  229. console.log("success");
  230. })
  231. .catch(function(e){
  232. console.log(e);
  233. })
  234. })
  235. .catch((error)=>{
  236. let printError = error;
  237. if (error = "Error: The email address is badly formatted."){
  238. alert (this.state.email);
  239. printError = "Input formatted incorrectly.";
  240. }
  241. document.getElementById('error_message2').innerHTML = printError;
  242. })
  243. }
  244. else{
  245. document.getElementById('error_message2').innerHTML = "Please fill in all the inputs.";
  246. }
  247. }
  248. }
  249.  
  250. render(){
  251. return(
  252. <div>
  253. <Title/>
  254. <form>
  255. <fieldset>
  256. <legend>Create Account:</legend>
  257. <label htmlFor = "username_input_create">Username</label>
  258. <input type = "text" id = "username_input_create" name = "username" value = {this.state.username} onChange = {this.handleChange}/>
  259. <br/>
  260. <label>Email
  261. <input type = "email" name = "email" id = "email_input" value = {this.state.email} onChange = {this.handleChange}/>
  262. </label>
  263. <br/>
  264. <label htmlFor = "password_input_create">Password</label>
  265. <input type = "password" id = "password_input_create" name = "password" value = {this.state.password} onChange = {this.handleChange}/>
  266. <br/>
  267. <label htmlFor = "password_input_create2">Confirm Password</label>
  268. <input type = "password" id = "password_input_create2" name = "password2" value = {this.state.password2} onChange = {this.handleChange}/>
  269. <br/>
  270. <p id = "error_message2"></p>
  271. <br/>
  272. <a onClick ={this.handleClick}>Already have an account? Log in!</a>
  273. <br/>
  274. <button type = "submit" onClick = {this.CreateAccount}>Create Account</button>
  275. </fieldset>
  276. </form>
  277.  
  278. <img id = "login_pic" src = "https://cdn.vox-cdn.com/uploads/chorus_image/image/50365629/usa-today-9450036.0.jpg"/>
  279. <img id = "login_pic2" src = "https://cdn.vox-cdn.com/uploads/chorus_image/image/50365629/usa-today-9450036.0.jpg"/>
  280.  
  281. </div>
  282. );
  283.  
  284. }
  285. }
  286.  
  287.  
  288. export default LoginPage;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement