This commit is contained in:
Dan Mihailescu 2024-12-15 18:17:02 -05:00
parent f0f32d71fc
commit ce0d940953
35 changed files with 62 additions and 66 deletions

View file

@ -13,4 +13,4 @@ const config = {
options: {} options: {}
} }
}; };
export default config; export default config;

View file

@ -10,4 +10,4 @@ const preview = {
} }
}; };
export default preview; export default preview;

View file

@ -16,4 +16,4 @@ h2 {
h3 { h3 {
@apply font-display text-2xl; @apply font-display text-2xl;
} }

3
src/app.d.ts vendored
View file

@ -2,10 +2,9 @@
// for information about these interfaces // for information about these interfaces
declare global { declare global {
namespace App { namespace App {
// interface Error {} // interface Error {}
interface Locals { interface Locals {
user: import("lucia").User | null; user: import('lucia').User | null;
session: import('lucia').Session | null; session: import('lucia').Session | null;
} }
// interface PageData {} // interface PageData {}

View file

@ -27,4 +27,4 @@
}); });
</script> </script>
<Story name="Default" args={{ label: 'Button', size: 'normal', primary: true }} /> <Story name="Default" args={{ label: 'Button', size: 'normal', primary: true }} />

View file

@ -48,4 +48,4 @@
.button--secondary { .button--secondary {
@apply bg-blue-600 text-white; @apply bg-blue-600 text-white;
} }
</style> </style>

View file

@ -40,4 +40,4 @@
<Story name="Text" args={{ label: 'Text', name: 'text', type: 'text' }} /> <Story name="Text" args={{ label: 'Text', name: 'text', type: 'text' }} />
<Story name="Password" args={{ label: 'Password', name: 'pass', type: 'password' }} /> <Story name="Password" args={{ label: 'Password', name: 'pass', type: 'password' }} />
<Story name="Email" args={{ label: 'Email', name: 'email', type: 'email' }} /> <Story name="Email" args={{ label: 'Email', name: 'email', type: 'email' }} />

View file

@ -33,4 +33,4 @@
all: unset; all: unset;
@apply text-lg; @apply text-lg;
} }
</style> </style>

View file

@ -9,4 +9,4 @@
}); });
</script> </script>
<Story name="Default" args={{}} /> <Story name="Default" args={{}} />

View file

@ -48,4 +48,4 @@
transform: rotate(-360deg); transform: rotate(-360deg);
} }
} }
</style> </style>

View file

@ -9,4 +9,4 @@
}); });
</script> </script>
<Story name="Default" args={{ title: 'Storybook' }} /> <Story name="Default" args={{ title: 'Storybook' }} />

View file

@ -28,4 +28,4 @@
.navbar h2 { .navbar h2 {
@apply text-xl; @apply text-xl;
} }
</style> </style>

View file

@ -1 +1 @@
export * from './Navbar.svelte'; export * from './Navbar.svelte';

View file

@ -1,2 +1,2 @@
// place files you want to import through the `$lib` alias in this folder. // place files you want to import through the `$lib` alias in this folder.
export * from './components'; export * from './components';

View file

@ -21,4 +21,4 @@ export const LoadConfig = (): Configuration => {
}; };
}; };
export const Config = LoadConfig(); export const Config = LoadConfig();

View file

@ -20,4 +20,4 @@ export const yogaLogger: YogaLogger = {
// @ts-expect-error types dont match // @ts-expect-error types dont match
logger.error(...args); logger.error(...args);
} }
}; };

View file

@ -1,32 +1,32 @@
import { Lucia } from "lucia"; import { Lucia } from 'lucia';
import { PrismaAdapter } from "@lucia-auth/adapter-prisma"; import { PrismaAdapter } from '@lucia-auth/adapter-prisma';
import { PrismaClient } from "@prisma/client"; import { PrismaClient } from '@prisma/client';
const client = new PrismaClient(); const client = new PrismaClient();
const adapter = new PrismaAdapter(client.session, client.user) const adapter = new PrismaAdapter(client.session, client.user);
// expect error (see next section) // expect error (see next section)
export const auth = new Lucia(adapter, { export const auth = new Lucia(adapter, {
sessionCookie: { sessionCookie: {
attributes: { attributes: {
secure: process.env.NODE_ENV === "production" secure: process.env.NODE_ENV === 'production'
} }
}, },
getUserAttributes: (attributes)=>{ getUserAttributes: (attributes) => {
return { return {
email: attributes.email email: attributes.email
} };
} }
}); });
declare module "lucia" { declare module 'lucia' {
interface Register { interface Register {
Lucia: typeof Lucia; Lucia: typeof Lucia;
DatabaseUserAttributes: DatabaseUserAttributes DatabaseUserAttributes: DatabaseUserAttributes;
} }
} }
interface DatabaseUserAttributes { interface DatabaseUserAttributes {
email: string email: string;
} }
export type Auth = typeof auth; export type Auth = typeof auth;

View file

@ -25,4 +25,4 @@ export const builder = new SchemaBuilder<PothosType>({
// warn when not using a query parameter correctly // warn when not using a query parameter correctly
onUnusedQuery: process.env.NODE_ENV === 'production' ? null : 'warn' onUnusedQuery: process.env.NODE_ENV === 'production' ? null : 'warn'
} }
}); });

