const signIn = action(async (formData: FormData) => {
'use server';
const email = String(formData.get('email'));
const password = String(formData.get('password'));
const [user] = await db
.select({ id: users.id, passwordHash: users.passwordHash })
.from(users)
.where(eq(users.email, email));
if (!user) return new Error('Invalid Credentials');
if (!(await bcrypt.compare(password, user.passwordHash))) return new Error('Invalid Credentials');
const token = jwt.sign({ id: user.id }, process.env.AUTH_SECRET!, {
expiresIn: '3 days'
});
const event = getRequestEvent()!;
setCookie(event.nativeEvent, 'accessToken', token, {
httpOnly: true,
secure: true,
path: '/',
sameSite: 'lax'
});
return redirect('/');
}, 'signin');
const signIn = action(async (formData: FormData) => {
'use server';
const email = String(formData.get('email'));
const password = String(formData.get('password'));
const [user] = await db
.select({ id: users.id, passwordHash: users.passwordHash })
.from(users)
.where(eq(users.email, email));
if (!user) return new Error('Invalid Credentials');
if (!(await bcrypt.compare(password, user.passwordHash))) return new Error('Invalid Credentials');
const token = jwt.sign({ id: user.id }, process.env.AUTH_SECRET!, {
expiresIn: '3 days'
});
const event = getRequestEvent()!;
setCookie(event.nativeEvent, 'accessToken', token, {
httpOnly: true,
secure: true,
path: '/',
sameSite: 'lax'
});
return redirect('/');
}, 'signin');