fix: simplify auth client and use window.location for redirects

This commit is contained in:
Ajax Davis 2026-01-01 23:33:44 +10:00
parent 5d4d25c8ad
commit 260107c52a
3 changed files with 36 additions and 39 deletions

View file

@ -18,28 +18,25 @@ export default function SignInPage() {
setLoading(true);
try {
await signIn.email(
{
email,
password,
callbackURL: '/dashboard',
},
{
onSuccess: () => {
router.refresh();
router.push('/dashboard');
},
onError: (ctx) => {
console.error('Sign in error:', ctx.error);
setError(ctx.error.message || 'Failed to sign in');
},
}
);
return; // onSuccess/onError handle the flow
const { data, error } = await signIn.email({
email,
password,
});
if (error) {
console.error('Sign in error:', error);
setError(error.message || 'Failed to sign in');
setLoading(false);
return;
}
if (data) {
// Successfully signed in - redirect to dashboard
window.location.href = '/dashboard';
}
} catch (err) {
console.error('Sign in exception:', err);
setError('An unexpected error occurred');
} finally {
setLoading(false);
}
}

View file

@ -19,27 +19,25 @@ export default function SignUpPage() {
setLoading(true);
try {
await signUp.email(
{
name,
email,
password,
callbackURL: '/verify-email',
},
{
onSuccess: () => {
router.push('/verify-email');
},
onError: (ctx) => {
console.error('Sign up error:', ctx.error);
setError(ctx.error.message || 'Failed to create account');
},
}
);
return; // onSuccess/onError handle the flow
const { data, error } = await signUp.email({
name,
email,
password,
});
if (error) {
console.error('Sign up error:', error);
setError(error.message || 'Failed to create account');
setLoading(false);
return;
}
if (data) {
// Successfully signed up - redirect to verify email page
window.location.href = '/verify-email';
}
} catch {
setError('An unexpected error occurred');
} finally {
setLoading(false);
}
}

View file

@ -2,6 +2,8 @@
import { createAuthClient } from 'better-auth/react';
export const authClient = createAuthClient();
export const authClient = createAuthClient({
baseURL: typeof window !== 'undefined' ? window.location.origin : undefined,
});
export const { signIn, signUp, signOut, useSession } = authClient;