From 0daa8cfa72060506ca7e814aef4a46a405cde674 Mon Sep 17 00:00:00 2001 From: keannu125 Date: Sun, 26 Feb 2023 22:31:19 +0800 Subject: [PATCH] Polished and added more apis --- src/Components/Api/Api.tsx | 46 +++++++++++++++++++--- src/Components/Header/Header.tsx | 8 ++-- src/Components/HomeIcon/HomeIcon.tsx | 42 ++++++++++---------- src/Components/LoginButton/LoginButton.tsx | 6 ++- src/Routes/Login/Login.tsx | 6 ++- 5 files changed, 75 insertions(+), 33 deletions(-) diff --git a/src/Components/Api/Api.tsx b/src/Components/Api/Api.tsx index b7f6f5a..a95e7f9 100644 --- a/src/Components/Api/Api.tsx +++ b/src/Components/Api/Api.tsx @@ -32,9 +32,9 @@ export function UserLogin(user: user) { return axios .post("http://localhost:8000/api/v1/accounts/token/login/", user) .then(async (response) => { - console.log(response.data); - localStorage.setItem("token", response.data); - console.log(await UserInfo()); + localStorage.setItem("token", JSON.stringify(response.data.auth_token)); + console.log("Stored: ", JSON.parse(localStorage.getItem("token") || "")); + StoreUser(); return true; }) .catch((error) => { @@ -43,12 +43,46 @@ export function UserLogin(user: user) { }); } -export function UserInfo() { - const token = localStorage.getItem("token"); +export function StoreUser() { + const token = JSON.parse(localStorage.getItem("token") || ""); return axios .get("http://localhost:8000/api/v1/accounts/users/me/", { headers: { - Authorization: "Token 8b3a393fc7601a5a1f2a831bc795905c05420782", + Authorization: "Token " + token, + }, + }) + .then((response) => { + localStorage.setItem("user", JSON.stringify(response.data)); + }) + .catch((error) => { + console.log("Error in storing user data"); + console.log(error); + }); +} + +export function UserInfo() { + const token = JSON.parse(localStorage.getItem("token") || ""); + return axios + .get("http://localhost:8000/api/v1/accounts/users/me/", { + headers: { + Authorization: "Token " + token, + }, + }) + .then((response) => { + return response.data; + }) + .catch((error) => { + console.log("Error in fetching user data"); + console.log(error); + }); +} + +export function GetUsername() { + const token = JSON.parse(localStorage.getItem("token") || ""); + return axios + .get("http://localhost:8000/api/v1/accounts/users/me/", { + headers: { + Authorization: "Token " + token, }, }) .then((response) => { diff --git a/src/Components/Header/Header.tsx b/src/Components/Header/Header.tsx index 3372d35..2fc6bae 100644 --- a/src/Components/Header/Header.tsx +++ b/src/Components/Header/Header.tsx @@ -15,10 +15,10 @@ export default function Header() { ...{ flex: 2 }, }} > - - - - + + + +
navigate("/")} > -
- - - - - - -
+ + + + + + ); } diff --git a/src/Components/LoginButton/LoginButton.tsx b/src/Components/LoginButton/LoginButton.tsx index 70448c8..1f71443 100644 --- a/src/Components/LoginButton/LoginButton.tsx +++ b/src/Components/LoginButton/LoginButton.tsx @@ -3,9 +3,13 @@ import { useState } from "react"; import { Button } from "@mui/material"; import { useNavigate } from "react-router-dom"; import styles from "../../styles"; +import { GetUsername, UserInfo } from "../Api/Api"; export default function LoginButton() { const [logged_in, setLoggedIn] = useState(false); + const [user, setUser] = useState( + JSON.parse(localStorage.getItem("user") || "").username + ); const navigate = useNavigate(); if (!logged_in) { return ( @@ -26,7 +30,7 @@ export default function LoginButton() { } return (
-

Logged in as

+

Logged in as {user}