Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/v3' into v3
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper committed Jan 17, 2024
2 parents acfe757 + bc7b9a6 commit be8aada
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 20 deletions.
7 changes: 3 additions & 4 deletions docs/pages/basics/sessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ declare module "lucia" {
Lucia: typeof lucia;
DatabaseSessionAttributes: DatabaseSessionAttributes;
}
}

interface DatabaseSessionAttributes {
country: string;
interface DatabaseSessionAttributes {
ip_country: string;
}
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/pages/database/drizzle.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ title: "Drizzle ORM"
Adapters for Drizzle ORM are provided by `@lucia-auth/adapter-drizzle`. Supports MySQL, PostgreSQL, and SQLite. You're free to rename the underlying table and column names as long as the field names are the same (e.g. `expiresAt`).

```
npm install @lucia-auth/adapter-sqlite@beta
npm install @lucia-auth/adapter-drizzle@beta
```

## MySQL
Expand Down
9 changes: 5 additions & 4 deletions docs/pages/guides/email-and-password/password-reset.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ The token should be valid for at most few hours.

```ts
import { TimeSpan, createDate } from "oslo";
import { generateId } from "lucia";

async function createPasswordResetToken(userId: string): Promise<string> {
// optionally invalidate all existing tokens
Expand All @@ -40,7 +41,6 @@ When a user requests a password reset email, check if the email is valid and cre

```ts
import { generateId } from "lucia";
import { encodeHex } from "oslo/encoding";

app.post("/reset-password", async () => {
let email: string;
Expand Down Expand Up @@ -71,7 +71,8 @@ Make sure to implement rate limiting based on IP addresses.
Extract the verification token from the URL and validate by checking the expiration date. If the token is valid, invalidate all existing user sessions, update the database, and create a new session.

```ts
import { isWithinExpiration } from "oslo";
import { isWithinExpirationDate } from "oslo";
import { Argon2id } from "oslo/password";

app.post("/reset-password/:token", async () => {
let password = formData.get("password");
Expand All @@ -95,15 +96,15 @@ app.post("/reset-password/:token", async () => {
status: 400
});
}
if (!isWithinExpiration(token.expires_at)) {
if (!isWithinExpirationDate(token.expires_at)) {
await db.table("password_reset_token").where("id", "=", token.id).delete();
return new Response(null, {
status: 400
});
}

await lucia.invalidateUserSessions(user.id);
const hashedPassword = new Argon2id().hash(password);
const hashedPassword = await new Argon2id().hash(password);
await db.table("user").where("id", "=", user.id).update({
hashed_password: hashedPassword
});
Expand Down
14 changes: 7 additions & 7 deletions docs/pages/guides/oauth/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ Import `GitHub` from Arctic and initialize it with the client ID and secret.
// auth.ts
import { GitHub } from "arctic";

export const githubAuth = new GitHub(clientId, clientSecret);
export const github = new GitHub(clientId, clientSecret);
```

## Creating authorization URL

Create a route to handle authorization. Generate a new state, create a new authorization URL with `createAuthorizationURL()`, store the state, and redirect the user to the authorization URL. The user will be prompted to sign in with GitHub.

```ts
import { githubAuth } from "./auth.js";
import { github } from "./auth.js";
import { generateState } from "arctic";
import { serializeCookie } from "oslo/cookie";

Expand Down Expand Up @@ -103,7 +103,7 @@ You can now create a sign in button with just an anchor tag.
In the callback route, first get the state from the cookie and the search params and compare them. Validate the authorization code in the search params with `validateAuthorizationCode()`. This will throw a [`OAuth2RequestError`](https://oslo.js.org/reference/oauth2/OAuth2RequestError) if the code or credentials are invalid. After validating the code, get the user's profile using the access token. Check if the user is already registered with the GitHub ID and create a new user if not. Finally, create a new session and set the session cookie.

```ts
import { githubAuth, lucia } from "./auth.js";
import { github, lucia } from "./auth.js";
import { OAuth2RequestError } from "arctic";
import { generateId } from "lucia";
import { parseCookies } from "oslo/cookie";
Expand All @@ -124,15 +124,15 @@ app.get("/login/github/callback", async (request: Request): Promise<Response> =>
}

try {
const tokens = await githubAuth.validateAuthorizationCode(code);
const tokens = await github.validateAuthorizationCode(code);
const githubUserResponse = await fetch("https://api.github.com/user", {
headers: {
Authorization: `Bearer ${tokens.accessToken}`
}
});
const githubUserResult: GitHubUserResult = await githubUserResponse.json();

const existingUser = await db.table("user").where("github_id", "=", githubUser.id).get();
const existingUser = await db.table("user").where("github_id", "=", githubUserResult.id).get();

if (existingUser) {
const session = await lucia.createSession(existingUser.id, {});
Expand All @@ -149,8 +149,8 @@ app.get("/login/github/callback", async (request: Request): Promise<Response> =>
const userId = generateId(15);
await db.table("user").insert({
id: userId,
username: github.login,
github_id: github.id
username: githubUserResult.login,
github_id: githubUserResult.id
});

const session = await lucia.createSession(userId, {});
Expand Down
10 changes: 7 additions & 3 deletions docs/pages/tutorials/username-and-password/sveltekit.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ npx degit https://github.com/lucia-auth/examples/tree/v3/sveltekit/username-and-
Add a `username` and `password` column to your user table.

| column | type | attributes |
| ---------- | -------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| ---------- | -------- | ---------- |
| `username` | `string` | unique |
| `password` | `string` | | Create a `DatabaseUserAttributes` interface in the module declaration and add your database columns. By default, Lucia will not expose any database columns to the `User` type. To add a `username` field to it, use the `getUserAttributes()` option. |
| `password` | `string` | |

Create a `DatabaseUserAttributes` interface in the module declaration and add your database columns. By default, Lucia will not expose any database columns to the `User` type. To add a `username` field to it, use the `getUserAttributes()` option.

```ts
import { Lucia } from "lucia";
Expand Down Expand Up @@ -77,6 +79,7 @@ Create a form action in `routes/signup/+page.server.ts`. First do a very basic i
// routes/signup/+page.server.ts
import { lucia } from "$lib/server/auth";
import { fail, redirect } from "@sveltejs/kit";
import { Argon2id } from "oslo/password";

import type { Actions } from "./$types";

Expand Down Expand Up @@ -164,6 +167,7 @@ Create an API route as `pages/api/signup.ts`. First do a very basic input valida
```ts
import { lucia } from "$lib/server/auth";
import { fail, redirect } from "@sveltejs/kit";
import { Argon2id } from "oslo/password";

import type { Actions } from "./$types";

Expand Down Expand Up @@ -256,7 +260,7 @@ export const actions: Actions = {
}
await auth.invalidateSession(event.locals.session.id);
const sessionCookie = lucia.createBlankSessionCookie();
context.cookies.set(sessionCookie.name, sessionCookie.value, {
event.cookies.set(sessionCookie.name, sessionCookie.value, {
path: ".",
...sessionCookie.attributes
});
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/upgrade-v3/sqlite.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
BetterSqlite3Adapter,
CloudflareD1Adapter,
LibSQLAdapter
} from "@lucia-auth/adapter-mysql";
} from "@lucia-auth/adapter-sqlite";

new BetterSqlite3Adapter(db, {
// table names
Expand Down

0 comments on commit be8aada

Please sign in to comment.