|
1 | | -import React from 'react'; |
2 | | -import './App.css'; |
3 | | -import BlogContainer from './components/BlogContainer'; |
4 | | -import LoginForm from './components/LoginForm'; |
5 | | -import axios from 'axios'; |
| 1 | +import React from "react"; |
| 2 | +import "./App.css"; |
| 3 | +import BlogContainer from "./components/BlogContainer"; |
| 4 | +import LoginForm from "./components/LoginForm"; |
| 5 | +import axios from "axios"; |
| 6 | +import HeadingBanner from "./components/HeadingBanner"; |
| 7 | +import Footer from "./components/Footer"; |
6 | 8 | class App extends React.Component { |
7 | | - state = { |
8 | | - name:"", |
9 | | - email:"", |
10 | | - password:"", |
11 | | - password2:"", |
12 | | - address:"", |
13 | | - route:"login" |
14 | | - } |
15 | | - onChange = (e) => { |
16 | | - this.setState({ |
17 | | - [e.target.name]:e.target.value |
18 | | - }); |
19 | | - }; |
| 9 | + state = { |
| 10 | + name: "", |
| 11 | + email: "", |
| 12 | + password: "", |
| 13 | + password2: "", |
| 14 | + address: "", |
| 15 | + route: "login", |
| 16 | + }; |
| 17 | + onChange = (e) => { |
| 18 | + this.setState({ |
| 19 | + [e.target.name]: e.target.value, |
| 20 | + }); |
| 21 | + }; |
20 | 22 |
|
21 | | - authenticate = (login) => { |
22 | | - const user = { |
23 | | - email:this.state.email, |
24 | | - password:this.state.password |
25 | | - }; |
26 | | - if(login){ |
27 | | - console.log(this.state.email,this.state.password) |
28 | | - // if(this.state.email==="admin" && this.state.password==="admin") |
29 | | - // { |
30 | | - // this.setState({route:"admin"}) |
31 | | - // } |
32 | | - // else |
33 | | - // { |
34 | | - // alert("Wrong login credentials! Please try again"); |
35 | | - // } |
36 | | - |
37 | | - axios |
38 | | - .post(`/api/authenticate`,user) |
39 | | - .then(res => { |
40 | | - if (res.status === 200) { |
41 | | - this.setState({route:"blog"}) |
42 | | - } else { |
43 | | - const error = new Error(res.error); |
44 | | - throw error; |
45 | | - } |
46 | | - }) |
47 | | - .catch(err => { |
48 | | - console.error(err); |
49 | | - alert('Error logging in please try again/register'); |
50 | | - }); |
51 | | - |
52 | | - } |
53 | | - else{ |
54 | | - if(this.state.password!==this.state.password2){ |
55 | | - alert("Your passwords do not match, please make sure both passwords match.") |
56 | | - return |
57 | | - } |
58 | | - axios |
59 | | - .post(`/api/register`,user) |
60 | | - .then(res => { |
| 23 | + authenticate = (login) => { |
| 24 | + const user = { |
| 25 | + name: this.state.name, |
| 26 | + email: this.state.email, |
| 27 | + password: this.state.password, |
| 28 | + }; |
| 29 | + if (login) { |
| 30 | + axios |
| 31 | + .post(`http://localhost:5001/api/authenticate`, user) |
| 32 | + .then((res) => { |
61 | 33 | if (res.status === 200) { |
62 | | - alert("Registered Successfully! Welcome to Blog Planet ") |
63 | | - this.setState({route:"blog"}) |
| 34 | + this.setState({ route: "blog", name: res.data.name }); |
64 | 35 | } else { |
65 | 36 | const error = new Error(res.error); |
66 | 37 | throw error; |
67 | 38 | } |
68 | 39 | }) |
69 | | - .catch(err => { |
| 40 | + .catch((err) => { |
70 | 41 | console.error(err); |
71 | | - alert('Error logging in please try again with another email-id'); |
| 42 | + alert( |
| 43 | + "Failed to login! Please try again with correct credentials or register" |
| 44 | + ); |
| 45 | + }); |
| 46 | + } else { |
| 47 | + if (this.state.password !== this.state.password2) { |
| 48 | + alert( |
| 49 | + "Your passwords do not match, please make sure both passwords match." |
| 50 | + ); |
| 51 | + return; |
| 52 | + } |
| 53 | + axios |
| 54 | + .post(`http://localhost:5001/api/register`, user) |
| 55 | + .then((res) => { |
| 56 | + if (res.status === 200) { |
| 57 | + alert("Registered Successfully! Welcome to Blog Planet "); |
| 58 | + this.setState({ route: "blog",name: res.data.name }); |
| 59 | + } else { |
| 60 | + const error = new Error(res.error); |
| 61 | + throw error; |
| 62 | + } |
| 63 | + }) |
| 64 | + .catch((err) => { |
| 65 | + console.error(err); |
| 66 | + alert("Failed to register! Please try again with another email-id"); |
72 | 67 | }); |
73 | | - } |
74 | 68 | } |
75 | | - render(){ |
| 69 | + }; |
| 70 | + |
| 71 | + render() { |
76 | 72 | return ( |
77 | | - <div className="App"> |
78 | | - { |
79 | | - this.state.route==="login"? |
80 | | - <LoginForm onChange={this.onChange} onSubmit={this.onSubmit} authenticate={this.authenticate}/>:<BlogContainer name={this.state.name} email={this.state.email}/> |
81 | | - } |
82 | | - </div> |
83 | | - ); |
84 | | - } |
| 73 | + <div className="App"> |
| 74 | + {this.state.route === "login" ? ( |
| 75 | + <LoginForm |
| 76 | + onChange={this.onChange} |
| 77 | + authenticate={this.authenticate} |
| 78 | + /> |
| 79 | + ) : ( |
| 80 | + <React.Fragment> |
| 81 | + <HeadingBanner /> |
| 82 | + <BlogContainer name={this.state.name} email={this.state.email} /> |
| 83 | + <Footer /> |
| 84 | + </React.Fragment> |
| 85 | + )} |
| 86 | + </div> |
| 87 | + ); |
| 88 | + } |
85 | 89 | } |
86 | 90 |
|
87 | 91 | export default App; |
0 commit comments