diff --git a/client/src/App.js b/client/src/App.js index 30bf1430a..999af5f38 100644 --- a/client/src/App.js +++ b/client/src/App.js @@ -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"; @@ -51,24 +51,28 @@ 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, + }); + } }); } } @@ -76,23 +80,23 @@ class App extends Component { 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"); }); @@ -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); }); }; @@ -134,37 +138,43 @@ class App extends Component { } + render={(routerProps) => ( + + )} > } + render={(routerProps) => ( + + )} > - + } + render={(routerProps) => ( + + )} > ( + render={(routerProps) => ( )} > ( + render={(routerProps) => ( )} > ( + render={(routerProps) => ( ( + render={(routerProps) => ( )} >