diff --git a/src/Components/Api/Api.tsx b/src/Components/Api/Api.tsx
index a95e7f9..701e0b4 100644
--- a/src/Components/Api/Api.tsx
+++ b/src/Components/Api/Api.tsx
@@ -1,5 +1,5 @@
import axios from "axios";
-
+import { useSelector, useDispatch } from "react-redux";
export function GetNotes() {
return axios.get("http://localhost:8000/api/v1/notes/").then((response) => {
return response.data;
@@ -34,7 +34,6 @@ export function UserLogin(user: user) {
.then(async (response) => {
localStorage.setItem("token", JSON.stringify(response.data.auth_token));
console.log("Stored: ", JSON.parse(localStorage.getItem("token") || ""));
- StoreUser();
return true;
})
.catch((error) => {
@@ -43,23 +42,6 @@ export function UserLogin(user: user) {
});
}
-export function StoreUser() {
- const token = JSON.parse(localStorage.getItem("token") || "");
- return axios
- .get("http://localhost:8000/api/v1/accounts/users/me/", {
- headers: {
- 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
@@ -76,20 +58,3 @@ export function UserInfo() {
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) => {
- return response.data;
- })
- .catch((error) => {
- console.log("Error in fetching user data");
- console.log(error);
- });
-}
diff --git a/src/Components/LoginButton/LoginButton.tsx b/src/Components/LoginButton/LoginButton.tsx
index 1f71443..663c3f0 100644
--- a/src/Components/LoginButton/LoginButton.tsx
+++ b/src/Components/LoginButton/LoginButton.tsx
@@ -1,15 +1,27 @@
import * as React from "react";
-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";
+import { useSelector, useDispatch } from "react-redux";
+import { SetLoggedOut } from "../../Features/Redux/Slices/LoginSlice/LoginSlice";
+import { UnsetUser } from "../../Features/Redux/Slices/LoggedInUserSlice/LoggedInUserSlice";
+
+export interface user {
+ LoggedInUser: {
+ value: {
+ email: string;
+ id: number;
+ username: string;
+ };
+ };
+}
export default function LoginButton() {
- const [logged_in, setLoggedIn] = useState(false);
- const [user, setUser] = useState(
- JSON.parse(localStorage.getItem("user") || "").username
+ const dispatch = useDispatch();
+ const logged_in = useSelector(
+ (state: { Login: { logged_in: boolean } }) => state.Login.logged_in
);
+ const logged_in_user = useSelector((state: user) => state.LoggedInUser.value);
const navigate = useNavigate();
if (!logged_in) {
return (
@@ -30,13 +42,16 @@ export default function LoginButton() {
}
return (
-
Logged in as {user}
+
Logged in as {logged_in_user.username}