fix(auth): migrate from next-auth to better-auth (#1453)
This commit is contained in:
parent
453f5bb3e6
commit
b4f595a68c
40 changed files with 668 additions and 390 deletions
|
|
@ -1,10 +1,7 @@
|
|||
"use server";
|
||||
|
||||
import { z } from "zod";
|
||||
|
||||
import { createUser, getUser } from "@/lib/db/queries";
|
||||
|
||||
import { signIn } from "./auth";
|
||||
import { auth } from "@/lib/auth";
|
||||
|
||||
const authFormSchema = z.object({
|
||||
email: z.string().email(),
|
||||
|
|
@ -25,10 +22,11 @@ export const login = async (
|
|||
password: formData.get("password"),
|
||||
});
|
||||
|
||||
await signIn("credentials", {
|
||||
email: validatedData.email,
|
||||
password: validatedData.password,
|
||||
redirect: false,
|
||||
await auth.api.signInEmail({
|
||||
body: {
|
||||
email: validatedData.email,
|
||||
password: validatedData.password,
|
||||
},
|
||||
});
|
||||
|
||||
return { status: "success" };
|
||||
|
|
@ -61,24 +59,29 @@ export const register = async (
|
|||
password: formData.get("password"),
|
||||
});
|
||||
|
||||
const [user] = await getUser(validatedData.email);
|
||||
|
||||
if (user) {
|
||||
return { status: "user_exists" } as RegisterActionState;
|
||||
}
|
||||
await createUser(validatedData.email, validatedData.password);
|
||||
await signIn("credentials", {
|
||||
email: validatedData.email,
|
||||
password: validatedData.password,
|
||||
redirect: false,
|
||||
const result = await auth.api.signUpEmail({
|
||||
body: {
|
||||
email: validatedData.email,
|
||||
password: validatedData.password,
|
||||
name: validatedData.email,
|
||||
},
|
||||
});
|
||||
|
||||
if (!result) {
|
||||
return { status: "failed" };
|
||||
}
|
||||
|
||||
return { status: "success" };
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return { status: "invalid_data" };
|
||||
}
|
||||
|
||||
const message = error instanceof Error ? error.message : "";
|
||||
if (message.includes("already exists") || message.includes("UNIQUE")) {
|
||||
return { status: "user_exists" };
|
||||
}
|
||||
|
||||
return { status: "failed" };
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue