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');
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');
14 Replies
Brendonovich
Brendonovichβ€’5mo ago
hmm this should work
Raqueebuddin Aziz
Raqueebuddin AzizOPβ€’5mo ago
onClick={async () => {
'use server';

const event = getRequestEvent()!;
deleteCookie(event.nativeEvent, 'accessToken');
return redirect('/signin');
}}
onClick={async () => {
'use server';

const event = getRequestEvent()!;
deleteCookie(event.nativeEvent, 'accessToken');
return redirect('/signin');
}}
should this work? it doesn't the signin one is weird it works if I remove everything b/w getRquestevent and use server even if it get pasts all the early returns, because the cookie is set correctly it just doesn't redirect
export const route = {
load: cache(async () => {
const user = await getUser()
if (!user) {
return redirect('/signin')
}
}, '(protected) load')
};
export const route = {
load: cache(async () => {
const user = await getUser()
if (!user) {
return redirect('/signin')
}
}, '(protected) load')
};
also this only redirects if it's wrapped in cache, is that intended? if yes is this documented somewhere? thanks for any help, really appreciate it πŸ™‚
Brendonovich
Brendonovichβ€’5mo ago
This won’t work as the redirect helper only works if the function is wrapped in cache/action
Raqueebuddin Aziz
Raqueebuddin AzizOPβ€’5mo ago
Got it, couldn't find the documentation saying that I see the examples have cache, I still think it could help if its added to the text as well, is the docs repo accepting PRs? Thank you for all the help Brendon πŸ™‚
Brendonovich
Brendonovichβ€’5mo ago
yeah it's open to PRs
Raqueebuddin Aziz
Raqueebuddin AzizOPβ€’5mo ago
figured out why the original redirect wasn't working, my auth guard would redirect back to sign in, must have something to do with the order in which cookies are updated and requests being made Is there a way to make sure my layout load runs after the cookies are set? removing cache from getUser fixed it getUser was used in the layout guard
Brendonovich
Brendonovichβ€’5mo ago
ah btw cache functions should be kept at the top level, not declared inline like this
Raqueebuddin Aziz
Raqueebuddin AzizOPβ€’5mo ago
can I ask the reason?
Brendonovich
Brendonovichβ€’5mo ago
the idea is that you declare the data fetcher once and then reuse it so you'd reuse that cached function anywhere you need the user
Raqueebuddin Aziz
Raqueebuddin AzizOPβ€’5mo ago
got it, I think in this case the cache function would still be only called once
Brendonovich
Brendonovichβ€’5mo ago
so no matter where you request user data that redirect will happen and you can call it both in the preload + use it in your markup
Raqueebuddin Aziz
Raqueebuddin AzizOPβ€’5mo ago
you mean in the getUser function? if I use cache there it doesn't redirect properly or invalidate properly I should say removing it fixed the issue I understand that means getUsers is called on every navigation but I think thats whats supposed to happen with auth guards
Brendonovich
Brendonovichβ€’5mo ago
i'd argue otherwise but whatever works
Want results from more Discord servers?
Add your server