Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor user verification redirection, fix reset password validation and improve loading states #5

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions client/src/components/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ const Input = ({
type={isPassword ? (obscure ? "password" : "text") : type}
placeholder={placeholder}
autoComplete={autoComplete}
id={name?.toLowerCase() ?? label.toLowerCase()}
name={name?.toLowerCase() ?? label.toLowerCase()}
id={name?.toLowerCase() ?? label.toLowerCase().trim()}
name={name?.toLowerCase() ?? label.toLowerCase().trim()}
disabled={disabled}
required={required}
maxLength={maxLength}
Expand Down
4 changes: 2 additions & 2 deletions client/src/constants/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export const SIGNIN_INPUTS = [

export const RESET_PASSWORD_INPUTS = [
{
name: "old-password",
name: "oldPassword",
label: "Old Password",
tooltip: "Enter your current password",
placeholder: "e.g. m#P52s@ap$V",
Expand All @@ -96,7 +96,7 @@ export const RESET_PASSWORD_INPUTS = [
maxLength: 128,
},
{
name: "new-password",
name: "newPassword",
label: "New Password",
tooltip:
"Create a password with at least 8 characters, including uppercase, lowercase, numbers, and special characters for security",
Expand Down
3 changes: 2 additions & 1 deletion client/src/lib/DTO/reset-dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type ResetDTO = {
token: string;
password: string;
oldpassword: string;
newpassword: string;
};
3 changes: 2 additions & 1 deletion client/src/lib/services/auth-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ export const AuthService = {
"Content-Type": "application/json",
},
body: JSON.stringify({
password: reset.password,
old_password: reset.oldpassword,
new_password: reset.newpassword,
}),
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const ResetPasswordForm = () => {
disabled={isPending}
{...r}
validation={(value) => {
if (r.name === "new-password") {
if (r.name === "newPassword") {
setConfirmPassword(value);
}
if (r.name === undefined) {
Expand Down
25 changes: 5 additions & 20 deletions client/src/pages/sign-in/_components/sign-in-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { toast } from "sonner";
import { GenericResponse } from "@/lib/classes/generic-response-class";
import { ErrorResponse } from "@/lib/classes/error-response-class";
import { AuthService } from "@/lib/services/auth-service";
import { useAuthStore } from "@/lib/stores/auth-store";
import { SIGNIN_INPUTS } from "@/constants/collections";
import { useAuthStore } from "@/lib/stores/auth-store";
import { SignInDTO } from "@/lib/DTO/sign-in-dto";
import { Button } from "@/components/text-button";
import { AppRoutes } from "@/constants/routes";
Expand All @@ -25,28 +25,13 @@ const SignInForm = () => {
setAuthorized(true);
},
onError: (error: ErrorResponse) => {
toast.error("Please verify to sign in. Didn't get the code?", {
className: "gap-x-4",
actionButtonStyle: {
background: "rgb(var(--accent))",
color: "rgb(var(--foreground))",
padding: "16px",
marginLeft: "16px",
filter: `
drop-shadow(0 0px 25px rgb(var(--accent)))
drop-shadow(0 0px 50px rgb(var(--accent)))
`,
},
action: {
label: "Resend",
onClick: () => navigate(AppRoutes.ResendVerify),
},
});

if (error.status == 403) {
toast.error("Please verify to sign in");
navigate(AppRoutes.VerifyEmail);
} else {
toast.error(error.message);
setAuthorized(false);
}
setAuthorized(false);
},
});

Expand Down
7 changes: 4 additions & 3 deletions client/src/pages/verify-email/_components/resend-code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ const ResendCode = () => {
return (
<div
className="p-4 w-full text-sm lg:text-base rounded-md border
border-secondary/50 bg-secondary/15 drop-shadow-2xl flex-center gap-x-2"
border-secondary/50 bg-secondary/15 drop-shadow-2xl flex-center gap-x-2"
>
<p className="font-medium">Didn't receive a code?</p>
<button
className="font-bold underline-offset-4 hover:underline text-secondary transition-all hover:drop-shadow-secondary-glow"
className="font-bold underline-offset-4 hover:underline text-secondary transition-all hover:drop-shadow-secondary-glow
disabled:text-secondary/50 disabled:pointer-events-none"
type="button"
onClick={onClick}
disabled={isPending}
>
Resend
{isPending ? "Resending..." : "Resend"}
</button>
</div>
);
Expand Down
15 changes: 12 additions & 3 deletions controllers/auth_controller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"fmt"
"math/rand"
"strconv"
"time"
Expand Down Expand Up @@ -250,10 +251,11 @@ func ForgotPassword(c fiber.Ctx) error {

func ResetPassword(c fiber.Ctx) error {
var data struct {
Password string `json:"password"`
OldPassword string `json:"old_password"`
NewPassword string `json:"new_password"`
}
if err := c.Bind().JSON(&data); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(dto.ErrorDTO{Error: "Either password is invalid or empty"})
return c.Status(fiber.StatusBadRequest).JSON(dto.ErrorDTO{Error: "Old or new password is either invalid or empty"})
}

token := c.Params("token")
Expand All @@ -271,7 +273,14 @@ func ResetPassword(c fiber.Ctx) error {
return c.Status(fiber.StatusInternalServerError).JSON(dto.ErrorDTO{Error: res.Error.Error()})
}

password, err := utils.HashPassword(data.Password)
fmt.Println("Password", user.Password)
fmt.Println("Old Password", data.OldPassword)

if err := utils.VerifyPassword([]byte(user.Password), []byte(data.OldPassword)); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(dto.ErrorDTO{Error: "Password did not match"})
}

password, err := utils.HashPassword(data.NewPassword)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(dto.ErrorDTO{Error: err.Error()})
}
Expand Down