fix: improve auth sign-in handling and cookie configuration
- Add onSuccess/onError callbacks to signIn.email() for better response handling - Add trustedOrigins configuration for production domain - Configure explicit cookie attributes (sameSite, secure, httpOnly) - Enable session cookie caching for better performance - Handle edge case where signIn returns neither data nor error 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
522c198041
commit
499f163c31
2 changed files with 40 additions and 9 deletions
|
|
@ -16,21 +16,40 @@ export default function SignInPage() {
|
|||
setLoading(true);
|
||||
|
||||
try {
|
||||
const { data, error } = await signIn.email({
|
||||
email,
|
||||
password,
|
||||
});
|
||||
const result = await signIn.email(
|
||||
{
|
||||
email,
|
||||
password,
|
||||
},
|
||||
{
|
||||
onSuccess: () => {
|
||||
// Redirect on successful sign-in
|
||||
window.location.href = '/dashboard';
|
||||
},
|
||||
onError: (ctx) => {
|
||||
console.error('Sign in error:', ctx.error);
|
||||
setError(ctx.error.message || 'Failed to sign in');
|
||||
setLoading(false);
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
if (error) {
|
||||
console.error('Sign in error:', error);
|
||||
setError(error.message || 'Failed to sign in');
|
||||
// Handle case where no callback was triggered
|
||||
if (result.error) {
|
||||
console.error('Sign in error:', result.error);
|
||||
setError(result.error.message || 'Failed to sign in');
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (data) {
|
||||
// Successfully signed in - redirect to dashboard
|
||||
// If we have data but onSuccess didn't fire, redirect manually
|
||||
if (result.data) {
|
||||
window.location.href = '/dashboard';
|
||||
} else {
|
||||
// Neither data nor error - something unexpected happened
|
||||
console.error('Sign in returned no data or error:', result);
|
||||
setError('Sign in failed. Please try again.');
|
||||
setLoading(false);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Sign in exception:', err);
|
||||
|
|
|
|||
|
|
@ -35,6 +35,18 @@ export const auth = betterAuth({
|
|||
session: {
|
||||
expiresIn: 60 * 60 * 24 * 7, // 7 days
|
||||
updateAge: 60 * 60 * 24, // 1 day
|
||||
cookieCache: {
|
||||
enabled: true,
|
||||
maxAge: 60 * 5, // 5 minutes
|
||||
},
|
||||
},
|
||||
trustedOrigins: ['https://tpmjs.com'],
|
||||
advanced: {
|
||||
defaultCookieAttributes: {
|
||||
sameSite: 'lax',
|
||||
secure: true,
|
||||
httpOnly: true,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue