Skip to content

Commit

Permalink
add framework getting started guides
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper committed Nov 30, 2023
1 parent 5467f1f commit d39359d
Show file tree
Hide file tree
Showing 19 changed files with 559 additions and 25 deletions.
4 changes: 2 additions & 2 deletions documentation-v3/src/pages/basics/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const lucia = new Lucia(adapter, {

declare module "lucia" {
interface Register {
Lucia: typeof auth;
Lucia: typeof lucia;
DatabaseSessionAttributes: {
ip_country: string;
};
Expand All @@ -122,7 +122,7 @@ const lucia = new Lucia(adapter, {

declare module "lucia" {
interface Register {
Lucia: typeof auth;
Lucia: typeof lucia;
DatabaseUserAttributes: {
username: string;
};
Expand Down
4 changes: 2 additions & 2 deletions documentation-v3/src/pages/basics/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Here are some common issues and how to resolve them. Feel free to ask for help i

## `User` and `Session` are typed as `any`

Make sure you've registered your types. Check the `typeof auth` is indeed an instance of `Lucia` (not a function that returns `Lucia`) and that there are no TS errors (including `@ts-ignore`) when declaring `Lucia`. `Register` must be an `interface`, not `type`.
Make sure you've registered your types. Check the `typeof lucia` is indeed an instance of `Lucia` (not a function that returns `Lucia`) and that there are no TS errors (including `@ts-ignore`) when declaring `Lucia`. `Register` must be an `interface`, not `type`.

```ts
import { Lucia } from "lucia";
Expand All @@ -18,7 +18,7 @@ const lucia = new Lucia(adapter, {

declare module "lucia" {
interface Register {
Lucia: typeof auth;
Lucia: typeof lucia;
}
}
```
Expand Down
4 changes: 2 additions & 2 deletions documentation-v3/src/pages/basics/sessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Defining custom session attributes requires 2 steps. First, add the required col
```ts
declare module "lucia" {
interface Register {
Lucia: typeof auth;
Lucia: typeof lucia;
DatabaseSessionAttributes: {
ip_country: string;
};
Expand Down Expand Up @@ -80,7 +80,7 @@ const session = await lucia.createSession(userId, {

declare module "lucia" {
interface Register {
Lucia: typeof auth;
Lucia: typeof lucia;
DatabaseSessionAttributes: {
ip_country: string;
};
Expand Down
2 changes: 1 addition & 1 deletion documentation-v3/src/pages/basics/users.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Defining custom session attributes requires 2 steps. First, add the required col
```ts
declare module "lucia" {
interface Register {
Lucia: typeof auth;
Lucia: typeof lucia;
DatabaseUserAttributes: {
username: string;
};
Expand Down
79 changes: 79 additions & 0 deletions documentation-v3/src/pages/getting-started/astro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
layout: "@layouts/DocLayout.astro"
title: "Getting started in Astro"
---

Install Lucia using your package manager of your choice. While not strictly necessary, we recommend installing [Oslo](https://oslo.js.org), which Lucia is built on, for various auth utilities (which a lot of the guides use).

```
npm install lucia@beta oslo
```

## Initialize Lucia

Import `Lucia` and initialize it with your adapter. Refer to the [Database](/database) page to learn how to setup your database and initialize the adapter. Make sure you:

- Use the `astro` middleware
- Configure the `sessionCookie` option
- Register your `Lucia` instance type

```ts
// src/auth.ts
import { Lucia } from "lucia";
import { astro } from "lucia/middleware";

const adapter = new BetterSQLite3Adapter(db); // your adapter

export const lucia = new Lucia(adapter, {
middleware: astro(),
sessionCookie: {
// IMPORTANT!
attributes: {
// set to `true` when using HTTPS
secure: import.meta.env.PROD
}
}
});

// IMPORTANT!
declare module "lucia" {
interface Register {
Lucia: typeof lucia;
}
}
```

## Setup middleware

If you're planning to use cookies to store the session, we recommend setting up middleware to make `AuthRequest` available in all routes.

```ts
// src/middleware.ts
import { lucia } from "./lucia";

import type { MiddlewareResponseHandler } from "astro";

export const onRequest: MiddlewareResponseHandler = async (context, next) => {
context.locals.lucia = lucia.handleRequest(context);
return await next();
};
```

Make sure sure to type `App.Locals` as well.

```ts
// src/env.d.ts

/// <reference types="astro/client" />
declare namespace App {
interface Locals {
lucia: import("lucia").AuthRequest;
}
}
```

## Next steps

You can learn all the concepts and APIs by reading the [Basics section](/basics/sessions) in the docs. If you prefer writing code immediately, check out the [Starter guides](/starter-guides) page or the [examples repository](/https://github.com/lucia-auth/examples).

If you have any questions, [join our Discord server](https://discord.com/invite/PwrK3kpVR3)!
50 changes: 50 additions & 0 deletions documentation-v3/src/pages/getting-started/elysia.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
layout: "@layouts/DocLayout.astro"
title: "Getting started in Elysia"
---

Install Lucia using your package manager of your choice. While not strictly necessary, we recommend installing [Oslo](https://oslo.js.org), which Lucia is built on, for various auth utilities (which a lot of the guides use).

```
npm install lucia@beta oslo
```

## Initialize Lucia

Import `Lucia` and initialize it with your adapter. Refer to the [Database](/database) page to learn how to setup your database and initialize the adapter. Make sure you:

- Use the `elysia` middleware
- Configure the `sessionCookie` option
- Register your `Lucia` instance type

```ts
// server/utils/auth.ts
import { Lucia } from "lucia";
import { elysia } from "lucia/middleware";

const adapter = new BetterSQLite3Adapter(db); // your adapter

export const lucia = new Lucia(adapter, {
middleware: elysia(),
sessionCookie: {
// IMPORTANT!
attributes: {
// set to `true` when using HTTPS
secure: process.env.NODE_ENV === "production"
}
}
});

// IMPORTANT!
declare module "lucia" {
interface Register {
Lucia: typeof lucia;
}
}
```

## Next steps

You can learn all the concepts and APIs by reading the [Basics section](/basics/sessions) in the docs. If you prefer writing code immediately, check out the [Starter guides](/starter-guides) page or the [examples repository](/https://github.com/lucia-auth/examples).

If you have any questions, [join our Discord server](https://discord.com/invite/PwrK3kpVR3)!
64 changes: 64 additions & 0 deletions documentation-v3/src/pages/getting-started/express.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
layout: "@layouts/DocLayout.astro"
title: "Getting started in Express"
---

Install Lucia using your package manager of your choice. While not strictly necessary, we recommend installing [Oslo](https://oslo.js.org), which Lucia is built on, for various auth utilities (which a lot of the guides use).

```
npm install lucia@beta oslo
```

## Initialize Lucia

Import `Lucia` and initialize it with your adapter. Refer to the [Database](/database) page to learn how to setup your database and initialize the adapter. Make sure you:

- Use the `express` middleware
- Configure the `sessionCookie` option
- Register your `Lucia` instance type

```ts
// server/utils/auth.ts
import { Lucia } from "lucia";
import { express } from "lucia/middleware";

const adapter = new BetterSQLite3Adapter(db); // your adapter

export const lucia = new Lucia(adapter, {
middleware: express(),
sessionCookie: {
// IMPORTANT!
attributes: {
// set to `true` when using HTTPS
secure: process.env.NODE_ENV === "production"
}
}
});

// IMPORTANT!
declare module "lucia" {
interface Register {
Lucia: typeof lucia;
}
}
```

## Polyfill

If you're using Node.js 18 or below, you'll need to polyfill the Web Crypto API. This is not required in Node.js 20, CouldFlare Workers, Deno, Bun, and Vercel Edge Functions. This can be done either by importing `webcrypto`, or by enabling an experimental flag.

```ts
import { webcrypto } from "node:crypto";

globalThis.crypto = webcrypto as Crypto;
```

```
node --experimental-web-crypto index.js
```

## Next steps

You can learn all the concepts and APIs by reading the [Basics section](/basics/sessions) in the docs. If you prefer writing code immediately, check out the [Starter guides](/starter-guides) page or the [examples repository](/https://github.com/lucia-auth/examples).

If you have any questions, [join our Discord server](https://discord.com/invite/PwrK3kpVR3)!
64 changes: 64 additions & 0 deletions documentation-v3/src/pages/getting-started/hono.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
layout: "@layouts/DocLayout.astro"
title: "Getting started in Hono"
---

Install Lucia using your package manager of your choice. While not strictly necessary, we recommend installing [Oslo](https://oslo.js.org), which Lucia is built on, for various auth utilities (which a lot of the guides use).

```
npm install lucia@beta oslo
```

## Initialize Lucia

Import `Lucia` and initialize it with your adapter. Refer to the [Database](/database) page to learn how to setup your database and initialize the adapter. Make sure you:

- Use the `express` middleware
- Configure the `sessionCookie` option
- Register your `Lucia` instance type

```ts
// server/utils/auth.ts
import { Lucia } from "lucia";
import { hono } from "lucia/middleware";

const adapter = new BetterSQLite3Adapter(db); // your adapter

export const lucia = new Lucia(adapter, {
middleware: hono(),
sessionCookie: {
// IMPORTANT!
attributes: {
// set to `true` when using HTTPS
secure: process.env.NODE_ENV === "production"
}
}
});

// IMPORTANT!
declare module "lucia" {
interface Register {
Lucia: typeof lucia;
}
}
```

## Polyfill

If you're using Node.js 18 or below, you'll need to polyfill the Web Crypto API. This is not required in Node.js 20, CouldFlare Workers, Deno, Bun, and Vercel Edge Functions. This can be done either by importing `webcrypto`, or by enabling an experimental flag.

```ts
import { webcrypto } from "node:crypto";

globalThis.crypto = webcrypto as Crypto;
```

```
node --experimental-web-crypto index.js
```

## Next steps

You can learn all the concepts and APIs by reading the [Basics section](/basics/sessions) in the docs. If you prefer writing code immediately, check out the [Starter guides](/starter-guides) page or the [examples repository](/https://github.com/lucia-auth/examples).

If you have any questions, [join our Discord server](https://discord.com/invite/PwrK3kpVR3)!
28 changes: 15 additions & 13 deletions documentation-v3/src/pages/getting-started/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ title: "Getting started"

A framework-specific guide is also available for:

- [Astro]()
- [Elysia]()
- [Express]()
- [Hono]()
- [Next.js App router]()
- [Next.js Pages router]()
- [Node.js]()
- [Nuxt]()
- [SvelteKit]()
- [Astro](/getting-started/astro)
- [Elysia](/getting-started/elysia)
- [Express](/getting-started/express)
- [Hono](/getting-started/hono)
- [Next.js App router](/getting-started/nextjs-app)
- [Next.js Pages router](/getting-started/nextjs-pages)
- [Nuxt](/getting-started/nuxt)
- [SvelteKit](/getting-started/sveltekit)

## Installation

Expand All @@ -25,7 +24,10 @@ npm install lucia@beta oslo

## Initialize Lucia

Import `Lucia` and initialize it with your adapter. Refer to the [Database](/database) page to learn how to setup your database and initialize the adapter. **Make sure to configure the `sessionCookie` option and register your `Lucia` instance type.**
Import `Lucia` and initialize it with your adapter. Refer to the [Database](/database) page to learn how to setup your database and initialize the adapter. Make sure you:

- Configure the `sessionCookie` option
- Register your `Lucia` instance type

```ts
import { Lucia } from "lucia";
Expand All @@ -35,16 +37,16 @@ const adapter = new BetterSQLite3Adapter(db); // your adapter
export const lucia = new Lucia(adapter, {
sessionCookie: {
attributes: {
// IMPORTANT!
secure: PRODUCTION === true // `true` when deploying to HTTPS (production)
// set to `true` when using HTTPS
secure: process.env.NODE_ENV === "production"
}
}
});

// IMPORTANT!
declare module "lucia" {
interface Register {
Lucia: typeof auth;
Lucia: typeof lucia;
}
}
```
Expand Down
Loading

0 comments on commit d39359d

Please sign in to comment.