SolidJSS
SolidJSβ€’2y agoβ€’
27 replies
Raqueebuddin Aziz

how to redirect from form actions

I want to redirect to the index page from signin form action on successful with

Any insight would be helpful

My current code doesn't redirect
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');
Was this page helpful?