fix: simplify auth client and use window.location for redirects
This commit is contained in:
parent
5d4d25c8ad
commit
260107c52a
3 changed files with 36 additions and 39 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue