authjs-knexjs-adapter
Advanced tools
Comparing version 1.1.1 to 2.0.0
72
index.js
@@ -1,72 +0,6 @@ | ||
/* | ||
Auth.js requires a specific database schema. Use the following Knex.js | ||
migration to create it. Note: This migration is configured for MariaDB. It may | ||
work out-of-the-box with other database engines, or you may need to modify it | ||
according to your application. | ||
export const up = (knex) => { | ||
return knex.schema | ||
.createTable("User", (table) => { | ||
table.uuid("id").defaultTo(knex.raw("(UUID())")); | ||
table.text("name"); | ||
table.text("email").unique(); | ||
table.timestamp("emailVerified"); | ||
table.text("image"); | ||
table.primary("id"); | ||
}) | ||
.createTable("Session", (table) => { | ||
table.uuid("id").defaultTo(knex.raw("(UUID())")); | ||
table.timestamp("expires"); | ||
table.text("sessionToken").notNullable().unique(); | ||
table.uuid("userId"); | ||
table.primary("id"); | ||
table.foreign("userId").references("id").on("User"); | ||
}) | ||
.createTable("Account", (table) => { | ||
table.uuid("id").defaultTo(knex.raw("(UUID())")); | ||
table.uuid("userId"); | ||
table.text("type").notNullable(); | ||
table.text("provider").notNullable(); | ||
table.text("providerAccountId").notNullable(); | ||
table.text("refresh_token"); | ||
table.text("access_token"); | ||
table.bigInteger("expires_at"); | ||
table.text("token_type"); | ||
table.text("scope"); | ||
table.text("id_token"); | ||
table.text("session_state"); | ||
table.primary("id"); | ||
table.unique(["provider", "providerAccountId"]); | ||
table.foreign("userId").references("id").on("User"); | ||
}) | ||
.createTable("VerificationToken", (table) => { | ||
table.text("identifier"); | ||
table.string("token", 255); | ||
table.timestamp("expires").notNullable(); | ||
table.primary("token"); | ||
table.unique(["token", "identifier"]); | ||
}); | ||
}; | ||
export const down = (knex) => { | ||
return knex.schema | ||
.dropTable("VerificationToken") | ||
.dropTable("Account") | ||
.dropTable("Session") | ||
.dropTable("User"); | ||
}; | ||
*/ | ||
/** | ||
* A Next-Auth adapter for Knex.js | ||
* An adapter for Auth.js/NextAuth.js to allow you to connect to any database | ||
* service via Knex.js. | ||
* | ||
* @param knex - The Knex.js connection | ||
* @param {Knex} knex - The Knex.js connection | ||
* @param options - Auth.js options | ||
@@ -73,0 +7,0 @@ * @returns {Adapter} - An Auth.js adapter for Knex.js |
{ | ||
"name": "authjs-knexjs-adapter", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "An adapter for Auth.js/NextAuth.js to allow you to connect to any database service via Knex.js.", | ||
@@ -36,7 +36,8 @@ "exports": "./index.js", | ||
}, | ||
"types": "./index.d.ts", | ||
"files": [ | ||
"index.js", | ||
"types/index.d.ts" | ||
"*.js", | ||
"*.d.ts*", | ||
"src" | ||
], | ||
"types": "types/index.d.ts", | ||
"devDependencies": { | ||
@@ -43,0 +44,0 @@ "knex": "^2.4.2", |
107
README.md
@@ -6,2 +6,6 @@ # Auth.js Knex.js Adapter | ||
<p align="center"> | ||
<img src="./logo.png" /> | ||
</p> | ||
## Installation | ||
@@ -15,9 +19,15 @@ | ||
Import it into `pages/api/auth/[...nextauth].ts`, give it a Knex.js database | ||
connection, and set it as the `adapter` in your `NextAuth()` configuration. | ||
Note: Make sure you also install `knex` and the appropriate database library for | ||
your environment (e.g. `mysql2`). | ||
### Next.js | ||
Import the adapter into `pages/api/auth/[...nextauth].ts`, give it a Knex.js | ||
database connection, and set it as the `adapter` in your `NextAuth()` | ||
configuration. | ||
```javascript | ||
import { KnexAdapter } from "authjs-knexjs-adapter"; | ||
import knex from "knex"; | ||
import NextAuth from "next-auth"; | ||
import knex from "knex"; | ||
import { KnexAdapter } from "authjs-knexjs-adapter"; | ||
@@ -41,71 +51,44 @@ const db = knex({ | ||
## Database Schema | ||
### SvelteKit | ||
Auth.js requires a specific database schema. Use the following Knex.js | ||
migration to create it. Note: This migration is configured for MariaDB. It may | ||
work out-of-the-box with other database engines, or you may need to modify it | ||
according to your application. | ||
Import the adapter into `src/hooks.server.js`, give it a Knex.js database | ||
connection, and set it as the `adapter` in your `SvelteKitAuth` configuration. | ||
```javascript | ||
export const up = (knex) => { | ||
return knex.schema | ||
.createTable("User", (table) => { | ||
table.uuid("id").defaultTo(knex.raw("(UUID())")); | ||
table.text("name"); | ||
table.text("email").unique(); | ||
table.timestamp("emailVerified"); | ||
table.text("image"); | ||
import { DB_HOST, DB_PORT, DB_USER, DB_PASSWORD, DB_DATABASE } from '$env/static/private'; | ||
import { KnexAdapter } from 'authjs-knexjs-adapter'; | ||
import { SvelteKitAuth } from '@auth/sveltekit'; | ||
import knex from "knex"; | ||
table.primary("id"); | ||
}) | ||
.createTable("Session", (table) => { | ||
table.uuid("id").defaultTo(knex.raw("(UUID())")); | ||
table.timestamp("expires"); | ||
table.text("sessionToken").notNullable().unique(); | ||
table.uuid("userId"); | ||
const db = knex({ | ||
client: 'mysql2', | ||
connection: { | ||
host: DB_HOST, | ||
port: parseInt(DB_PORT ?? '3306', 10), | ||
user: DB_USER, | ||
password: DB_PASSWORD, | ||
database: DB_DATABASE | ||
} | ||
}); | ||
table.primary("id"); | ||
export const handle = SvelteKitAuth({ | ||
adapter: KnexAdapter(db), | ||
providers: [ /* your providers */ ], | ||
}); | ||
``` | ||
table.foreign("userId").references("id").on("User"); | ||
}) | ||
.createTable("Account", (table) => { | ||
table.uuid("id").defaultTo(knex.raw("(UUID())")); | ||
table.uuid("userId"); | ||
table.text("type").notNullable(); | ||
table.text("provider").notNullable(); | ||
table.text("providerAccountId").notNullable(); | ||
table.text("refresh_token"); | ||
table.text("access_token"); | ||
table.bigInteger("expires_at"); | ||
table.text("token_type"); | ||
table.text("scope"); | ||
table.text("id_token"); | ||
table.text("session_state"); | ||
## Database Schema | ||
table.primary("id"); | ||
Auth.js requires a specific database schema. There are two options provided here | ||
to create this schema: | ||
table.unique(["provider", "providerAccountId"]); | ||
- [A Knex.js migration file](./migrations/migration.js) | ||
- [A schema dump SQL file](./migrations/migration.sql) | ||
table.foreign("userId").references("id").on("User"); | ||
}) | ||
.createTable("VerificationToken", (table) => { | ||
table.text("identifier"); | ||
table.string("token", 255); | ||
table.timestamp("expires").notNullable(); | ||
Use either option or create the database schema some other way. | ||
table.primary("token"); | ||
Note: These files are configured for MariaDB. They may work out-of-the-box with | ||
other database engines, or you may need to modify them according to your | ||
application. | ||
table.unique(["token", "identifier"]); | ||
}); | ||
}; | ||
export const down = (knex) => { | ||
return knex.schema | ||
.dropTable("VerificationToken") | ||
.dropTable("Account") | ||
.dropTable("Session") | ||
.dropTable("User"); | ||
}; | ||
``` | ||
## License | ||
@@ -112,0 +95,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19921
7
359
113