Skip to content

Commit

Permalink
fix(wizard): fic bug with first boot
Browse files Browse the repository at this point in the history
  • Loading branch information
rishikanthc committed Oct 19, 2024
1 parent 1c2d7c1 commit 078673a
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/hooks.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,29 @@ export const authentication: Handle = async ({ event, resolve }) => {
return response;
};

export const configuration: Handle = async ({event, resolve}) => {
const settings = await event.locals.pb.collection('settings').getList(1,1);
export const configuration: Handle = async ({ event, resolve }) => {
try {
// Try to fetch settings collection
const settings = await event.locals.pb.collection('settings').getList(1, 1);

console.log(settings)
const condition = (settings.items.length <= 0) || settings.items?.wizard
// Check if the collection is empty or wizard hasn't been completed
const condition = (settings.items.length <= 0) || settings.items[0]?.wizard;

if (condition && !event.url.pathname.endsWith('/wizard') && !event.url.pathname.startsWith('/api/')) {
// If the wizard is not completed, redirect to /wizard
// Redirect to wizard if conditions are met
console.log("Redirecting to wizard <-------------");
throw redirect(307, '/wizard');
}
} catch (error) {
// If the settings collection doesn't exist or there's an error, redirect to wizard
console.error('Error fetching settings collection or collection does not exist:', error);
if (!event.url.pathname.endsWith('/wizard') && !event.url.pathname.startsWith('/api/')) {
throw redirect(307, '/wizard');
}
}

return resolve(event)
// Resolve event if no issues
return resolve(event);
};

export const handle = sequence(authentication, configuration);

0 comments on commit 078673a

Please sign in to comment.