From 84b6981dcb6392870e2a09fcf598ea5533527860 Mon Sep 17 00:00:00 2001 From: Fingertips Date: Fri, 13 Sep 2024 16:32:13 +0800 Subject: [PATCH] Create sign out button Consume sign out auth api and fix cookie token not showing in the application tab of the browser --- client/src/components/icon-button.tsx | 33 +++++++++++++++++++ .../{button.tsx => text-button.tsx} | 12 +++++-- client/src/constants/keys.ts | 1 + client/src/constants/routes.ts | 1 + client/src/lib/services/auth-service.ts | 10 +++++- .../pages/root/_components/header/index.tsx | 6 +++- .../_components/header/sign-out-button.tsx | 27 +++++++++++++++ .../root/_components/header/toggle-mode.tsx | 8 ++--- .../sign-in/_components/sign-in-form.tsx | 2 +- .../sign-up/_components/sign-up-form.tsx | 2 +- controllers/auth_controller.go | 32 ++++++++---------- 11 files changed, 102 insertions(+), 32 deletions(-) create mode 100644 client/src/components/icon-button.tsx rename client/src/components/{button.tsx => text-button.tsx} (80%) create mode 100644 client/src/pages/root/_components/header/sign-out-button.tsx diff --git a/client/src/components/icon-button.tsx b/client/src/components/icon-button.tsx new file mode 100644 index 0000000..5b0fc40 --- /dev/null +++ b/client/src/components/icon-button.tsx @@ -0,0 +1,33 @@ +import { Loader2, LucideProps } from "lucide-react"; + +interface IconButtonProps { + icon: React.ForwardRefExoticComponent< + Omit & React.RefAttributes + >; + size?: number; + onClick: () => void; + disabled?: boolean; + loading?: boolean; +} + +const IconButton = ({ + icon: Icon, + size = 22, + onClick, + disabled, + loading, +}: IconButtonProps) => { + return ( + + ); +}; + +export default IconButton; diff --git a/client/src/components/button.tsx b/client/src/components/text-button.tsx similarity index 80% rename from client/src/components/button.tsx rename to client/src/components/text-button.tsx index 304c4e4..0e8fd71 100644 --- a/client/src/components/button.tsx +++ b/client/src/components/text-button.tsx @@ -1,6 +1,6 @@ import { Loader2 } from "lucide-react"; -interface ButtonProps { +interface TextButtonProps { label: string; onClick?: () => void; type: "submit" | "reset" | "button" | undefined; @@ -8,7 +8,13 @@ interface ButtonProps { loading?: boolean; } -const Button = ({ label, onClick, type, disabled, loading }: ButtonProps) => { +const TextButton = ({ + label, + onClick, + type, + disabled, + loading, +}: TextButtonProps) => { return ( + ); }; diff --git a/client/src/pages/sign-in/_components/sign-in-form.tsx b/client/src/pages/sign-in/_components/sign-in-form.tsx index 16f51a1..7e5a3c5 100644 --- a/client/src/pages/sign-in/_components/sign-in-form.tsx +++ b/client/src/pages/sign-in/_components/sign-in-form.tsx @@ -8,7 +8,7 @@ import { SIGNININPUTS } from "@/constants/collections"; import { SignInDTO } from "@/lib/DTO/sign-in.dto"; import { AppRoutes } from "@/constants/routes"; import { SIGNINKEY } from "@/constants/keys"; -import { Button } from "@/components/button"; +import { Button } from "@/components/text-button"; import { Input } from "@/components/input"; const SignInForm = () => { diff --git a/client/src/pages/sign-up/_components/sign-up-form.tsx b/client/src/pages/sign-up/_components/sign-up-form.tsx index 06b5aab..c04a29b 100644 --- a/client/src/pages/sign-up/_components/sign-up-form.tsx +++ b/client/src/pages/sign-up/_components/sign-up-form.tsx @@ -8,7 +8,7 @@ import { SIGNUPINPUTS } from "@/constants/collections"; import { SignUpDTO } from "@/lib/DTO/sign-up.dto"; import { AppRoutes } from "@/constants/routes"; import { SIGNUPKEY } from "@/constants/keys"; -import { Button } from "@/components/button"; +import { Button } from "@/components/text-button"; import { Input } from "@/components/input"; const SignUpForm = () => { diff --git a/controllers/auth_controller.go b/controllers/auth_controller.go index de96275..26e3f3d 100644 --- a/controllers/auth_controller.go +++ b/controllers/auth_controller.go @@ -76,31 +76,25 @@ func SignIn(c fiber.Ctx) error { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid credentials"}) } - token, err := utils.GenerateJWTToken(user.ID.String(), user.Username) - if err != nil { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()}) - } - - utils.SetCookieToken(c, token) + if user.IsVerified { + token, err := utils.GenerateJWTToken(user.ID.String(), user.Username) + if err != nil { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()}) + } - user.LastSignedIn = time.Now() + utils.SetCookieToken(c, token) - if time.Now().After(*user.VerificationTokenExpiration) { - verificationToken := rand.Intn(9000) + 1000 - tokenString := strconv.Itoa(verificationToken) - user.VerificationToken = &tokenString + user.LastSignedIn = time.Now() - if err := utils.SendEmailVerification(user.Email, user.Username, *user.VerificationToken); err != nil { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err}) + res = database.Instance.Save(&user) + if res.Error != nil { + return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Unable to save sign in credentials"}) } - } - res = database.Instance.Save(&user) - if res.Error != nil { - return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Unable to save sign in credentials"}) + return c.Status(fiber.StatusOK).JSON(fiber.Map{"message": "Sign in successful", "user": user}) + } else { + return c.Status(fiber.StatusForbidden).JSON(fiber.Map{"error": "User is not verified"}) } - - return c.Status(fiber.StatusOK).JSON(fiber.Map{"message": "Sign in successful", "user": user}) } func SignOut(c fiber.Ctx) error {