View file

@ -1 +1 @@
export * from './schema'; export * from './schema';

View file

@ -11,4 +11,4 @@ export const DateScalar = builder.scalarType('Date', {
} }
return new Date(date); return new Date(date);
} }
}); });

View file

@ -5,4 +5,4 @@ export type Scalars = {
Input: Date; Input: Date;
Output: Date; Output: Date;
}; };
}; };

View file

@ -15,4 +15,4 @@ import './Scalars';
import './posts'; import './posts';
import './users'; import './users';
export const Schema = builder.toSchema(); export const Schema = builder.toSchema();

View file

@ -107,4 +107,4 @@ builder.mutationFields((t) => ({
return post; return post;
} }
}) })
})); }));

View file

@ -80,4 +80,4 @@ builder.mutationFields((t) => ({
return post; return post;
} }
}) })
})); }));

View file

@ -1,3 +1,3 @@
import { PrismaClient } from '@prisma/client'; import { PrismaClient } from '@prisma/client';
export const prisma = new PrismaClient(); export const prisma = new PrismaClient();

View file

@ -4,4 +4,4 @@ import type { YogaInitialContext } from 'graphql-yoga';
export const Context = (initialContext: YogaInitialContext) => ({ export const Context = (initialContext: YogaInitialContext) => ({
...initialContext, ...initialContext,
config: Config config: Config
}); });

View file

@ -1,2 +1,2 @@
export * from './context'; export * from './context';
export * from './server'; export * from './server';

View file

@ -11,4 +11,4 @@ export const Yoga = createYoga<RequestEvent>({
// Let Yoga use sveltekit's Response object // Let Yoga use sveltekit's Response object
fetchAPI: { Response }, fetchAPI: { Response },
logging: yogaLogger logging: yogaLogger
}); });

View file

@ -11,4 +11,4 @@
.layout { .layout {
@apply h-screen w-screen animate-fade bg-slate-100; @apply h-screen w-screen animate-fade bg-slate-100;
} }
</style> </style>

View file

@ -15,4 +15,4 @@ export async function load(event) {
return { return {
authenticated: !!user authenticated: !!user
}; };
} }

View file

@ -21,4 +21,4 @@
.site-loader { .site-loader {
@apply flex h-screen w-screen flex-col items-center justify-center gap-6; @apply flex h-screen w-screen flex-col items-center justify-center gap-6;
} }
</style> </style>

View file

@ -1,3 +1,3 @@
import { Yoga } from '$lib/server/yoga'; import { Yoga } from '$lib/server/yoga';
export { Yoga as GET, Yoga as POST }; export { Yoga as GET, Yoga as POST };

View file

@ -5,4 +5,4 @@
</script> </script>
<Navbar title="Svelte" /> <Navbar title="Svelte" />
{@render children()} {@render children()}

View file

@ -1,7 +1,7 @@
import { logger } from '$lib/server/logger'; import { logger } from '$lib/server/logger';
import { prisma } from '$lib/server/prisma'; import { prisma } from '$lib/server/prisma';
import { error, redirect, type Actions } from '@sveltejs/kit'; import { error, redirect, type Actions } from '@sveltejs/kit';
import { Argon2id } from "oslo/password" import { Argon2id } from 'oslo/password';
import { auth } from '$lib/server/lucia.js'; import { auth } from '$lib/server/lucia.js';
export const actions = { export const actions = {
@ -19,12 +19,12 @@ export const actions = {
logger.error('User not found! ${user}'); logger.error('User not found! ${user}');
return error(401); return error(401);
} }
const pw = form.get('password') as string; const password = form.get('password') as string;
if(!pw) { if (!password) {
return error(401, 'Password is required') return error(401, 'Password is required');
} }
const validPassword = await new Argon2id().verify(user.password,pw); const validPassword = await new Argon2id().verify(user.password, password);
if(!validPassword) { if (!validPassword) {
return error(400, 'Password is incorrect!'); return error(400, 'Password is incorrect!');
} }
const session = await auth.createSession(user.id, []); const session = await auth.createSession(user.id, []);
@ -36,14 +36,12 @@ export const actions = {
redirect(302, '/'); redirect(302, '/');
}, },
register: async (event) => { register: async (event) => {
const form = await event.request.formData(); const form = await event.request.formData();
if (!form.has('email') || !form.has('name') || !form.has('password')) { if (!form.has('email') || !form.has('name') || !form.has('password')) {
return error(400); return error(400);
} }
const hashedPassword = await new Argon2id().hash(form.get('password') as string) const hashedPassword = await new Argon2id().hash(form.get('password') as string);
const user = await prisma.user.create({ const user = await prisma.user.create({
data: { data: {
email: form.get('email') as string, email: form.get('email') as string,
@ -61,6 +59,5 @@ export const actions = {
maxAge: 120 maxAge: 120
}); });
redirect(302, '/'); redirect(302, '/');
} }
} satisfies Actions; } satisfies Actions;

View file

@ -22,4 +22,4 @@ export default {
}, },
plugins: [typography] plugins: [typography]
} satisfies Config; } satisfies Config;