Redirects in sveltekits are actually throws and so if you have a redirect inside a try catch block, the redirect will trigger the catch clause. This will result in the redirect not actually getting caught by sveltekit so the redirect never runs.
The solution is to check the error if you are embedding a redirect inside a try catch block and throw the error once more if it is a redirect.
This can be seen in the below login example:
import { isRedirect, redirect } from '@sveltejs/kit';
import bcrypt from "bcrypt";
export const actions = {
default: async ({ locals, request }) => {
try {
const db = locals.db;
const values = await request.formData();
const username = values.get('username');
const password = values.get('password');
const row = db.prepare('SELECT * FROM users WHERE username = ?').get(username);
if (!row) {
return { error: 'Invalid username.' };
}
let valid = await bcrypt.compare(password, row.password);
if (!valid) {
return { error: 'Invalid password.' };
}
await locals.session.set({ active: true, username: username });
redirect(302,"/");
} catch (e) {
if (isRedirect(e)) throw e;
return { error: error.message };
}
}
};
isRedirect
is a function from sveltekit that we can use to check the error. If it is a redirect, we throw it again so that sveltekit can handle the redirect.
This is the magical part of sveltekit that I don't like. Bah.