Skip to content

Commit

Permalink
Drizzle SQLite adapter: Fix previous change (#1501)
Browse files Browse the repository at this point in the history
  • Loading branch information
pilcrowonpaper authored Mar 21, 2024
1 parent fd66ddb commit ea98e37
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github: pilcrowOnPaper
github: pilcrowOnPaper
2 changes: 1 addition & 1 deletion docs/pages/getting-started/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,4 @@ module.exports = {

## The Copenhagen Book

This documentation often references [the Copenhagen Book](https://thecopenhagenbook.com/mfa). This is an open-source guide on implementing auth and should come in handy when implementing anything auth, including passkeys, multi-factor authentication, and a bit of cryptography. We recommend reading it to learn more about auth in web applications.
This documentation often references [the Copenhagen Book](https://thecopenhagenbook.com/mfa). This is an open-source guide on implementing auth and should come in handy when implementing anything auth, including passkeys, multi-factor authentication, and a bit of cryptography. We recommend reading it to learn more about auth in web applications.
2 changes: 1 addition & 1 deletion docs/pages/getting-started/nextjs-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ You can learn all the concepts and APIs by reading the [Basics section](/basics/

This documentation often references [the Copenhagen Book](https://thecopenhagenbook.com/mfa). This is an open-source guide on implementing auth and should come in handy when implementing anything auth, including passkeys, multi-factor authentication, and a bit of cryptography. We recommend reading it to learn more about auth in web applications.

If you have any questions, [join our Discord server](https://discord.com/invite/PwrK3kpVR3)!
If you have any questions, [join our Discord server](https://discord.com/invite/PwrK3kpVR3)!
3 changes: 1 addition & 2 deletions docs/pages/getting-started/nextjs-pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,4 @@ You can learn all the concepts and APIs by reading the [Basics section](/basics/

This documentation often references [the Copenhagen Book](https://thecopenhagenbook.com/mfa). This is an open-source guide on implementing auth and should come in handy when implementing anything auth, including passkeys, multi-factor authentication, and a bit of cryptography. We recommend reading it to learn more about auth in web applications.


If you have any questions, [join our Discord server](https://discord.com/invite/PwrK3kpVR3)!
If you have any questions, [join our Discord server](https://discord.com/invite/PwrK3kpVR3)!
9 changes: 2 additions & 7 deletions docs/pages/guides/email-and-password/password-reset.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ app.get("/reset-password/:token", async () => {
"Referrer-Policy": "no-referrer"
}
});
})
});
```

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. Make sure to set the `Referrer-Policy` header here as well.
Expand All @@ -107,10 +107,7 @@ app.post("/reset-password/:token", async () => {
// ...

const tokenHash = encodeHex(await sha256(new TextEncoder().encode(verificationToken)));
const token = await db
.table("password_reset_token")
.where("token_hash", "=", tokenHash)
.get();
const token = await db.table("password_reset_token").where("token_hash", "=", tokenHash).get();
if (token) {
await db.table("password_reset_token").where("token_hash", "=", tokenHash).delete();
}
Expand Down Expand Up @@ -139,5 +136,3 @@ app.post("/reset-password/:token", async () => {
});
});
```


2 changes: 1 addition & 1 deletion docs/pages/guides/oauth/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ For a step-by-step, framework-specific tutorial, see the [GitHub OAuth](/tutoria
- [Account linking](/guides/oauth/account-linking)
- [Custom OAuth providers](/guides/oauth/custom-providers)

We recommend reading through the [OAuth guide](https://thecopenhagenbook.com/oauth) in the Copenhagen Book.
We recommend reading through the [OAuth guide](https://thecopenhagenbook.com/oauth) in the Copenhagen Book.
8 changes: 6 additions & 2 deletions packages/adapter-drizzle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# @lucia-auth/adapter-drizzle

## 1.0.7

- Fix previous revert in 1.0.6. ([#1501](https://github.com/lucia-auth/lucia/pull/1501))

## 1.0.6

- Revert previous optimization for D1 support.
- Revert previous optimization for D1 support ([#1500](https://github.com/lucia-auth/lucia/pull/1500)).

## 1.0.5

- Fix table types ([#1495](https://github.com/lucia-auth/lucia/pull/1495)).
- Fix table types ([#1495](https://github.com/lucia-auth/lucia/pull/1495)).

## 1.0.4

Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-drizzle/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lucia-auth/adapter-drizzle",
"version": "1.0.6",
"version": "1.0.7",
"description": "Drizzle ORM adapter for Lucia",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
33 changes: 10 additions & 23 deletions packages/adapter-drizzle/src/drivers/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,19 @@ export class DrizzleMySQLAdapter implements Adapter {
public async getSessionAndUser(
sessionId: string
): Promise<[session: DatabaseSession | null, user: DatabaseUser | null]> {
// https://github.com/drizzle-team/drizzle-orm/issues/555
const [databaseSession, databaseUser] = await Promise.all([
this.getSession(sessionId),
this.getUserFromSessionId(sessionId)
]);
return [databaseSession, databaseUser];
}

private async getSession(sessionId: string): Promise<DatabaseSession | null> {
const result = await this.db
.select()
.select({
user: this.userTable,
session: this.sessionTable
})
.from(this.sessionTable)
.innerJoin(this.userTable, eq(this.sessionTable.userId, this.userTable.id))
.where(eq(this.sessionTable.id, sessionId));
if (result.length !== 1) return null;
return transformIntoDatabaseSession(result[0]);
if (result.length !== 1) return [null, null];
return [
transformIntoDatabaseSession(result[0].session),
transformIntoDatabaseUser(result[0].user)
];
}

public async getUserSessions(userId: UserId): Promise<DatabaseSession[]> {
Expand All @@ -58,17 +56,6 @@ export class DrizzleMySQLAdapter implements Adapter {
});
}

private async getUserFromSessionId(sessionId: string): Promise<DatabaseUser | null> {
const { _, $inferInsert, $inferSelect, getSQL, ...userColumns } = this.userTable;
const result = await this.db
.select(userColumns)
.from(this.sessionTable)
.innerJoin(this.userTable, eq(this.sessionTable.userId, this.userTable.id))
.where(eq(this.sessionTable.id, sessionId));
if (result.length !== 1) return null;
return transformIntoDatabaseUser(result[0]);
}

public async setSession(session: DatabaseSession): Promise<void> {
await this.db.insert(this.sessionTable).values({
id: session.id,
Expand Down
34 changes: 23 additions & 11 deletions packages/adapter-drizzle/src/drivers/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,32 @@ export class DrizzleSQLiteAdapter implements Adapter {
public async getSessionAndUser(
sessionId: string
): Promise<[session: DatabaseSession | null, user: DatabaseUser | null]> {
// https://github.com/drizzle-team/drizzle-orm/issues/555
const [databaseSession, databaseUser] = await Promise.all([
this.getSession(sessionId),
this.getUserFromSessionId(sessionId)
]);
return [databaseSession, databaseUser];
}

private async getSession(sessionId: string): Promise<DatabaseSession | null> {
const result = await this.db
.select({
user: this.userTable,
session: this.sessionTable
})
.select()
.from(this.sessionTable)
.where(eq(this.sessionTable.id, sessionId));
if (result.length !== 1) return null;
return transformIntoDatabaseSession(result[0]);
}

private async getUserFromSessionId(sessionId: string): Promise<DatabaseUser | null> {
const { _, $inferInsert, $inferSelect, getSQL, ...userColumns } = this.userTable;
const result = await this.db
.select(userColumns)
.from(this.sessionTable)
.innerJoin(this.userTable, eq(this.sessionTable.userId, this.userTable.id))
.where(eq(this.sessionTable.id, sessionId))
.get();
if (!result) return [null, null];
return [
transformIntoDatabaseSession(result.session),
transformIntoDatabaseUser(result.user)
];
.where(eq(this.sessionTable.id, sessionId));
if (result.length !== 1) return null;
return transformIntoDatabaseUser(result[0]);
}

public async getUserSessions(userId: UserId): Promise<DatabaseSession[]> {
Expand Down

0 comments on commit ea98e37

Please sign in to comment.