Skip to content

Commit

Permalink
Custom user ID (#1472)
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper authored Mar 9, 2024
1 parent 1e4e1b8 commit 4e4f615
Show file tree
Hide file tree
Showing 30 changed files with 119 additions and 71 deletions.
15 changes: 15 additions & 0 deletions docs/pages/basics/users.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,18 @@ We do not automatically expose all database columns as

1. Each project has its own code styling rules
2. You generally don't want to expose sensitive data such as hashed passwords (even worse if you send the entire user object to the client)

### Define user ID type

User IDs are typed as strings by default. You can override this by declaring `Register.UserId`.

```ts
declare module "lucia" {
interface Register {
Lucia: typeof lucia;
UserId: number;
}
}

const session = await lucia.createSession(0, {});
```
2 changes: 1 addition & 1 deletion docs/pages/database/postgresql.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: "PostgreSQL"

# PostgreSQL

> If you use Drizzle ORM in your project, use the [Drizzle adapter](https://lucia-auth.com/database/drizzle#postgresql) instead as Drizzle ORM overrides how your database driver handles dates.
> If you use Drizzle ORM in your project, use the [Drizzle adapter](https://lucia-auth.com/database/drizzle#postgresql) instead as Drizzle ORM overrides how your database driver handles dates.
`@lucia-auth/adapter-postgresql` package provides adapters for PostgreSQL drivers:

Expand Down
1 change: 0 additions & 1 deletion docs/pages/getting-started/sveltekit.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ If you use `@sveltejs/adapter-node`, make sure to install `oslo` as a `dependenc
npm install oslo
```


## Initialize Lucia

Import `Lucia` and initialize it with your adapter. Refer to the [Database](/database) page to learn how to set up your database and initialize the adapter. Make sure to configure the `sessionCookie` option and register your `Lucia` instance type
Expand Down
5 changes: 3 additions & 2 deletions docs/pages/reference/main/Adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ Represents a database adapter.
```ts
//$ DatabaseSession=/reference/main/DatabaseSession
//$ DatabaseUser=/reference/main/DatabaseUser
//$ UserId=/reference/main/UserId
interface Adapter {
deleteExpiredSessions(): Promise<void>;
deleteSession(sessionId: string): Promise<void>;
deleteUserSessions(userId: string): Promise<void>;
deleteUserSessions(userId: $$UserId): Promise<void>;
getSessionAndUser(
sessionId: string
): Promise<[session: $$DatabaseSession | null, user: $$DatabaseUser | null]>;
getUserSessions(userId: string): Promise<$$DatabaseSession[]>;
getUserSessions(userId: $$UserId): Promise<$$DatabaseSession[]>;
setSession(session: $$DatabaseSession): Promise<void>;
updateSessionExpiration(sessionId: string, expiresAt: Date): Promise<void>;
}
Expand Down
3 changes: 2 additions & 1 deletion docs/pages/reference/main/DatabaseSession.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ Represents a session stored in a database.

```ts
//$ DatabaseSessionAttributes=/reference/main/DatabaseSessionAttributes
//$ UserId=/reference/main/UserId
interface DatabaseSession {
id: string;
userId: string;
userId: $$UserId;
expiresAt: Date;
attributes: $$DatabaseSessionAttributes;
}
Expand Down
3 changes: 2 additions & 1 deletion docs/pages/reference/main/DatabaseUser.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ Represents a user stored in a database.

```ts
//$ DatabaseUserAttributes=/reference/main/DatabaseUserAttributes
//$ UserId=/reference/main/UserId
interface DatabaseUser {
id: string;
id: $$UserId;
attributes: DatabaseUserAttributes;
}
```
Expand Down
6 changes: 5 additions & 1 deletion docs/pages/reference/main/Lucia/createSession.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ Method of [`Lucia`](/reference/main/Lucia). Creates a new session.
```ts
//$ DatabaseSessionAttributes=/reference/main/DatabaseSessionAttributes
//$ Session=/reference/main/Session
function createSession(userId: string, attributes: $$DatabaseSessionAttributes): Promise<$$Session>;
//$ UserId=/reference/main/UserId
function createSession(
userId: $$UserId,
attributes: $$DatabaseSessionAttributes
): Promise<$$Session>;
```

### Parameters
Expand Down
3 changes: 2 additions & 1 deletion docs/pages/reference/main/Lucia/getUserSessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Method of [`Lucia`](/reference/main/Lucia). Gets all sessions of a user.

```ts
//$ Session=/reference/main/Session
function getUserSessions(userId: string): Promise<Session[]>;
//$ UserId=/reference/main/UserId
function getUserSessions(userId: $$UserId): Promise<Session[]>;
```

### Parameters
Expand Down
3 changes: 2 additions & 1 deletion docs/pages/reference/main/Lucia/invalidateUserSessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Method of [`Lucia`](/reference/main/Lucia). Invalidates all sessions of a user.
## Definition

```ts
function invalidateUserSessions(userId: string): Promise<void>;
//$ UserId=/reference/main/UserId
function invalidateUserSessions(userId: $$UserId): Promise<void>;
```

### Parameters
Expand Down
5 changes: 3 additions & 2 deletions docs/pages/reference/main/Session.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ Represents a session.

```ts
//$ SessionAttributes=/reference/main/SessionAttributes
interface Session extends SessionAttributes {
//$ UserId=/reference/main/UserId
interface Session extends $$SessionAttributes {
id: string;
expiresAt: Date;
fresh: boolean;
userId: string;
userId: $$UserId;
}
```

Expand Down
5 changes: 3 additions & 2 deletions docs/pages/reference/main/User.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ Represents a user.

```ts
//$ UserAttributes=/reference/main/UserAttributes
interface User extends UserAttributes {
id: string;
//$ UserId=/reference/main/UserId
interface User extends $$UserAttributes {
id: $$UserId;
}
```

Expand Down
13 changes: 13 additions & 0 deletions docs/pages/reference/main/UserId.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: "UserId"
---

# `UserId`

User ID.

## Definition

```ts
type UserId = string;
```
2 changes: 1 addition & 1 deletion docs/pages/upgrade-v3/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,6 @@ Make sure to install `oslo` as a `dependency`, not as `devDependency` to prevent

As per SvelteKit documentation :

>Development dependencies will be bundled into your app using Rollup. To control whether a given package is bundled or externalised, place it in devDependencies or dependencies respectively in your package.json.
> Development dependencies will be bundled into your app using Rollup. To control whether a given package is bundled or externalised, place it in devDependencies or dependencies respectively in your package.json.
See [SvelteKit documentation](https://kit.svelte.dev/docs/adapter-node#deploying) for details.
2 changes: 1 addition & 1 deletion packages/adapter-drizzle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 1.0.2

- Fix Drizzle type definitions ([#1426](https://github.com/lucia-auth/lucia/pull/1426))
- Fix Drizzle type definitions ([#1426](https://github.com/lucia-auth/lucia/pull/1426))

## 1.0.1

Expand Down
9 changes: 9 additions & 0 deletions packages/adapter-drizzle/tests/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare module "lucia" {
interface Register {
DatabaseUserAttributes: {
username: string;
};
}
}

export {};
9 changes: 9 additions & 0 deletions packages/adapter-mongodb/tests/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare module "lucia" {
interface Register {
DatabaseUserAttributes: {
username: string;
};
}
}

export {};
8 changes: 0 additions & 8 deletions packages/adapter-mongodb/tests/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,3 @@ await User.deleteMany({});
await Session.deleteMany({});

process.exit(0);

declare module "lucia" {
interface Register {
DatabaseUserAttributes: {
username: string;
};
}
}
8 changes: 0 additions & 8 deletions packages/adapter-mongodb/tests/mongoose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,3 @@ await User.deleteMany();
await Session.deleteMany();

process.exit(0);

declare module "lucia" {
interface Register {
DatabaseUserAttributes: {
username: string;
};
}
}
9 changes: 9 additions & 0 deletions packages/adapter-mysql/tests/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare module "lucia" {
interface Register {
DatabaseUserAttributes: {
username: string;
};
}
}

export {};
9 changes: 9 additions & 0 deletions packages/adapter-postgresql/tests/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare module "lucia" {
interface Register {
DatabaseUserAttributes: {
username: string;
};
}
}

export {};
8 changes: 0 additions & 8 deletions packages/adapter-postgresql/tests/neon-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,3 @@ await sql("DROP TABLE public.session");
await sql("DROP TABLE public.user");

process.exit();

declare module "lucia" {
interface Register {
DatabaseUserAttributes: {
username: string;
};
}
}
8 changes: 0 additions & 8 deletions packages/adapter-postgresql/tests/node-postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,3 @@ await pool.query("DROP TABLE public.session");
await pool.query("DROP TABLE public.user");

process.exit();

declare module "lucia" {
interface Register {
DatabaseUserAttributes: {
username: string;
};
}
}
8 changes: 0 additions & 8 deletions packages/adapter-postgresql/tests/postgresjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,3 @@ await sql`DROP TABLE public.session`;
await sql`DROP TABLE public.user`;

process.exit();

declare module "lucia" {
interface Register {
DatabaseUserAttributes: {
username: string;
};
}
}
6 changes: 3 additions & 3 deletions packages/adapter-sqlite/src/drivers/better-sqlite3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ class BetterSqlite3Controller implements Controller {
}

public async get<T>(sql: string, args: any[]): Promise<T | null> {
return this.db.prepare(sql).get(...args);
return this.db.prepare(sql).get(...args) as T | null;
}

public async getAll<T>(sql: string, args: any[]): Promise<T[]> {
return this.db.prepare(sql).all(...args);
return this.db.prepare(sql).all(...args) as T[];
}

public async execute(sql: string, args: any[]): Promise<void> {
await this.db.prepare(sql).run(...args);
this.db.prepare(sql).run(...args);
}
}
2 changes: 2 additions & 0 deletions packages/adapter-sqlite/tests/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ declare module "lucia" {
};
}
}

export {};
6 changes: 5 additions & 1 deletion packages/lucia/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# lucia

## 3.1.0

- [#1472](https://github.com/lucia-auth/lucia/pull/1472) : Add option to configure user ID type

## 3.0.1

- Fix `LegacyScrypt` generating malformed hash (see [#1370](https://github.com/lucia-auth/lucia/pull/1370)) - no security concerns
- Fix `LegacyScrypt` generating malformed hash (see [#1370](https://github.com/lucia-auth/lucia/pull/1370) - no security concerns)

## 3.0.0

Expand Down
2 changes: 1 addition & 1 deletion packages/lucia/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lucia",
"version": "3.0.1",
"version": "3.1.0",
"description": "A simple and flexible authentication library",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
13 changes: 7 additions & 6 deletions packages/lucia/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import type { Adapter } from "./database.js";
import type {
RegisteredDatabaseSessionAttributes,
RegisteredDatabaseUserAttributes,
RegisteredLucia
RegisteredLucia,
UserId
} from "./index.js";
import { CookieAttributes } from "oslo/cookie";

Expand All @@ -23,11 +24,11 @@ export interface Session extends SessionAttributes {
id: string;
expiresAt: Date;
fresh: boolean;
userId: string;
userId: UserId;
}

export interface User extends UserAttributes {
id: string;
id: UserId;
}

export class Lucia<
Expand Down Expand Up @@ -98,7 +99,7 @@ export class Lucia<
);
}

public async getUserSessions(userId: string): Promise<Session[]> {
public async getUserSessions(userId: UserId): Promise<Session[]> {
const databaseSessions = await this.adapter.getUserSessions(userId);
const sessions: Session[] = [];
for (const databaseSession of databaseSessions) {
Expand Down Expand Up @@ -154,7 +155,7 @@ export class Lucia<
}

public async createSession(
userId: string,
userId: UserId,
attributes: RegisteredDatabaseSessionAttributes,
options?: {
sessionId?: string;
Expand Down Expand Up @@ -182,7 +183,7 @@ export class Lucia<
await this.adapter.deleteSession(sessionId);
}

public async invalidateUserSessions(userId: string): Promise<void> {
public async invalidateUserSessions(userId: UserId): Promise<void> {
await this.adapter.deleteUserSessions(userId);
}

Expand Down
11 changes: 6 additions & 5 deletions packages/lucia/src/database.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import type {
RegisteredDatabaseSessionAttributes,
RegisteredDatabaseUserAttributes
RegisteredDatabaseUserAttributes,
UserId
} from "./index.js";

export interface Adapter {
getSessionAndUser(
sessionId: string
): Promise<[session: DatabaseSession | null, user: DatabaseUser | null]>;
getUserSessions(userId: string): Promise<DatabaseSession[]>;
getUserSessions(userId: UserId): Promise<DatabaseSession[]>;
setSession(session: DatabaseSession): Promise<void>;
updateSessionExpiration(sessionId: string, expiresAt: Date): Promise<void>;
deleteSession(sessionId: string): Promise<void>;
deleteUserSessions(userId: string): Promise<void>;
deleteUserSessions(userId: UserId): Promise<void>;
deleteExpiredSessions(): Promise<void>;
}

export interface DatabaseUser {
id: string;
id: UserId;
attributes: RegisteredDatabaseUserAttributes;
}

export interface DatabaseSession {
userId: string;
expiresAt: Date;
id: string;
id: UserId;
attributes: RegisteredDatabaseSessionAttributes;
}
Loading

0 comments on commit 4e4f615

Please sign in to comment.