Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated cached token issue #510

Merged
merged 1 commit into from
Apr 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 41 additions & 31 deletions client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
faAngleLeft,
faAngleRight,
faCommentDots,
faEdit
faEdit,
} from "@fortawesome/free-solid-svg-icons";
import { fab } from "@fortawesome/free-brands-svg-icons";
import { fas } from "@fortawesome/free-solid-svg-icons";
Expand Down Expand Up @@ -51,48 +51,52 @@ class App extends Component {
localStorage.getItem("token") && localStorage.getItem("token") != undefined ? true : false,
email: "",
first_name: "",
image: ""
image: "",
};
}
componentDidMount() {
if (this.state.logged_in) {
fetch("http://localhost:8000/auth/user", {
headers: {
Authorization: `Token ${localStorage.getItem("token")}`
}
Authorization: `Token ${localStorage.getItem("token")}`,
},
})
.then(res => res.json())
.then(json => {
.then((res) => res.json())
.then((json) => {
console.log(json);
this.setState({
email: json.email,
first_name: json.profile.first_name,
image: json.profile.image
});
if (json.detail == "Invalid token.") {
this.handle_logout();
} else {
this.setState({
email: json.email,
first_name: json.profile.first_name,
image: json.profile.image,
});
}
});
}
}
handle_login = (data, history) => {
fetch("http://localhost:8000/auth/login", {
method: "POST",
headers: {
"Content-Type": "application/json"
"Content-Type": "application/json",
},
body: JSON.stringify(data)
body: JSON.stringify(data),
})
.then(res => res.json())
.then(json => {
.then((res) => res.json())
.then((json) => {
console.log(json.token);
json.token ? localStorage.setItem("token", json.token) : console.log("no token");
this.setState({
logged_in: json.token != undefined ? true : false,
email: json.user.email,
first_name: json.user.profile.first_name,
image: json.user.profile.image
image: json.user.profile.image,
});
history.push("/home");
})
.catch(err => {
.catch((err) => {
console.log(err);
alert("Please enter valid email and password");
});
Expand All @@ -101,23 +105,23 @@ class App extends Component {
fetch("http://localhost:8000/auth/register", {
method: "POST",
headers: {
"Content-Type": "application/json"
"Content-Type": "application/json",
},
body: JSON.stringify(data)
body: JSON.stringify(data),
})
.then(res => res.json())
.then(json => {
.then((res) => res.json())
.then((json) => {
console.log(json);
json.token ? localStorage.setItem("token", json.token) : console.log("no token");
this.setState({
logged_in: json.token != undefined ? true : false,
email: json.user.email,
first_name: json.user.profile.first_name,
image: json.user.profile.image
image: json.user.profile.image,
});
history.push("/home");
})
.catch(err => {
.catch((err) => {
console.log(err);
});
};
Expand All @@ -134,37 +138,43 @@ class App extends Component {
<Route
path="/hawaii-2020"
exact
render={routerProps => <Hawaii2020 tripName={"Hawaii"} handle_logout={this.handle_logout} {...routerProps} />}
render={(routerProps) => (
<Hawaii2020 tripName={"Hawaii"} handle_logout={this.handle_logout} {...routerProps} />
)}
></Route>
<Route
path="/book-a-trip"
exact
render={routerProps => <BookATripPage handle_logout={this.handle_logout} {...routerProps} />}
render={(routerProps) => (
<BookATripPage handle_logout={this.handle_logout} {...routerProps} />
)}
></Route>

<Route
path="/member-page"
exact
render={routerProps => <MemberPage handle_logout={this.handle_logout} {...routerProps} />}
render={(routerProps) => (
<MemberPage handle_logout={this.handle_logout} {...routerProps} />
)}
></Route>
<Route
path="/coming_soon"
exact
render={routerProps => (
render={(routerProps) => (
<ComingSoonPage handle_logout={this.handle_logout} {...routerProps} {...this.state} />
)}
></Route>
<Route
path="/login"
exact
render={routerProps => (
render={(routerProps) => (
<LoginPage handleLogin={this.handle_login} {...routerProps} {...this.state} />
)}
></Route>
<Route
path="/register"
exact
render={routerProps => (
render={(routerProps) => (
<OnboardingPage
handleSignup={this.handle_signup}
{...routerProps}
Expand All @@ -175,7 +185,7 @@ class App extends Component {
<Route
path="/home"
exact
render={routerProps => (
render={(routerProps) => (
<HomePage handle_logout={this.handle_logout} {...routerProps} {...this.state} />
)}
></Route>
Expand Down