A running work in progress. Examples of common auth patterns I keep around for convenience.
Branch | Pattern | Description |
---|---|---|
main |
Simple Sign In | Username/password login with a database and token management. |
Coming Soon | Session Sign In | Like the previous setup but adds an additional sessions layer. This way we can remotely logout users by invalidating the session on our end. |
Coming Soon | Sign In w/Solana | Sessions + Sign in using a Solana wallet. |
Coming Soon | Sign In w/Discord | Sessions + Sign in using Discord. |
Coming Soon | Sign In w/Twitter | Sessions + Sign in using Twitter. |
src/hooks.server.ts
file runs on every request. This file determines if the user is signed in and allowed to view gated content. If a gated route is loaded but the user doesn't have a JWT/valid session cookie in their browser, they will be redirected to login.src/schemas.ts
to define the new type.src/models.ts
to register it with the database.InferSchemaType
in src/types.ts
to register your new schema as a TypeScript type.From here you should be able to import the models into endpoints and use mongoose
as expected. See examples in existing endpoints.
src/routes/api
These are server side endpoints the UI hits.
src/routes/+page.svelte
The home page that is gated by authentication.
src/routes/app/login/+page.svelte
The login page.
src/routes/app/sign-up/+page.svelte
The create account page.
Before anything, ensure you have Node installed and then install the project.
npm install
The following environment variables are required. Your made up secret to create JWTs, and MongoDB connection string.
ENV_JWT_SECRET
: Some secret/password you generate and keep secret that is uses to encode/decode JWTs.ENV_MONGO
: MongoDB connection string. Should look something like mongodb+srv://username:password@url.mongodb.net/sign-on?retryWrites=true&w=majority
.Create a .env
in the root of the project that looks like this but with your values.
ENV_JWT_SECRET=
ENV_MONGO=
npm run dev
npm run build