[8ca35dc] | 1 | import React from "react";
|
---|
| 2 | import axios from "axios";
|
---|
[24819a8] | 3 | import WelcomeContent from "./WelcomeContent";
|
---|
| 4 | import AuthContent from "./AuthContent";
|
---|
[5a9c93b] | 5 | import LoginForm from "./LoginForm";
|
---|
[8ca35dc] | 6 | import AuthForm from "./AuthForm";
|
---|
| 7 | import Buttons from './Buttons';
|
---|
| 8 | import restaurants from "./Restaurants";
|
---|
[24819a8] | 9 |
|
---|
[8ca35dc] | 10 | class AppContent extends React.Component {
|
---|
[5a9c93b] | 11 | constructor(props) {
|
---|
| 12 | super(props);
|
---|
| 13 | this.state = {
|
---|
[8ca35dc] | 14 | componentToShow: "welcome",
|
---|
| 15 | isAuthenticated: false,
|
---|
| 16 | user: null,
|
---|
| 17 | loading: false // Add loading state
|
---|
[5a9c93b] | 18 | };
|
---|
[8ca35dc] | 19 | }
|
---|
| 20 |
|
---|
| 21 | componentDidMount() {
|
---|
| 22 | const token = localStorage.getItem('token');
|
---|
| 23 | console.log(token);
|
---|
| 24 | if (token) {
|
---|
| 25 | this.setAuthToken(token);
|
---|
| 26 | this.setState({ componentToShow: "restaurants", isAuthenticated: true });
|
---|
| 27 | }
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | fetchUserDetails = (token) => {
|
---|
| 31 | console.log("Fetch");
|
---|
| 32 | axios.get("/api/user")
|
---|
| 33 | .then((response) => {
|
---|
| 34 | this.setState({ user: response.data, componentToShow: "restaurants", isAuthenticated: true });
|
---|
| 35 | })
|
---|
| 36 | .catch((error) => {
|
---|
| 37 | console.error("Failed to fetch user details:", error);
|
---|
| 38 | this.logout();
|
---|
| 39 | });
|
---|
[5a9c93b] | 40 | };
|
---|
| 41 |
|
---|
| 42 | login = () => {
|
---|
[8ca35dc] | 43 | this.setState({ componentToShow: "login" });
|
---|
[5a9c93b] | 44 | }
|
---|
| 45 |
|
---|
| 46 | logout = () => {
|
---|
[8ca35dc] | 47 | localStorage.removeItem('token');
|
---|
| 48 | this.setAuthToken(null);
|
---|
| 49 | this.setState({ componentToShow: "welcome", isAuthenticated: false });
|
---|
[5a9c93b] | 50 | }
|
---|
| 51 |
|
---|
| 52 | onLogin = (e, email, password) => {
|
---|
| 53 | e.preventDefault();
|
---|
[8ca35dc] | 54 | // After successful login, save the token
|
---|
| 55 | axios.post("/api/login", { email, password })
|
---|
| 56 | .then((response) => {
|
---|
| 57 | const token = response.data.token;
|
---|
| 58 | localStorage.setItem('token', token); // Save the token
|
---|
| 59 | console.log(token);
|
---|
| 60 | this.setAuthToken(token); // Set the token for future requests
|
---|
| 61 | this.setState({ componentToShow: "restaurants", isAuthenticated: true });
|
---|
| 62 | })
|
---|
| 63 | .catch((error) => {
|
---|
| 64 | console.error("Login failed:", error);
|
---|
| 65 | this.setState({ componentToShow: "welcome" });
|
---|
| 66 | });
|
---|
| 67 |
|
---|
[5a9c93b] | 68 | };
|
---|
| 69 |
|
---|
| 70 | onRegister = (e, firstName, lastName, email, password) => {
|
---|
| 71 | e.preventDefault();
|
---|
[8ca35dc] | 72 | axios.post("/api/register", { firstName, lastName, email, password })
|
---|
| 73 | .then((response) => {
|
---|
| 74 | const token = response.data.token;
|
---|
| 75 | localStorage.setItem('token', token);
|
---|
| 76 | console.log(token);
|
---|
| 77 | this.setAuthToken(token);
|
---|
| 78 | this.fetchUserDetails(token);
|
---|
| 79 | })
|
---|
| 80 | .catch((error) => {
|
---|
| 81 | this.setState({ componentToShow: "welcome" });
|
---|
| 82 | console.error(error);
|
---|
| 83 | });
|
---|
| 84 | };
|
---|
| 85 |
|
---|
| 86 | setAuthToken = (token) => {
|
---|
| 87 | if (token) {
|
---|
| 88 | axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
---|
| 89 | } else {
|
---|
| 90 | delete axios.defaults.headers.common["Authorization"];
|
---|
| 91 | }
|
---|
[5a9c93b] | 92 | };
|
---|
| 93 |
|
---|
[24819a8] | 94 | render() {
|
---|
| 95 | return (
|
---|
| 96 | <div>
|
---|
[8ca35dc] | 97 | <Buttons login={this.login} logout={this.logout} />
|
---|
| 98 | {this.state.componentToShow === "welcome" && <WelcomeContent />}
|
---|
| 99 | {this.state.componentToShow === "restaurants" && <AuthContent />}
|
---|
| 100 | {this.state.componentToShow === "login" && <LoginForm onLogin={this.onLogin} onRegister={this.onRegister} />}
|
---|
| 101 | {this.state.loading && <div>Loading...</div>} {/* Show loading state */}
|
---|
[24819a8] | 102 | </div>
|
---|
[8ca35dc] | 103 | );
|
---|
[24819a8] | 104 | }
|
---|
[5a9c93b] | 105 | }
|
---|
[8ca35dc] | 106 |
|
---|
| 107 |
|
---|
| 108 | export default AppContent;
|
---|