Refactor(signin with otp): make code more clean
This commit is contained in:
parent
2b61c97cb4
commit
99781de44c
|
@ -1,34 +1,53 @@
|
|||
"use server";
|
||||
import { encodedRedirect } from "@/utils/utils";
|
||||
|
||||
import { createClient } from "@/utils/supabase/server";
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
export const signInAction = async (formData: FormData) => {
|
||||
const email = formData.get("email") as string;
|
||||
export const signInAction = async (formData: { email: string }) => {
|
||||
const supabase = await createClient();
|
||||
const encodeEmail = encodeURIComponent(formData.email);
|
||||
|
||||
if (!email || email === "") {
|
||||
return encodedRedirect("error", "/sign-in", "Email is required");
|
||||
try {
|
||||
// First, check for existing session
|
||||
const {
|
||||
data: { session },
|
||||
error: sessionError,
|
||||
} = await supabase.auth.getSession();
|
||||
|
||||
// If there's an active session and the email matches
|
||||
if (session && session.user.email === formData.email) {
|
||||
return {
|
||||
success: true,
|
||||
message: "Already logged in",
|
||||
redirectTo: "/protected/dashboard", // or wherever you want to redirect logged in users
|
||||
};
|
||||
}
|
||||
|
||||
// If no active session or different email, proceed with OTP
|
||||
const { data, error } = await supabase.auth.signInWithOtp({
|
||||
email: formData.email,
|
||||
options: {
|
||||
shouldCreateUser: false,
|
||||
},
|
||||
});
|
||||
|
||||
if (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
redirectTo: `/verify-otp?email=${encodeEmail}`,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "OTP has been sent to your email",
|
||||
redirectTo: `/verify-otp?email=${encodeEmail}`,
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
error: "An unexpected error occurred",
|
||||
redirectTo: "/sign-in",
|
||||
};
|
||||
}
|
||||
|
||||
const { data, error } = await supabase.auth.signInWithOtp({
|
||||
email,
|
||||
options: {
|
||||
shouldCreateUser: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (error && error.message.includes("User not found")) {
|
||||
// Encode email parameter untuk keamanan
|
||||
const encodedEmail = encodeURIComponent(email);
|
||||
return redirect(`/sign-in?email=${encodedEmail}`);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return encodedRedirect("error", "/sign-in", error.message);
|
||||
}
|
||||
|
||||
// Redirect dengan email parameter
|
||||
const encodedEmail = encodeURIComponent(email);
|
||||
return redirect(`/verify-otp?email=${encodedEmail}`);
|
||||
};
|
|
@ -1,103 +1,103 @@
|
|||
import { cn } from "@/lib/utils";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Card,
|
||||
CardContent,
|
||||
CardDescription,
|
||||
CardHeader,
|
||||
CardTitle,
|
||||
} from "@/components/ui/card";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { SubmitButton } from "../submit-button";
|
||||
import { signInAction } from "@/actions/auth/sign-in";
|
||||
// import { cn } from "@/lib/utils";
|
||||
// import { Button } from "@/components/ui/button";
|
||||
// import {
|
||||
// Card,
|
||||
// CardContent,
|
||||
// CardDescription,
|
||||
// CardHeader,
|
||||
// CardTitle,
|
||||
// } from "@/components/ui/card";
|
||||
// import { Input } from "@/components/ui/input";
|
||||
// import { Label } from "@/components/ui/label";
|
||||
// import { SubmitButton } from "../submit-button";
|
||||
// import { signInAction } from "@/actions/auth/sign-in";
|
||||
|
||||
export function LoginForm({
|
||||
className,
|
||||
...props
|
||||
}: React.ComponentPropsWithoutRef<"div">) {
|
||||
return (
|
||||
<div className={cn("flex flex-col gap-6", className)} {...props}>
|
||||
<Card>
|
||||
<CardHeader className="text-center">
|
||||
<CardTitle className="text-xl">Welcome back</CardTitle>
|
||||
<CardDescription>
|
||||
Login with your Apple or Google account
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<form>
|
||||
<div className="grid gap-6">
|
||||
{/* <div className="flex flex-col gap-4">
|
||||
<Button variant="outline" className="w-full">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
|
||||
<path
|
||||
d="M12.152 6.896c-.948 0-2.415-1.078-3.96-1.04-2.04.027-3.91 1.183-4.961 3.014-2.117 3.675-.546 9.103 1.519 12.09 1.013 1.454 2.208 3.09 3.792 3.039 1.52-.065 2.09-.987 3.935-.987 1.831 0 2.35.987 3.96.948 1.637-.026 2.676-1.48 3.676-2.948 1.156-1.688 1.636-3.325 1.662-3.415-.039-.013-3.182-1.221-3.22-4.857-.026-3.04 2.48-4.494 2.597-4.559-1.429-2.09-3.623-2.324-4.39-2.376-2-.156-3.675 1.09-4.61 1.09zM15.53 3.83c.843-1.012 1.4-2.427 1.245-3.83-1.207.052-2.662.805-3.532 1.818-.78.896-1.454 2.338-1.273 3.714 1.338.104 2.715-.688 3.559-1.701"
|
||||
fill="currentColor"
|
||||
/>
|
||||
</svg>
|
||||
Login with Apple
|
||||
</Button>
|
||||
<Button variant="outline" className="w-full">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
|
||||
<path
|
||||
d="M12.48 10.92v3.28h7.84c-.24 1.84-.853 3.187-1.787 4.133-1.147 1.147-2.933 2.4-6.053 2.4-4.827 0-8.6-3.893-8.6-8.72s3.773-8.72 8.6-8.72c2.6 0 4.507 1.027 5.907 2.347l2.307-2.307C18.747 1.44 16.133 0 12.48 0 5.867 0 .307 5.387.307 12s5.56 12 12.173 12c3.573 0 6.267-1.173 8.373-3.36 2.16-2.16 2.84-5.213 2.84-7.667 0-.76-.053-1.467-.173-2.053H12.48z"
|
||||
fill="currentColor"
|
||||
/>
|
||||
</svg>
|
||||
Login with Google
|
||||
</Button>
|
||||
</div> */}
|
||||
<div className="relative text-center text-sm after:absolute after:inset-0 after:top-1/2 after:z-0 after:flex after:items-center after:border-t after:border-border">
|
||||
{/* <span className="relative z-10 bg-background px-2 text-muted-foreground">
|
||||
Or continue with
|
||||
</span> */}
|
||||
</div>
|
||||
<div className="grid gap-6">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="email">Email</Label>
|
||||
<Input
|
||||
id="email"
|
||||
type="email"
|
||||
placeholder="m@example.com"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
{/* <div className="grid gap-2">
|
||||
<div className="flex items-center">
|
||||
<Label htmlFor="password">Password</Label>
|
||||
<a
|
||||
href="#"
|
||||
className="ml-auto text-sm underline-offset-4 hover:underline"
|
||||
>
|
||||
Forgot your password?
|
||||
</a>
|
||||
</div>
|
||||
<Input id="password" type="password" required />
|
||||
</div> */}
|
||||
<SubmitButton
|
||||
type="submit"
|
||||
className="w-full"
|
||||
pendingText="Signing In..."
|
||||
formAction={signInAction}
|
||||
>
|
||||
Login
|
||||
</SubmitButton>
|
||||
</div>
|
||||
<div className="text-center text-sm">
|
||||
Don't have an account?{" "}
|
||||
<a href="/contact-us" className="underline underline-offset-4">
|
||||
Contact Us
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<div className="text-balance text-center text-xs text-muted-foreground [&_a]:underline [&_a]:underline-offset-4 [&_a]:hover:text-primary ">
|
||||
By clicking continue, you agree to our <a href="#">Terms of Service</a>{" "}
|
||||
and <a href="#">Privacy Policy</a>.
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
// export function LoginForm({
|
||||
// className,
|
||||
// ...props
|
||||
// }: React.ComponentPropsWithoutRef<"div">) {
|
||||
// return (
|
||||
// <div className={cn("flex flex-col gap-6", className)} {...props}>
|
||||
// <Card>
|
||||
// <CardHeader className="text-center">
|
||||
// <CardTitle className="text-xl">Welcome back</CardTitle>
|
||||
// <CardDescription>
|
||||
// Login with your Apple or Google account
|
||||
// </CardDescription>
|
||||
// </CardHeader>
|
||||
// <CardContent>
|
||||
// <form>
|
||||
// <div className="grid gap-6">
|
||||
// {/* <div className="flex flex-col gap-4">
|
||||
// <Button variant="outline" className="w-full">
|
||||
// <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
|
||||
// <path
|
||||
// d="M12.152 6.896c-.948 0-2.415-1.078-3.96-1.04-2.04.027-3.91 1.183-4.961 3.014-2.117 3.675-.546 9.103 1.519 12.09 1.013 1.454 2.208 3.09 3.792 3.039 1.52-.065 2.09-.987 3.935-.987 1.831 0 2.35.987 3.96.948 1.637-.026 2.676-1.48 3.676-2.948 1.156-1.688 1.636-3.325 1.662-3.415-.039-.013-3.182-1.221-3.22-4.857-.026-3.04 2.48-4.494 2.597-4.559-1.429-2.09-3.623-2.324-4.39-2.376-2-.156-3.675 1.09-4.61 1.09zM15.53 3.83c.843-1.012 1.4-2.427 1.245-3.83-1.207.052-2.662.805-3.532 1.818-.78.896-1.454 2.338-1.273 3.714 1.338.104 2.715-.688 3.559-1.701"
|
||||
// fill="currentColor"
|
||||
// />
|
||||
// </svg>
|
||||
// Login with Apple
|
||||
// </Button>
|
||||
// <Button variant="outline" className="w-full">
|
||||
// <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
|
||||
// <path
|
||||
// d="M12.48 10.92v3.28h7.84c-.24 1.84-.853 3.187-1.787 4.133-1.147 1.147-2.933 2.4-6.053 2.4-4.827 0-8.6-3.893-8.6-8.72s3.773-8.72 8.6-8.72c2.6 0 4.507 1.027 5.907 2.347l2.307-2.307C18.747 1.44 16.133 0 12.48 0 5.867 0 .307 5.387.307 12s5.56 12 12.173 12c3.573 0 6.267-1.173 8.373-3.36 2.16-2.16 2.84-5.213 2.84-7.667 0-.76-.053-1.467-.173-2.053H12.48z"
|
||||
// fill="currentColor"
|
||||
// />
|
||||
// </svg>
|
||||
// Login with Google
|
||||
// </Button>
|
||||
// </div> */}
|
||||
// <div className="relative text-center text-sm after:absolute after:inset-0 after:top-1/2 after:z-0 after:flex after:items-center after:border-t after:border-border">
|
||||
// {/* <span className="relative z-10 bg-background px-2 text-muted-foreground">
|
||||
// Or continue with
|
||||
// </span> */}
|
||||
// </div>
|
||||
// <div className="grid gap-6">
|
||||
// <div className="grid gap-2">
|
||||
// <Label htmlFor="email">Email</Label>
|
||||
// <Input
|
||||
// id="email"
|
||||
// type="email"
|
||||
// placeholder="m@example.com"
|
||||
// required
|
||||
// />
|
||||
// </div>
|
||||
// {/* <div className="grid gap-2">
|
||||
// <div className="flex items-center">
|
||||
// <Label htmlFor="password">Password</Label>
|
||||
// <a
|
||||
// href="#"
|
||||
// className="ml-auto text-sm underline-offset-4 hover:underline"
|
||||
// >
|
||||
// Forgot your password?
|
||||
// </a>
|
||||
// </div>
|
||||
// <Input id="password" type="password" required />
|
||||
// </div> */}
|
||||
// <SubmitButton
|
||||
// type="submit"
|
||||
// className="w-full"
|
||||
// pendingText="Signing In..."
|
||||
// formAction={signInAction}
|
||||
// >
|
||||
// Login
|
||||
// </SubmitButton>
|
||||
// </div>
|
||||
// <div className="text-center text-sm">
|
||||
// Don't have an account?{" "}
|
||||
// <a href="/contact-us" className="underline underline-offset-4">
|
||||
// Contact Us
|
||||
// </a>
|
||||
// </div>
|
||||
// </div>
|
||||
// </form>
|
||||
// </CardContent>
|
||||
// </Card>
|
||||
// <div className="text-balance text-center text-xs text-muted-foreground [&_a]:underline [&_a]:underline-offset-4 [&_a]:hover:text-primary ">
|
||||
// By clicking continue, you agree to our <a href="#">Terms of Service</a>{" "}
|
||||
// and <a href="#">Privacy Policy</a>.
|
||||
// </div>
|
||||
// </div>
|
||||
// );
|
||||
// }
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
import { z } from "zod";
|
||||
|
||||
export const Roles = {
|
||||
ADMIN: "admin",
|
||||
USER: "user",
|
||||
STAFF: "staff",
|
||||
};
|
||||
|
||||
export type Roles = typeof Roles;
|
||||
|
||||
export const userSchema = z.object({
|
||||
id: z.string(),
|
||||
email: z.string(),
|
||||
emailVerified: z.boolean().default(false),
|
||||
password: z.string().nullable(),
|
||||
firstName: z.string().nullable(),
|
||||
lastName: z.string().nullable(),
|
||||
avatar: z.string().nullable(),
|
||||
role: z.nativeEnum(Roles).default(Roles.USER),
|
||||
createdAt: z.date(),
|
||||
updatedAt: z.date(),
|
||||
lastSignedIn: z.date().nullable(),
|
||||
metadata: z.any().nullable(),
|
||||
profile: z
|
||||
.object({
|
||||
id: z.string(),
|
||||
userId: z.string(),
|
||||
bio: z.string().nullable(),
|
||||
phone: z.string().nullable(),
|
||||
address: z.string().nullable(),
|
||||
city: z.string().nullable(),
|
||||
country: z.string().nullable(),
|
||||
birthDate: z.date().nullable(),
|
||||
})
|
||||
.nullable(),
|
||||
});
|
||||
|
||||
export type User = z.infer<typeof userSchema>;
|
||||
|
||||
export const userInsertSchema = userSchema.pick({
|
||||
email: true,
|
||||
password: true,
|
||||
firstName: true,
|
||||
lastName: true,
|
||||
role: true,
|
||||
profile: true,
|
||||
});
|
||||
|
||||
export type UserInsert = z.infer<typeof userInsertSchema>;
|
||||
|
||||
export const signInSchema = z.object({
|
||||
email: z.string(),
|
||||
password: z.string(),
|
||||
phone: z.string(),
|
||||
username: z.string(),
|
||||
});
|
||||
|
||||
export type SignIn = z.infer<typeof signInSchema>;
|
||||
|
||||
export const signInWithPasswordlessSchema = signInSchema.pick({
|
||||
email: true,
|
||||
});
|
||||
|
||||
export type SignInWithOtp = z.infer<typeof signInWithPasswordlessSchema>;
|
||||
|
||||
export interface SignInResponse {
|
||||
success: boolean;
|
||||
message?: string;
|
||||
error?: string;
|
||||
errors?: Record<string, string>;
|
||||
redirectTo?: string;
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
import { SignInResponse, SignInWithOtp } from "../entities/models/user.model";
|
||||
|
||||
export interface SignInRepository {
|
||||
signInWithPasswordless(email: SignInWithOtp): Promise<SignInResponse>;
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
import { SignInResponse, SignInWithOtp } from "../../entities/models/user.model";
|
||||
import { SignInRepository } from "../../repositories/signin.repository";
|
||||
|
||||
export class SignInUseCase {
|
||||
constructor(private signInRepository: SignInRepository) {}
|
||||
|
||||
async executeSignInWithPasswordless(email: SignInWithOtp): Promise<SignInResponse> {
|
||||
return this.signInRepository.signInWithPasswordless(email);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,113 @@
|
|||
import { toast } from "@/hooks/use-toast";
|
||||
import { useState } from "react";
|
||||
import { ContactRepositoryImpl } from "../repositories/contact-us.repository.impl";
|
||||
import { validateContactForm } from "../validators/contact-us.validator";
|
||||
import { SignInWithOtp } from "@/src/applications/entities/models/user.model";
|
||||
import { SignInRepositoryImpl } from "../repositories/signin.repository.impl";
|
||||
import { validateSignInWithOtp } from "../validators/signin.validator";
|
||||
import Router from "next/router";
|
||||
import { useNavigations } from "@/hooks/use-navigations";
|
||||
|
||||
export const useSignInForm = () => {
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
const [formData, setFormData] = useState<SignInWithOtp>({
|
||||
email: "",
|
||||
});
|
||||
const [errors, setErrors] = useState<
|
||||
Partial<Record<keyof SignInWithOtp, string>>
|
||||
>({});
|
||||
const { router } = useNavigations();
|
||||
|
||||
const contactRepository = new SignInRepositoryImpl();
|
||||
|
||||
// Handle input change
|
||||
const handleChange = (
|
||||
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>
|
||||
) => {
|
||||
const { id, value } = e.target;
|
||||
setFormData((prev) => ({ ...prev, [id]: value }));
|
||||
|
||||
if (errors[id as keyof SignInWithOtp]) {
|
||||
setErrors((prev) => {
|
||||
const newErrors = { ...prev };
|
||||
delete newErrors[id as keyof SignInWithOtp];
|
||||
return newErrors;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// Handle select change
|
||||
const handleSelectChange = (value: string) => {
|
||||
setFormData((prev) => ({ ...prev, typeMessage: value }));
|
||||
|
||||
// Clear error when selecting
|
||||
if (errors.email) {
|
||||
setErrors((prev) => {
|
||||
const newErrors = { ...prev };
|
||||
delete newErrors.email;
|
||||
return newErrors;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
|
||||
e.preventDefault();
|
||||
const validation = validateSignInWithOtp(formData);
|
||||
if (!validation.success) {
|
||||
setErrors(validation.errors);
|
||||
return;
|
||||
}
|
||||
|
||||
setIsSubmitting(true);
|
||||
try {
|
||||
const response = await contactRepository.signInWithPasswordless(formData);
|
||||
|
||||
if (!response.success) {
|
||||
if (response.errors) {
|
||||
setErrors(response.errors);
|
||||
} else {
|
||||
toast({
|
||||
title: "Error",
|
||||
description: response.error || "An unexpected error occurred.",
|
||||
variant: "destructive",
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
// Add redirect handling
|
||||
if (response.redirectTo) {
|
||||
router.push(response.redirectTo);
|
||||
}
|
||||
|
||||
toast({
|
||||
title: "Success",
|
||||
description:
|
||||
response.message || `OTP has been sent to ${formData.email}`,
|
||||
});
|
||||
|
||||
setFormData({
|
||||
email: "",
|
||||
});
|
||||
setErrors({});
|
||||
} catch (error) {
|
||||
|
||||
toast({
|
||||
title: "Error",
|
||||
description: "An unexpected error occurred. Please try again later.",
|
||||
variant: "destructive",
|
||||
});
|
||||
} finally {
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
formData,
|
||||
errors,
|
||||
isSubmitting,
|
||||
setFormData,
|
||||
handleChange,
|
||||
handleSelectChange,
|
||||
handleSubmit,
|
||||
};
|
||||
};
|
|
@ -0,0 +1,23 @@
|
|||
import { signInAction } from "@/actions/auth/sign-in";
|
||||
import {
|
||||
SignInResponse,
|
||||
SignInWithOtp,
|
||||
} from "@/src/applications/entities/models/user.model";
|
||||
import { SignInRepository } from "@/src/applications/repositories/signin.repository";
|
||||
import Router from "next/router";
|
||||
|
||||
export class SignInRepositoryImpl implements SignInRepository {
|
||||
async signInWithPasswordless(email: SignInWithOtp): Promise<SignInResponse> {
|
||||
try {
|
||||
const result = await signInAction(email);
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
console.error("Error in SignInRepositoryImpl:", error);
|
||||
return {
|
||||
success: false,
|
||||
error: "An unexpected error occurred. Please try again later.",
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
import { ContactUsInsert } from "@/src/applications/entities/models/contact-us.model";
|
||||
import { SignInWithOtp } from "@/src/applications/entities/models/user.model";
|
||||
import { TValidator } from "@/utils/validator";
|
||||
|
||||
/**
|
||||
* Validate the contact form
|
||||
* @param {Object} formData - The form data to validate
|
||||
* @returns {Object} - Validation result with success flag and errors object
|
||||
*/
|
||||
export const validateSignInWithOtp = (
|
||||
formData: SignInWithOtp
|
||||
): { success: boolean; errors: Record<string, string> } => {
|
||||
const errors: Record<string, string> = {};
|
||||
let isValid = true;
|
||||
|
||||
// Validate email
|
||||
const emailResult = TValidator.validateEmail(formData.email);
|
||||
if (!emailResult.success) {
|
||||
errors.email = emailResult.error!;
|
||||
isValid = false;
|
||||
}
|
||||
|
||||
return {
|
||||
success: isValid,
|
||||
errors: isValid ? {} : errors,
|
||||
};
|
||||
};
|
Loading…
Reference in New Issue