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