xephyr
xephyr
BABetter Auth
Created by shu-sin on 4/6/2025 in #bug-reports
Generic oauth failing on missing email
Thanks for the clarification. From my perspective it isn't necessarily a need for email to be optional (though perhaps it is a need for other people). For me it is that I'd like authClient.signIn.oauth2()/auth.api.oauth2 to return a "partially complete" status with some identifier/token if: 1. The OAuth login was successful 2. The OAuth response did not include email This way we could handle requiring an email from the user, and then we could call something like authClient.signIn.resumeOauth2({ resumeToken, email }); to fully complete the auth. The fact it's required means we have to set a fake email as their email, then add a bunch of client code to block users from doing anything on the site until they give us their email (which people will try to avoid), etc.
20 replies
BABetter Auth
Created by shu-sin on 4/6/2025 in #bug-reports
Generic oauth failing on missing email
A genericOAuth2 provider not returning email, it doesn't look like its possible with the base library anyway but I thought I'd ask since if better-auth-ui found a way to handle it we could look at that
20 replies
BABetter Auth
Created by shu-sin on 4/6/2025 in #bug-reports
Generic oauth failing on missing email
would prefer it to be baked in to better-auth but for now it should do the job
20 replies
BABetter Auth
Created by shu-sin on 4/6/2025 in #bug-reports
Generic oauth failing on missing email
yeah my temporary solution is probably going to be email: fakeemail+{accountId}@whatever.com emailVerified: false and redirect to some UI to accept an email, and show a non-dismissible dialog asking for email on any other page if they try and circumvent it
20 replies
BABetter Auth
Created by shu-sin on 4/6/2025 in #bug-reports
Generic oauth failing on missing email
@daveycodez Sorry for the unexpected ping, but looking through the code for better-auth-ui I don't see anything that would handle this state on your side. Is that correct?
20 replies
BABetter Auth
Created by shu-sin on 4/6/2025 in #bug-reports
Generic oauth failing on missing email
If we insist on email being required, this UX would be acceptable to me. However since better-auth is headless, some API by which we can intercept the failure state => display a form of our choosing to the user => and subsequently resume the OAuth user creation flow would be ideal.
20 replies
BABetter Auth
Created by shu-sin on 4/6/2025 in #bug-reports
Generic oauth failing on missing email
No description
20 replies
BABetter Auth
Created by shu-sin on 4/6/2025 in #bug-reports
Generic oauth failing on missing email
Bumping this, email being required is a frustrating limitation.
20 replies
BABetter Auth
Created by Henrik on 4/15/2025 in #help
Next.js server actions auth
@KiNFiSH I've refactored it to use authClient now, but I'm still curious why the server action wasn't working so I've recreated it. Let me know what you think 🙂
export const signInEmailSchema = z.object({
email: z.string().email({ message: "Invalid email address" }),
password: z.string(),
});

export const SignInForm: FC<ComponentPropsWithoutRef<"div">> = ({
className,
...props
}) => {
const signInEmail = async (prevState: unknown, formData: FormData) => {
"use server";

const submission = parseWithZod(formData, {
schema: signInEmailSchema,
});
if (submission.status !== "success") return submission.reply();

try {
await auth.api.signInEmail({
body: submission.value,
});
} catch (error) {
if (error instanceof APIError) {
return submission.reply({
formErrors: [error.message],
});
}
}

redirect("/");
};
const [state, formAction, pending] = useActionState(signInEmail, undefined);
const [form, fields] = useForm({
lastResult: state,
onValidate({ formData }) {
return parseWithZod(formData, { schema: signInEmailSchema });
},
shouldValidate: "onInput",
});

return (
<form action={formAction} id={form.id} noValidate onSubmit={form.onSubmit}>
...
</form>
);
};
export const signInEmailSchema = z.object({
email: z.string().email({ message: "Invalid email address" }),
password: z.string(),
});

export const SignInForm: FC<ComponentPropsWithoutRef<"div">> = ({
className,
...props
}) => {
const signInEmail = async (prevState: unknown, formData: FormData) => {
"use server";

const submission = parseWithZod(formData, {
schema: signInEmailSchema,
});
if (submission.status !== "success") return submission.reply();

try {
await auth.api.signInEmail({
body: submission.value,
});
} catch (error) {
if (error instanceof APIError) {
return submission.reply({
formErrors: [error.message],
});
}
}

redirect("/");
};
const [state, formAction, pending] = useActionState(signInEmail, undefined);
const [form, fields] = useForm({
lastResult: state,
onValidate({ formData }) {
return parseWithZod(formData, { schema: signInEmailSchema });
},
shouldValidate: "onInput",
});

return (
<form action={formAction} id={form.id} noValidate onSubmit={form.onSubmit}>
...
</form>
);
};
21 replies
BABetter Auth
Created by Henrik on 4/15/2025 in #help
Next.js server actions auth
(I've switched to using authClient instead, ditched useActionState with server actions)
21 replies
BABetter Auth
Created by Henrik on 4/15/2025 in #help
Next.js server actions auth
at least not for me
21 replies
BABetter Auth
Created by Henrik on 4/15/2025 in #help
Next.js server actions auth
the session doesn't update though
21 replies
BABetter Auth
Created by Henrik on 4/15/2025 in #help
Next.js server actions auth
i'm not sure where the cookies are supposed to be set
21 replies
BABetter Auth
Created by Henrik on 4/15/2025 in #help
Next.js server actions auth
so the server action either returns a conform error response or redirects
21 replies
BABetter Auth
Created by Henrik on 4/15/2025 in #help
Next.js server actions auth
using Conform on the client to manage the form error states
21 replies
BABetter Auth
Created by Henrik on 4/15/2025 in #help
Next.js server actions auth
21 replies
BABetter Auth
Created by Henrik on 4/15/2025 in #help
Next.js server actions auth
I think I'm misunderstanding something about how it's meant to set cookies, because I'm not using asResponse and returning it because it's in useActionState so it can't return Response
21 replies
BABetter Auth
Created by Henrik on 4/15/2025 in #help
Next.js server actions auth
21 replies
BABetter Auth
Created by xephyr on 4/15/2025 in #help
Anyone using auth.api in a server action with useActionState?
FWIW here's the action:
"use server";

import { APIError } from "better-auth/api";
import { redirect } from "next/navigation";
import { parseWithZod } from "@conform-to/zod";
import { auth } from "@/lib/auth";
import { signInEmailSchema } from "./schema";

export const signInEmail = async (prevState: unknown, formData: FormData) => {
const submission = parseWithZod(formData, { schema: signInEmailSchema });
if (submission.status !== "success") return submission.reply();

try {
await auth.api.signInEmail({
body: submission.value,
});
} catch (error) {
if (error instanceof APIError) {
return submission.reply({
formErrors: [error.message],
});
}
}

redirect("/");
};
"use server";

import { APIError } from "better-auth/api";
import { redirect } from "next/navigation";
import { parseWithZod } from "@conform-to/zod";
import { auth } from "@/lib/auth";
import { signInEmailSchema } from "./schema";

export const signInEmail = async (prevState: unknown, formData: FormData) => {
const submission = parseWithZod(formData, { schema: signInEmailSchema });
if (submission.status !== "success") return submission.reply();

try {
await auth.api.signInEmail({
body: submission.value,
});
} catch (error) {
if (error instanceof APIError) {
return submission.reply({
formErrors: [error.message],
});
}
}

redirect("/");
};
3 replies