mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-04-20 03:50:20 +00:00
refactor: update authentication flow to use PIN instead of email/password, enhance OTP verification with NextAuth, and improve session handling in components
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 6m26s
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 6m26s
This commit is contained in:
parent
ef9f032366
commit
dbdc1df7d5
@ -23,6 +23,7 @@ export default async function Devices({
|
|||||||
<h3 className="text-sarLinkOrange text-2xl">My Devices</h3>
|
<h3 className="text-sarLinkOrange text-2xl">My Devices</h3>
|
||||||
<AddDeviceDialogForm user_id={session?.user?.id} />
|
<AddDeviceDialogForm user_id={session?.user?.id} />
|
||||||
</div>
|
</div>
|
||||||
|
<pre>{JSON.stringify(session, null, 2)}</pre>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
id="user-filters"
|
id="user-filters"
|
||||||
|
18
app/auth.ts
18
app/auth.ts
@ -22,28 +22,26 @@ export const authOptions: NextAuthOptions = {
|
|||||||
CredentialsProvider({
|
CredentialsProvider({
|
||||||
name: "Credentials",
|
name: "Credentials",
|
||||||
credentials: {
|
credentials: {
|
||||||
email: { label: "Email", type: "text", placeholder: "jsmith" },
|
pin: { label: "Pin", type: "text", placeholder: "000000" },
|
||||||
password: { label: "Password", type: "password" },
|
|
||||||
},
|
},
|
||||||
async authorize(credentials) {
|
async authorize(credentials) {
|
||||||
const { email, password } = credentials as {
|
const { pin } = credentials as {
|
||||||
email: string;
|
pin: string;
|
||||||
password: string;
|
|
||||||
};
|
};
|
||||||
console.log("email and password", email, password);
|
console.log("pin", pin);
|
||||||
const res = await fetch(
|
const res = await fetch(
|
||||||
`${process.env.NEXT_PUBLIC_API_URL}/auth/login/`,
|
`${process.env.SARLINK_API_BASE_URL}/callback/auth/`,
|
||||||
{
|
{
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
username: email,
|
token: pin,
|
||||||
password: password,
|
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
console.log(res);
|
||||||
console.log("status", res.status);
|
console.log("status", res.status);
|
||||||
|
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
@ -53,7 +51,7 @@ export const authOptions: NextAuthOptions = {
|
|||||||
return { ...data.user, apiToken: data.token, expiry: data.expiry };
|
return { ...data.user, apiToken: data.token, expiry: data.expiry };
|
||||||
case 400:
|
case 400:
|
||||||
throw new Error(
|
throw new Error(
|
||||||
JSON.stringify({ message: data.message, status: res.status }),
|
JSON.stringify({ message: data.token[0], status: res.status }),
|
||||||
);
|
);
|
||||||
case 429:
|
case 429:
|
||||||
throw new Error(
|
throw new Error(
|
||||||
|
@ -22,12 +22,10 @@ import type { ApiResponse, Atoll } from "@/lib/backend-types";
|
|||||||
import { getAtolls } from "@/queries/islands";
|
import { getAtolls } from "@/queries/islands";
|
||||||
import { keepPreviousData, useQuery } from "@tanstack/react-query";
|
import { keepPreviousData, useQuery } from "@tanstack/react-query";
|
||||||
|
|
||||||
|
|
||||||
export default function SignUpForm() {
|
export default function SignUpForm() {
|
||||||
const { data: atolls, isFetching } = useQuery<ApiResponse<Atoll>>({
|
const { data: atolls, isFetching } = useQuery<ApiResponse<Atoll>>({
|
||||||
queryKey: ["ATOLLS"],
|
queryKey: ["ATOLLS"],
|
||||||
queryFn: () =>
|
queryFn: () => getAtolls(),
|
||||||
getAtolls(),
|
|
||||||
placeholderData: keepPreviousData,
|
placeholderData: keepPreviousData,
|
||||||
staleTime: 1,
|
staleTime: 1,
|
||||||
});
|
});
|
||||||
@ -38,29 +36,28 @@ export default function SignUpForm() {
|
|||||||
message: "",
|
message: "",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
console.log(atoll)
|
console.log(atoll);
|
||||||
}, [atoll]);
|
}, [atoll]);
|
||||||
|
|
||||||
|
|
||||||
const params = useSearchParams();
|
const params = useSearchParams();
|
||||||
const phoneNumberFromUrl = params.get("phone_number");
|
const phoneNumberFromUrl = params.get("phone_number");
|
||||||
const NUMBER_WITHOUT_DASH = phoneNumberFromUrl?.split("-").join("");
|
const NUMBER_WITHOUT_DASH = phoneNumberFromUrl?.split("-").join("");
|
||||||
|
|
||||||
|
|
||||||
if (actionState?.db_error === "invalidPersonValidation") {
|
if (actionState?.db_error === "invalidPersonValidation") {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="h-24 w-72 text-center text-green-500 p-4 flex my-4 flex-col items-center justify-center border dark:title-bg bg-white dark:bg-black rounded-lg">{actionState.message}</div>
|
<div className="h-24 w-72 text-center text-green-500 p-4 flex my-4 flex-col items-center justify-center border dark:title-bg bg-white dark:bg-black rounded-lg">
|
||||||
|
{actionState.message}
|
||||||
|
</div>
|
||||||
<div className="mb-4 text-center text-sm">
|
<div className="mb-4 text-center text-sm">
|
||||||
Go to {" "}
|
Go to{" "}
|
||||||
<Link href="login" className="underline">
|
<Link href="login" className="underline">
|
||||||
login
|
login
|
||||||
</Link>
|
</Link>
|
||||||
</div>
|
</div>
|
||||||
</>
|
</>
|
||||||
)
|
);
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
<form
|
<form
|
||||||
@ -76,7 +73,8 @@ export default function SignUpForm() {
|
|||||||
<Input
|
<Input
|
||||||
className={cn(
|
className={cn(
|
||||||
"text-base",
|
"text-base",
|
||||||
actionState?.errors?.fieldErrors.name && "border-2 border-red-500",
|
actionState?.errors?.fieldErrors.name &&
|
||||||
|
"border-2 border-red-500",
|
||||||
)}
|
)}
|
||||||
name="name"
|
name="name"
|
||||||
type="text"
|
type="text"
|
||||||
@ -99,11 +97,13 @@ export default function SignUpForm() {
|
|||||||
type="text"
|
type="text"
|
||||||
maxLength={7}
|
maxLength={7}
|
||||||
disabled={isPending}
|
disabled={isPending}
|
||||||
defaultValue={(actionState?.payload?.get("id_card") || "") as string}
|
defaultValue={
|
||||||
|
(actionState?.payload?.get("id_card") || "") as string
|
||||||
|
}
|
||||||
className={cn(
|
className={cn(
|
||||||
"text-base",
|
"text-base",
|
||||||
actionState?.errors?.fieldErrors?.id_card &&
|
actionState?.errors?.fieldErrors?.id_card &&
|
||||||
"border-2 border-red-500",
|
"border-2 border-red-500",
|
||||||
)}
|
)}
|
||||||
placeholder="ID Card"
|
placeholder="ID Card"
|
||||||
/>
|
/>
|
||||||
@ -126,8 +126,10 @@ export default function SignUpForm() {
|
|||||||
<Select
|
<Select
|
||||||
disabled={isPending}
|
disabled={isPending}
|
||||||
onValueChange={(v) => {
|
onValueChange={(v) => {
|
||||||
console.log({ v })
|
console.log({ v });
|
||||||
setAtoll(atolls?.data.find((atoll) => atoll.id === Number.parseInt(v)));
|
setAtoll(
|
||||||
|
atolls?.data.find((atoll) => atoll.id === Number.parseInt(v)),
|
||||||
|
);
|
||||||
}}
|
}}
|
||||||
name="atoll_id"
|
name="atoll_id"
|
||||||
value={atoll?.id?.toString() ?? ""}
|
value={atoll?.id?.toString() ?? ""}
|
||||||
@ -187,7 +189,7 @@ export default function SignUpForm() {
|
|||||||
className={cn(
|
className={cn(
|
||||||
"text-base",
|
"text-base",
|
||||||
actionState?.errors?.fieldErrors?.address &&
|
actionState?.errors?.fieldErrors?.address &&
|
||||||
"border-2 border-red-500",
|
"border-2 border-red-500",
|
||||||
)}
|
)}
|
||||||
disabled={isPending}
|
disabled={isPending}
|
||||||
name="address"
|
name="address"
|
||||||
@ -211,7 +213,8 @@ export default function SignUpForm() {
|
|||||||
<Input
|
<Input
|
||||||
className={cn(
|
className={cn(
|
||||||
"text-base",
|
"text-base",
|
||||||
actionState?.errors?.fieldErrors?.dob && "border-2 border-red-500",
|
actionState?.errors?.fieldErrors?.dob &&
|
||||||
|
"border-2 border-red-500",
|
||||||
)}
|
)}
|
||||||
name="dob"
|
name="dob"
|
||||||
disabled={isPending}
|
disabled={isPending}
|
||||||
@ -233,7 +236,8 @@ export default function SignUpForm() {
|
|||||||
<Input
|
<Input
|
||||||
className={cn(
|
className={cn(
|
||||||
"text-base",
|
"text-base",
|
||||||
actionState?.errors?.fieldErrors.accNo && "border-2 border-red-500",
|
actionState?.errors?.fieldErrors.accNo &&
|
||||||
|
"border-2 border-red-500",
|
||||||
)}
|
)}
|
||||||
name="accNo"
|
name="accNo"
|
||||||
type="number"
|
type="number"
|
||||||
@ -258,8 +262,8 @@ export default function SignUpForm() {
|
|||||||
disabled={isPending}
|
disabled={isPending}
|
||||||
className={cn(
|
className={cn(
|
||||||
!phoneNumberFromUrl &&
|
!phoneNumberFromUrl &&
|
||||||
actionState?.errors?.fieldErrors?.phone_number &&
|
actionState?.errors?.fieldErrors?.phone_number &&
|
||||||
"border-2 border-red-500 rounded-md",
|
"border-2 border-red-500 rounded-md",
|
||||||
)}
|
)}
|
||||||
defaultValue={NUMBER_WITHOUT_DASH ?? ""}
|
defaultValue={NUMBER_WITHOUT_DASH ?? ""}
|
||||||
readOnly={Boolean(phoneNumberFromUrl)}
|
readOnly={Boolean(phoneNumberFromUrl)}
|
||||||
@ -280,15 +284,17 @@ export default function SignUpForm() {
|
|||||||
<div className="flex gap-2 items-center">
|
<div className="flex gap-2 items-center">
|
||||||
<input
|
<input
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
defaultChecked={(actionState?.payload?.get("terms") || "") as string === 'on'}
|
defaultChecked={
|
||||||
name="terms" id="terms" />
|
((actionState?.payload?.get("terms") || "") as string) === "on"
|
||||||
|
}
|
||||||
|
name="terms"
|
||||||
|
id="terms"
|
||||||
|
/>
|
||||||
<label
|
<label
|
||||||
htmlFor="terms"
|
htmlFor="terms"
|
||||||
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
|
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
|
||||||
>
|
>
|
||||||
<span>
|
<span>i accept</span>
|
||||||
i accept
|
|
||||||
</span>
|
|
||||||
<Link className="ml-1 underline" href="">
|
<Link className="ml-1 underline" href="">
|
||||||
terms and conditions
|
terms and conditions
|
||||||
</Link>
|
</Link>
|
||||||
@ -300,18 +306,19 @@ export default function SignUpForm() {
|
|||||||
</span>
|
</span>
|
||||||
)}
|
)}
|
||||||
<div className="flex gap-2 items-center">
|
<div className="flex gap-2 items-center">
|
||||||
|
|
||||||
<input
|
<input
|
||||||
type="checkbox"
|
type="checkbox"
|
||||||
defaultChecked={(actionState?.payload?.get("policy") || "") as string === 'on'}
|
defaultChecked={
|
||||||
name="policy" id="terms" />
|
((actionState?.payload?.get("policy") || "") as string) === "on"
|
||||||
|
}
|
||||||
|
name="policy"
|
||||||
|
id="terms"
|
||||||
|
/>
|
||||||
<label
|
<label
|
||||||
htmlFor="terms"
|
htmlFor="terms"
|
||||||
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
|
className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
|
||||||
>
|
>
|
||||||
<span>
|
<span>i undertand</span>
|
||||||
i undertand
|
|
||||||
</span>
|
|
||||||
<Link className="ml-1 underline" href="">
|
<Link className="ml-1 underline" href="">
|
||||||
the privacy policy
|
the privacy policy
|
||||||
</Link>
|
</Link>
|
||||||
@ -322,7 +329,6 @@ export default function SignUpForm() {
|
|||||||
{actionState?.errors?.fieldErrors?.policy}
|
{actionState?.errors?.fieldErrors?.policy}
|
||||||
</span>
|
</span>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
<Button disabled={isPending} className="mt-4 w-full" type="submit">
|
<Button disabled={isPending} className="mt-4 w-full" type="submit">
|
||||||
{isPending ? <Loader2 className="animate-spin" /> : "Submit"}
|
{isPending ? <Loader2 className="animate-spin" /> : "Submit"}
|
||||||
@ -331,7 +337,7 @@ export default function SignUpForm() {
|
|||||||
|
|
||||||
<div className="mb-4 text-center text-sm">
|
<div className="mb-4 text-center text-sm">
|
||||||
Already have an account?{" "}
|
Already have an account?{" "}
|
||||||
<Link href="login" className="underline">
|
<Link href="signin" className="underline">
|
||||||
login
|
login
|
||||||
</Link>
|
</Link>
|
||||||
</div>
|
</div>
|
||||||
|
@ -5,8 +5,9 @@ import { Input } from "@/components/ui/input";
|
|||||||
import { Label } from "@/components/ui/label";
|
import { Label } from "@/components/ui/label";
|
||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
import { Loader2 } from "lucide-react";
|
import { Loader2 } from "lucide-react";
|
||||||
|
import { signIn } from "next-auth/react";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter, useSearchParams } from "next/navigation";
|
||||||
import { useTransition } from "react";
|
import { useTransition } from "react";
|
||||||
import { type SubmitHandler, useForm } from "react-hook-form";
|
import { type SubmitHandler, useForm } from "react-hook-form";
|
||||||
import { toast } from "sonner";
|
import { toast } from "sonner";
|
||||||
@ -33,22 +34,26 @@ export default function VerifyOTPForm({
|
|||||||
},
|
},
|
||||||
resolver: zodResolver(OTPSchema),
|
resolver: zodResolver(OTPSchema),
|
||||||
});
|
});
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
const callbackUrl = searchParams.get("callbackUrl") || "/dashboard";
|
||||||
|
|
||||||
const onSubmit: SubmitHandler<z.infer<typeof OTPSchema>> = (data) => {
|
const onSubmit: SubmitHandler<z.infer<typeof OTPSchema>> = (data) => {
|
||||||
startTransition(async () => {
|
startTransition(async () => {
|
||||||
// const isVerified = await authClient.phoneNumber.verify({
|
const nextAuth = await signIn("credentials", {
|
||||||
// phoneNumber: phone_number,
|
pin: data.pin,
|
||||||
// code: data.pin,
|
callbackUrl,
|
||||||
// });
|
redirect: false,
|
||||||
// console.log({ isVerified });
|
});
|
||||||
// if (!isVerified.error) {
|
if (!nextAuth?.error) {
|
||||||
// router.push("/devices");
|
router.push("/devices");
|
||||||
// } else {
|
} else {
|
||||||
// toast.error(isVerified.error.message);
|
toast.error(JSON.parse(nextAuth?.error ?? "").message);
|
||||||
// }
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// "{\"message\":\"The token you entered isn't valid.\",\"status\":400}"
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<form
|
<form
|
||||||
onSubmit={handleSubmit(onSubmit)}
|
onSubmit={handleSubmit(onSubmit)}
|
||||||
@ -75,7 +80,7 @@ export default function VerifyOTPForm({
|
|||||||
</div>
|
</div>
|
||||||
<div className="mb-4 text-center text-sm">
|
<div className="mb-4 text-center text-sm">
|
||||||
Go back to{" "}
|
Go back to{" "}
|
||||||
<Link href="login" className="underline">
|
<Link href="signin" className="underline">
|
||||||
login
|
login
|
||||||
</Link>
|
</Link>
|
||||||
</div>
|
</div>
|
||||||
|
@ -6,5 +6,5 @@ export default withAuth(
|
|||||||
);
|
);
|
||||||
|
|
||||||
export const config = {
|
export const config = {
|
||||||
matcher: ["/about/:path*", "/dashboard/:path*"],
|
matcher: ["/about/:path*", "/dashboard/:path*", "/devices/:path*"],
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user