Update all routes to redirect users to create a profile if one does not exist
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
import type { LayoutServerLoad } from './$types';
|
||||
import { error } from '@sveltejs/kit';
|
||||
import { error, redirect } from '@sveltejs/kit';
|
||||
import { prisma } from '$lib/db';
|
||||
|
||||
export const load: LayoutServerLoad = async ({ locals }) => {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: locals.user?.id },
|
||||
select: { id: true, roleId: true }
|
||||
select: { id: true, roleId: true, profile: { select: { id: true } } }
|
||||
});
|
||||
if (!user) {
|
||||
throw error(403, 'not authorized');
|
||||
}
|
||||
if (!user.profile) {
|
||||
throw redirect(303, '/account/setup');
|
||||
}
|
||||
|
||||
return { userRoleId: user.roleId };
|
||||
};
|
||||
|
||||
@@ -1,23 +1,14 @@
|
||||
import type { PageServerLoad } from './$types';
|
||||
import { prisma } from '$lib/db';
|
||||
import { error, redirect } from '@sveltejs/kit';
|
||||
import { error } from '@sveltejs/kit';
|
||||
|
||||
export const load: PageServerLoad = async ({ locals }) => {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: locals.user?.id
|
||||
},
|
||||
include: {
|
||||
profile: true
|
||||
}
|
||||
where: { id: locals.user?.id },
|
||||
include: { profile: true }
|
||||
});
|
||||
|
||||
if (user) {
|
||||
if (!user?.profile) {
|
||||
throw redirect(403, '/account/setup');
|
||||
}
|
||||
return { user };
|
||||
} else {
|
||||
if (!user) {
|
||||
throw error(403, 'not authorized');
|
||||
}
|
||||
return { user };
|
||||
};
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
return;
|
||||
}
|
||||
|
||||
window.location.href = '/';
|
||||
window.location.href = '/account';
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -41,7 +41,7 @@
|
||||
<button
|
||||
onclick={async () => {
|
||||
console.log('signing in with google');
|
||||
authClient.signIn.social({ provider: 'google' });
|
||||
authClient.signIn.social({ provider: 'google', callbackURL: '/account' });
|
||||
}}
|
||||
>
|
||||
<img src="light/web_light_sq_SI.svg" alt="Sign In with Google" />
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Actions } from "./$types";
|
||||
import { auth } from "$lib/auth";
|
||||
import { fail } from "@sveltejs/kit";
|
||||
import { fail, redirect } from "@sveltejs/kit";
|
||||
|
||||
export const actions: Actions = {
|
||||
default: async ({ request }) => {
|
||||
@@ -26,7 +26,7 @@ export const actions: Actions = {
|
||||
})
|
||||
|
||||
if (response.user) {
|
||||
return { success: true }
|
||||
throw redirect(303, '/account/setup');
|
||||
}
|
||||
} catch (err) {
|
||||
return fail(400, err);
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
<button
|
||||
class="mx-auto"
|
||||
on:click={async () => {
|
||||
authClient.signIn.social({ provider: 'google' });
|
||||
authClient.signIn.social({ provider: 'google', callbackURL: '/account/setup' });
|
||||
}}><img src="light/web_light_sq_SU.svg" alt="Sign Up with Google" /></button
|
||||
>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user