fix: set better auth baseUrl

This commit is contained in:
Henry
2025-05-06 10:38:35 +01:00
parent 69004df19e
commit 414af74dc1
3 changed files with 41 additions and 28 deletions

View File

@@ -1,4 +1,5 @@
import { zodResolver } from "@hookform/resolvers/zod"; import { zodResolver } from "@hookform/resolvers/zod";
import { useState } from "react";
import { useForm } from "react-hook-form"; import { useForm } from "react-hook-form";
import { FaGoogle } from "react-icons/fa"; import { FaGoogle } from "react-icons/fa";
import { z } from "zod"; import { z } from "zod";
@@ -7,7 +8,6 @@ import { authClient } from "@kan/auth";
import Button from "~/components/Button"; import Button from "~/components/Button";
import Input from "~/components/Input"; import Input from "~/components/Input";
import { api } from "~/utils/api";
interface FormValues { interface FormValues {
email: string; email: string;
@@ -20,34 +20,53 @@ interface AuthProps {
const EmailSchema = z.object({ email: z.string().email() }); const EmailSchema = z.object({ email: z.string().email() });
export function Auth({ setIsMagicLinkSent }: AuthProps) { export function Auth({ setIsMagicLinkSent }: AuthProps) {
const [isLoginWithGooglePending, setIsLoginWithGooglePending] =
useState(false);
const [isLoginWithEmailPending, setIsLoginWithEmailPending] = useState(false);
const [loginError, setLoginError] = useState<string | null>(null);
const { const {
register, register,
handleSubmit, handleSubmit,
watch,
formState: { errors }, formState: { errors },
} = useForm<FormValues>({ } = useForm<FormValues>({
resolver: zodResolver(EmailSchema), resolver: zodResolver(EmailSchema),
}); });
const email = watch("email");
const loginWithEmail = api.auth.loginWithEmail.useMutation({
onSuccess: () => {
setIsMagicLinkSent(true, email);
},
});
const handleLoginWithEmail = async (email: string) => { const handleLoginWithEmail = async (email: string) => {
const { data, error } = await authClient.signIn.magicLink({ setIsLoginWithEmailPending(true);
setLoginError(null);
const { error } = await authClient.signIn.magicLink({
email, email,
callbackURL: "/boards", callbackURL: "/boards",
}); });
if (!error) { setIsLoginWithEmailPending(false);
if (error) {
setLoginError(
"Something went wrong, please try again later or contact customer support.",
);
} else {
setIsMagicLinkSent(true, email); setIsMagicLinkSent(true, email);
} }
}; };
const handleLoginWithGoogle = async () => {
setIsLoginWithGooglePending(true);
setLoginError(null);
const { error } = await authClient.signIn.social({
provider: "google",
callbackURL: "/boards",
});
setIsLoginWithGooglePending(false);
if (error) {
setLoginError("Failed to login with Google. Please try again.");
}
};
const onSubmit = async (values: FormValues) => { const onSubmit = async (values: FormValues) => {
await handleLoginWithEmail(values.email); await handleLoginWithEmail(values.email);
}; };
@@ -56,12 +75,8 @@ export function Auth({ setIsMagicLinkSent }: AuthProps) {
<div className="space-y-6"> <div className="space-y-6">
<div> <div>
<Button <Button
onClick={() => onClick={handleLoginWithGoogle}
authClient.signIn.social({ isLoading={isLoginWithGooglePending}
provider: "google",
callbackURL: "/boards",
})
}
iconLeft={<FaGoogle />} iconLeft={<FaGoogle />}
fullWidth fullWidth
size="lg" size="lg"
@@ -79,21 +94,17 @@ export function Auth({ setIsMagicLinkSent }: AuthProps) {
{...register("email", { required: true })} {...register("email", { required: true })}
placeholder="Enter your email address" placeholder="Enter your email address"
/> />
{!loginWithEmail.error && errors.email && ( {errors.email && (
<p className="mt-2 text-xs text-red-400"> <p className="mt-2 text-xs text-red-400">
Please enter a valid email address Please enter a valid email address
</p> </p>
)} )}
{loginWithEmail.error ? ( {loginError && (
<p className="mt-2 text-xs text-red-400"> <p className="mt-2 text-xs text-red-400">{loginError}</p>
Something went wrong, please try again later or contact customer )}
support.
</p>
) : null}
<div className="mt-[1.5rem]"> <div className="mt-[1.5rem]">
<Button <Button
isLoading={loginWithEmail.isPending} isLoading={isLoginWithEmailPending}
fullWidth fullWidth
size="lg" size="lg"
variant="secondary" variant="secondary"

View File

@@ -13,6 +13,7 @@ import { createStripeClient } from "@kan/stripe";
const db = createDrizzleClient(); const db = createDrizzleClient();
export const auth = betterAuth({ export const auth = betterAuth({
secret: process.env.BETTER_AUTH_SECRET!, secret: process.env.BETTER_AUTH_SECRET!,
baseURL: process.env.BETTER_AUTH_BASE_URL!,
database: drizzleAdapter(db, { database: drizzleAdapter(db, {
provider: "pg", provider: "pg",
schema: { schema: {

View File

@@ -62,7 +62,8 @@
"S3_SECRET_ACCESS_KEY", "S3_SECRET_ACCESS_KEY",
"S3_ENDPOINT", "S3_ENDPOINT",
"PORT", "PORT",
"BETTER_AUTH_SECRET" "BETTER_AUTH_SECRET",
"BETTER_AUTH_URL"
], ],
"globalPassThroughEnv": [ "globalPassThroughEnv": [
"NODE_ENV", "NODE_ENV",