added logout function and enhanced greeting
This commit is contained in:
@@ -8,6 +8,7 @@ import Cookies from "js-cookie";
|
|||||||
import { useAtom } from "jotai";
|
import { useAtom } from "jotai";
|
||||||
import { setIsLoggedInAtom } from "@/states/Atoms";
|
import { setIsLoggedInAtom } from "@/states/Atoms";
|
||||||
import { UserContext, type User } from "./states/Context";
|
import { UserContext, type User } from "./states/Context";
|
||||||
|
import { triggerLogoutAtom } from "@/states/Atoms";
|
||||||
|
|
||||||
const API_BASE =
|
const API_BASE =
|
||||||
(import.meta as any).env?.VITE_BACKEND_URL ||
|
(import.meta as any).env?.VITE_BACKEND_URL ||
|
||||||
@@ -16,8 +17,9 @@ const API_BASE =
|
|||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [user, setUser] = useState<User | undefined>(undefined);
|
const [user, setUser] = useState<User | undefined>(undefined);
|
||||||
|
|
||||||
const [, setIsLoggedIn] = useAtom(setIsLoggedInAtom);
|
const [, setIsLoggedIn] = useAtom(setIsLoggedInAtom);
|
||||||
|
const [, setTriggerLogout] = useAtom(triggerLogoutAtom);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (Cookies.get("token")) {
|
if (Cookies.get("token")) {
|
||||||
const verifyToken = async () => {
|
const verifyToken = async () => {
|
||||||
@@ -28,6 +30,7 @@ function App() {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
|
setTriggerLogout(false);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
setUser({ username: data.user.username, role: data.user.role });
|
setUser({ username: data.user.username, role: data.user.role });
|
||||||
setIsLoggedIn(true);
|
setIsLoggedIn(true);
|
||||||
|
|||||||
@@ -1,4 +1,16 @@
|
|||||||
import { useUserContext } from "@/states/Context";
|
import { useUserContext } from "@/states/Context";
|
||||||
|
import {
|
||||||
|
Container,
|
||||||
|
Stack,
|
||||||
|
Heading,
|
||||||
|
Text,
|
||||||
|
Badge,
|
||||||
|
Flex,
|
||||||
|
Button,
|
||||||
|
} from "@chakra-ui/react";
|
||||||
|
import { triggerLogoutAtom, setIsLoggedInAtom } from "@/states/Atoms";
|
||||||
|
import { useAtom } from "jotai";
|
||||||
|
import Cookies from "js-cookie";
|
||||||
|
|
||||||
export interface User {
|
export interface User {
|
||||||
username: string;
|
username: string;
|
||||||
@@ -7,12 +19,56 @@ export interface User {
|
|||||||
|
|
||||||
export const HomePage = () => {
|
export const HomePage = () => {
|
||||||
const userData = useUserContext();
|
const userData = useUserContext();
|
||||||
|
const [, setTriggerLogout] = useAtom(triggerLogoutAtom);
|
||||||
|
const [, setIsLoggedIn] = useAtom(setIsLoggedInAtom);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<Container maxW="7xl" className="px-6 sm:px-8 pt-10">
|
||||||
<h1>Home Page</h1>
|
<Stack as="header" gap={3} className="mb-16">
|
||||||
<p>Welcome, {userData.username}!</p>
|
<Flex
|
||||||
<p>Your role is: {userData.role}</p>
|
direction={{ base: "column", md: "row" }}
|
||||||
</div>
|
align={{ base: "stretch", md: "center" }}
|
||||||
|
justify="space-between"
|
||||||
|
gap={4}
|
||||||
|
>
|
||||||
|
<Stack gap={2}>
|
||||||
|
<Heading
|
||||||
|
size="2xl"
|
||||||
|
className="tracking-tight text-slate-900 dark:text-slate-100"
|
||||||
|
>
|
||||||
|
Home
|
||||||
|
</Heading>
|
||||||
|
<Stack
|
||||||
|
direction={{ base: "column", sm: "row" }}
|
||||||
|
gap={2}
|
||||||
|
alignItems="start"
|
||||||
|
>
|
||||||
|
<Text
|
||||||
|
fontSize="md"
|
||||||
|
className="text-slate-600 dark:text-slate-400"
|
||||||
|
>
|
||||||
|
Willkommen zurück, {userData.username}!
|
||||||
|
</Text>
|
||||||
|
<Badge variant="subtle" px={2} py={1} borderRadius="full">
|
||||||
|
Rolle: {userData.role}
|
||||||
|
</Badge>
|
||||||
|
</Stack>
|
||||||
|
</Stack>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
onClick={() => {
|
||||||
|
Cookies.remove("token");
|
||||||
|
setIsLoggedIn(false);
|
||||||
|
setTriggerLogout(true);
|
||||||
|
}}
|
||||||
|
variant="solid"
|
||||||
|
size="sm"
|
||||||
|
className="self-start md:self-auto"
|
||||||
|
>
|
||||||
|
Logout
|
||||||
|
</Button>
|
||||||
|
</Flex>
|
||||||
|
</Stack>
|
||||||
|
</Container>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import MyAlert from "../components/myChakra/MyAlert";
|
import MyAlert from "../components/myChakra/MyAlert";
|
||||||
import { Button, Card, Field, Input, Stack } from "@chakra-ui/react";
|
import { Button, Card, Field, Input, Stack } from "@chakra-ui/react";
|
||||||
import { setIsLoggedInAtom } from "@/states/Atoms";
|
import { setIsLoggedInAtom, triggerLogoutAtom } from "@/states/Atoms";
|
||||||
import { useAtom } from "jotai";
|
import { useAtom } from "jotai";
|
||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
import { Navigate, useNavigate } from "react-router-dom";
|
import { Navigate, useNavigate } from "react-router-dom";
|
||||||
@@ -13,6 +13,7 @@ const API_BASE =
|
|||||||
|
|
||||||
export const LoginPage = () => {
|
export const LoginPage = () => {
|
||||||
const [isLoggedIn, setIsLoggedIn] = useAtom(setIsLoggedInAtom);
|
const [isLoggedIn, setIsLoggedIn] = useAtom(setIsLoggedInAtom);
|
||||||
|
const [triggerLogout, setTriggerLogout] = useAtom(triggerLogoutAtom);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -57,6 +58,7 @@ export const LoginPage = () => {
|
|||||||
setIsError(true);
|
setIsError(true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
setTriggerLogout(false);
|
||||||
navigate("/", { replace: true });
|
navigate("/", { replace: true });
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -98,9 +100,18 @@ export const LoginPage = () => {
|
|||||||
<MyAlert status="error" title={errorMsg} description={errorDsc} />
|
<MyAlert status="error" title={errorMsg} description={errorDsc} />
|
||||||
)}
|
)}
|
||||||
<Button type="submit" onClick={() => handleLogin()} variant="solid">
|
<Button type="submit" onClick={() => handleLogin()} variant="solid">
|
||||||
Login <p>{isLoggedIn}</p>
|
Login
|
||||||
</Button>
|
</Button>
|
||||||
</Card.Footer>
|
</Card.Footer>
|
||||||
|
<Card.Footer justifyContent="flex-end">
|
||||||
|
{triggerLogout && (
|
||||||
|
<MyAlert
|
||||||
|
status="success"
|
||||||
|
title={"Logout erfolgreich!"}
|
||||||
|
description={"Sie wurden erfolgreich abgemeldet."}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</Card.Footer>
|
||||||
</Card.Root>
|
</Card.Root>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -2,3 +2,4 @@ import { atom } from "jotai";
|
|||||||
|
|
||||||
export const testAtom = atom<number>(0);
|
export const testAtom = atom<number>(0);
|
||||||
export const setIsLoggedInAtom = atom<boolean>(false);
|
export const setIsLoggedInAtom = atom<boolean>(false);
|
||||||
|
export const triggerLogoutAtom = atom<boolean>(false);
|
||||||
|
|||||||
Reference in New Issue
Block a user