fix: Add redirects based on auth state
This commit is contained in:
parent
68ea056a3b
commit
df50462d06
@ -1,5 +0,0 @@
|
|||||||
import { createAuthClient } from 'better-auth/svelte';
|
|
||||||
import { BETTER_AUTH_URL } from '$env/static/private';
|
|
||||||
export const authClient = createAuthClient({
|
|
||||||
baseURL: BETTER_AUTH_URL,
|
|
||||||
});
|
|
@ -1,12 +1,21 @@
|
|||||||
import { db } from '$lib/server/db';
|
import { db } from '$lib/server/db';
|
||||||
|
import { auth } from '$lib/server/db/auth';
|
||||||
import { newChannelSchema } from '$lib/types/schema';
|
import { newChannelSchema } from '$lib/types/schema';
|
||||||
import { fail, redirect } from '@sveltejs/kit';
|
import { fail, redirect } from '@sveltejs/kit';
|
||||||
import { message, setError, superValidate } from 'sveltekit-superforms';
|
import { message, setError, superValidate } from 'sveltekit-superforms';
|
||||||
import { zod } from 'sveltekit-superforms/adapters';
|
import { zod } from 'sveltekit-superforms/adapters';
|
||||||
import type { Actions } from './$types';
|
import type { Actions } from './$types';
|
||||||
|
|
||||||
export function load(): void {
|
export async function load({ request }): Promise<void> {
|
||||||
redirect(308, '/channel/general');
|
const session = await auth.api.getSession({
|
||||||
|
headers: request.headers,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (session) {
|
||||||
|
redirect(301, '/channel/general');
|
||||||
|
} else {
|
||||||
|
redirect(401, '/signup');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const actions = {
|
export const actions = {
|
||||||
|
@ -1,8 +1,17 @@
|
|||||||
import { db } from '$lib/server/db';
|
import { db } from '$lib/server/db';
|
||||||
import type { TypeMessage } from '$lib/types';
|
import type { TypeMessage } from '$lib/types';
|
||||||
import { error } from '@sveltejs/kit';
|
import { error, redirect } from '@sveltejs/kit';
|
||||||
|
import { auth } from '$lib/server/db/auth';
|
||||||
|
|
||||||
|
export async function load({ params, request }): Promise<{ messages: TypeMessage[] }> {
|
||||||
|
const session = await auth.api.getSession({
|
||||||
|
headers: request.headers,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!session) {
|
||||||
|
redirect(307, '/signup');
|
||||||
|
}
|
||||||
|
|
||||||
export async function load({ params }): Promise<{ messages: TypeMessage[] }> {
|
|
||||||
let messages: TypeMessage[];
|
let messages: TypeMessage[];
|
||||||
const rows = await db.getMessages(params.channel, 50);
|
const rows = await db.getMessages(params.channel, 50);
|
||||||
|
|
||||||
|
@ -1,14 +1,23 @@
|
|||||||
import { dev } from '$app/environment';
|
import { dev } from '$app/environment';
|
||||||
import { auth } from '$lib/server/db/auth';
|
import { auth } from '$lib/server/db/auth';
|
||||||
import { loginSchema } from '$lib/types/schema';
|
import { loginSchema } from '$lib/types/schema';
|
||||||
|
import { redirect } from '@sveltejs/kit';
|
||||||
import { fail, message, setError, superValidate } from 'sveltekit-superforms';
|
import { fail, message, setError, superValidate } from 'sveltekit-superforms';
|
||||||
import { zod } from 'sveltekit-superforms/adapters';
|
import { zod } from 'sveltekit-superforms/adapters';
|
||||||
import type { Actions } from './$types';
|
import type { Actions } from './$types';
|
||||||
|
|
||||||
export const load = async () => {
|
export async function load({ request }) {
|
||||||
|
const session = await auth.api.getSession({
|
||||||
|
headers: request.headers,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (session) {
|
||||||
|
redirect(307, '/channel/general');
|
||||||
|
}
|
||||||
|
|
||||||
const form = await superValidate(zod(loginSchema));
|
const form = await superValidate(zod(loginSchema));
|
||||||
return { form };
|
return { form };
|
||||||
};
|
}
|
||||||
|
|
||||||
export const actions = {
|
export const actions = {
|
||||||
login: async ({ request, cookies }) => {
|
login: async ({ request, cookies }) => {
|
||||||
|
@ -1,14 +1,23 @@
|
|||||||
import { dev } from '$app/environment';
|
import { dev } from '$app/environment';
|
||||||
import { auth } from '$lib/server/db/auth';
|
import { auth } from '$lib/server/db/auth';
|
||||||
import { signupSchema } from '$lib/types/schema';
|
import { signupSchema } from '$lib/types/schema';
|
||||||
|
import { redirect } from '@sveltejs/kit';
|
||||||
import { fail, message, setError, superValidate } from 'sveltekit-superforms';
|
import { fail, message, setError, superValidate } from 'sveltekit-superforms';
|
||||||
import { zod } from 'sveltekit-superforms/adapters';
|
import { zod } from 'sveltekit-superforms/adapters';
|
||||||
import type { Actions } from './$types';
|
import type { Actions } from './$types';
|
||||||
|
|
||||||
export const load = async () => {
|
export async function load({ request }) {
|
||||||
|
const session = await auth.api.getSession({
|
||||||
|
headers: request.headers,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (session) {
|
||||||
|
redirect(307, '/channel/general');
|
||||||
|
}
|
||||||
|
|
||||||
const form = await superValidate(zod(signupSchema));
|
const form = await superValidate(zod(signupSchema));
|
||||||
return { form };
|
return { form };
|
||||||
};
|
}
|
||||||
|
|
||||||
export const actions = {
|
export const actions = {
|
||||||
signup: async ({ request, cookies }) => {
|
signup: async ({ request, cookies }) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user