fix: use callbackURL and onSuccess/onError for Better Auth sign-in/sign-up
This commit is contained in:
parent
1d417d9a22
commit
de41d42349
2 changed files with 38 additions and 26 deletions
|
|
@ -18,19 +18,26 @@ export default function SignInPage() {
|
|||
setLoading(true);
|
||||
|
||||
try {
|
||||
const result = await signIn.email({
|
||||
email,
|
||||
password,
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
console.error('Sign in error:', result.error);
|
||||
setError(result.error.message || result.error.code || 'Failed to sign in');
|
||||
return;
|
||||
}
|
||||
|
||||
router.push('/dashboard');
|
||||
} catch {
|
||||
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
|
||||
} catch (err) {
|
||||
console.error('Sign in exception:', err);
|
||||
setError('An unexpected error occurred');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
|
|
|
|||
|
|
@ -19,19 +19,24 @@ export default function SignUpPage() {
|
|||
setLoading(true);
|
||||
|
||||
try {
|
||||
const result = await signUp.email({
|
||||
name,
|
||||
email,
|
||||
password,
|
||||
});
|
||||
|
||||
if (result.error) {
|
||||
console.error('Sign up error:', result.error);
|
||||
setError(result.error.message || result.error.code || 'Failed to create account');
|
||||
return;
|
||||
}
|
||||
|
||||
router.push('/verify-email');
|
||||
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
|
||||
} catch {
|
||||
setError('An unexpected error occurred');
|
||||
} finally {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue