nuxt-users 1.6.3 → 1.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/dist/cli.mjs +3 -4
- package/dist/module.d.mts +114 -1
- package/dist/module.d.ts +114 -1
- package/dist/module.json +1 -1
- package/dist/runtime/plugin.js +1 -1
- package/dist/runtime/server/api/login.post.js +1 -1
- package/dist/runtime/server/api/logout.get.js +1 -1
- package/dist/runtime/server/api/profile.get.js +1 -1
- package/dist/runtime/server/api/update-password.post.js +2 -1
- package/dist/runtime/server/services/password.js +1 -1
- package/dist/runtime/server/utils/create-migrations-table.d.ts +2 -0
- package/dist/runtime/server/utils/create-migrations-table.js +35 -0
- package/dist/runtime/server/utils/create-password-reset-tokens-table.d.ts +2 -0
- package/dist/runtime/server/utils/create-password-reset-tokens-table.js +40 -0
- package/dist/runtime/server/utils/create-personal-access-tokens-table.d.ts +2 -0
- package/dist/runtime/server/utils/create-personal-access-tokens-table.js +56 -0
- package/dist/runtime/server/utils/create-user.d.ts +1 -0
- package/dist/runtime/server/utils/create-user.js +1 -0
- package/dist/runtime/server/utils/create-users-table.d.ts +2 -0
- package/dist/runtime/server/utils/create-users-table.js +47 -0
- package/dist/runtime/server/utils/db.d.ts +4 -0
- package/dist/runtime/server/utils/db.js +46 -0
- package/dist/runtime/server/utils/index.d.ts +7 -0
- package/dist/runtime/server/utils/index.js +7 -0
- package/dist/runtime/server/utils/migrate.d.ts +4 -0
- package/dist/runtime/server/utils/migrate.js +63 -0
- package/dist/runtime/server/utils/user.d.ts +27 -0
- package/dist/runtime/server/utils/user.js +94 -0
- package/dist/utils.d.mts +1 -42
- package/dist/utils.d.ts +1 -42
- package/dist/utils.mjs +162 -3
- package/package.json +2 -1
- package/dist/shared/nuxt-users.CMEtv_Mw.d.mts +0 -124
- package/dist/shared/nuxt-users.CMEtv_Mw.d.ts +0 -124
- package/dist/shared/nuxt-users.C_UkhLJ9.mjs +0 -531
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
3
|
|
|
4
|
+
## v1.6.4
|
|
5
|
+
|
|
6
|
+
[compare changes](https://github.com/rrd108/nuxt-users/compare/v1.6.3...v1.6.4)
|
|
7
|
+
|
|
8
|
+
### 💅 Refactors
|
|
9
|
+
|
|
10
|
+
- Move utils to runtime/server ([78a5b50](https://github.com/rrd108/nuxt-users/commit/78a5b50))
|
|
11
|
+
|
|
12
|
+
### 🏡 Chore
|
|
13
|
+
|
|
14
|
+
- **release:** V1.6.3 ([365cfe2](https://github.com/rrd108/nuxt-users/commit/365cfe2))
|
|
15
|
+
- Fix formatting ([fdec442](https://github.com/rrd108/nuxt-users/commit/fdec442))
|
|
16
|
+
|
|
17
|
+
### ❤️ Contributors
|
|
18
|
+
|
|
19
|
+
- Rrd108 ([@rrd108](https://github.com/rrd108))
|
|
20
|
+
|
|
4
21
|
## v1.6.3
|
|
5
22
|
|
|
6
23
|
[compare changes](https://github.com/rrd108/nuxt-users/compare/v1.6.2...v1.6.3)
|
package/dist/cli.mjs
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { defineCommand, runMain } from 'citty';
|
|
3
|
-
import '
|
|
4
|
-
import 'bcrypt';
|
|
5
|
-
import { r as runMigrations, j as getPasswordValidationOptions, v as validatePassword, a as createUser$1, d as createUsersTable$1, e as createPersonalAccessTokensTable$1, h as createPasswordResetTokensTable$1, i as createMigrationsTable$1 } from './shared/nuxt-users.C_UkhLJ9.mjs';
|
|
3
|
+
import { runMigrations, createUser as createUser$1, createUsersTable as createUsersTable$1, createPersonalAccessTokensTable as createPersonalAccessTokensTable$1, createPasswordResetTokensTable as createPasswordResetTokensTable$1, createMigrationsTable as createMigrationsTable$1 } from '../dist/runtime/server/utils/index.js';
|
|
6
4
|
import { defaultOptions } from './module.mjs';
|
|
7
5
|
import { loadNuxt } from '@nuxt/kit';
|
|
6
|
+
import { getPasswordValidationOptions, validatePassword } from './utils.mjs';
|
|
8
7
|
import 'defu';
|
|
9
8
|
|
|
10
9
|
const getOptionsFromEnv = () => {
|
|
@@ -248,7 +247,7 @@ const main = defineCommand({
|
|
|
248
247
|
meta: {
|
|
249
248
|
name: "nuxt-users",
|
|
250
249
|
description: "CLI for Nuxt Users Module - Manage users, migrations, and database operations",
|
|
251
|
-
version: "1.
|
|
250
|
+
version: "1.6.3"
|
|
252
251
|
},
|
|
253
252
|
subCommands: {
|
|
254
253
|
migrate,
|
package/dist/module.d.mts
CHANGED
|
@@ -1,5 +1,118 @@
|
|
|
1
1
|
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
type DatabaseType = 'sqlite' | 'mysql' | 'postgresql';
|
|
4
|
+
type DatabaseConfig = {
|
|
5
|
+
path?: string;
|
|
6
|
+
host?: string;
|
|
7
|
+
port?: number;
|
|
8
|
+
user?: string;
|
|
9
|
+
password?: string;
|
|
10
|
+
database?: string;
|
|
11
|
+
};
|
|
12
|
+
interface RuntimeModuleOptions {
|
|
13
|
+
connector?: {
|
|
14
|
+
name: DatabaseType;
|
|
15
|
+
options: DatabaseConfig;
|
|
16
|
+
};
|
|
17
|
+
tables?: {
|
|
18
|
+
migrations?: string;
|
|
19
|
+
users?: string;
|
|
20
|
+
personalAccessTokens?: string;
|
|
21
|
+
passwordResetTokens?: string;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Mailer configuration options for sending emails (e.g., password resets)
|
|
25
|
+
* Uses nodemailer
|
|
26
|
+
*/
|
|
27
|
+
mailer?: MailerOptions;
|
|
28
|
+
/**
|
|
29
|
+
* Base URL for password reset links
|
|
30
|
+
* @default 'http://localhost:3000' // Example, will be set in module defaults
|
|
31
|
+
*/
|
|
32
|
+
passwordResetBaseUrl?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Skip database checks during module setup to prevent hanging
|
|
35
|
+
* @default false
|
|
36
|
+
*/
|
|
37
|
+
auth?: {
|
|
38
|
+
/**
|
|
39
|
+
* Whitelisted routes that do not require authentication
|
|
40
|
+
* @default ['/login']
|
|
41
|
+
* @example ['/login', '/register']
|
|
42
|
+
*/
|
|
43
|
+
whitelist?: string[];
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Password validation configuration
|
|
47
|
+
*/
|
|
48
|
+
passwordValidation?: {
|
|
49
|
+
/**
|
|
50
|
+
* Minimum password length
|
|
51
|
+
* @default 8
|
|
52
|
+
*/
|
|
53
|
+
minLength?: number;
|
|
54
|
+
/**
|
|
55
|
+
* Require uppercase letters
|
|
56
|
+
* @default true
|
|
57
|
+
*/
|
|
58
|
+
requireUppercase?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Require lowercase letters
|
|
61
|
+
* @default true
|
|
62
|
+
*/
|
|
63
|
+
requireLowercase?: boolean;
|
|
64
|
+
/**
|
|
65
|
+
* Require numbers
|
|
66
|
+
* @default true
|
|
67
|
+
*/
|
|
68
|
+
requireNumbers?: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Require special characters
|
|
71
|
+
* @default true
|
|
72
|
+
*/
|
|
73
|
+
requireSpecialChars?: boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Prevent common passwords
|
|
76
|
+
* @default true
|
|
77
|
+
*/
|
|
78
|
+
preventCommonPasswords?: boolean;
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
interface ModuleOptions extends Omit<RuntimeModuleOptions, 'tables' | 'auth' | 'passwordValidation'> {
|
|
82
|
+
connector: {
|
|
83
|
+
name: DatabaseType;
|
|
84
|
+
options: DatabaseConfig;
|
|
85
|
+
};
|
|
86
|
+
tables: {
|
|
87
|
+
migrations: string;
|
|
88
|
+
users: string;
|
|
89
|
+
personalAccessTokens: string;
|
|
90
|
+
passwordResetTokens: string;
|
|
91
|
+
};
|
|
92
|
+
auth: {
|
|
93
|
+
whitelist: string[];
|
|
94
|
+
};
|
|
95
|
+
passwordValidation: {
|
|
96
|
+
minLength: number;
|
|
97
|
+
requireUppercase: boolean;
|
|
98
|
+
requireLowercase: boolean;
|
|
99
|
+
requireNumbers: boolean;
|
|
100
|
+
requireSpecialChars: boolean;
|
|
101
|
+
preventCommonPasswords: boolean;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
interface MailerOptions {
|
|
105
|
+
host: string;
|
|
106
|
+
port: number;
|
|
107
|
+
secure?: boolean;
|
|
108
|
+
auth: {
|
|
109
|
+
user: string;
|
|
110
|
+
pass: string;
|
|
111
|
+
};
|
|
112
|
+
defaults?: {
|
|
113
|
+
from: string;
|
|
114
|
+
};
|
|
115
|
+
}
|
|
3
116
|
|
|
4
117
|
declare const defaultOptions: ModuleOptions;
|
|
5
118
|
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
package/dist/module.d.ts
CHANGED
|
@@ -1,5 +1,118 @@
|
|
|
1
1
|
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
type DatabaseType = 'sqlite' | 'mysql' | 'postgresql';
|
|
4
|
+
type DatabaseConfig = {
|
|
5
|
+
path?: string;
|
|
6
|
+
host?: string;
|
|
7
|
+
port?: number;
|
|
8
|
+
user?: string;
|
|
9
|
+
password?: string;
|
|
10
|
+
database?: string;
|
|
11
|
+
};
|
|
12
|
+
interface RuntimeModuleOptions {
|
|
13
|
+
connector?: {
|
|
14
|
+
name: DatabaseType;
|
|
15
|
+
options: DatabaseConfig;
|
|
16
|
+
};
|
|
17
|
+
tables?: {
|
|
18
|
+
migrations?: string;
|
|
19
|
+
users?: string;
|
|
20
|
+
personalAccessTokens?: string;
|
|
21
|
+
passwordResetTokens?: string;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Mailer configuration options for sending emails (e.g., password resets)
|
|
25
|
+
* Uses nodemailer
|
|
26
|
+
*/
|
|
27
|
+
mailer?: MailerOptions;
|
|
28
|
+
/**
|
|
29
|
+
* Base URL for password reset links
|
|
30
|
+
* @default 'http://localhost:3000' // Example, will be set in module defaults
|
|
31
|
+
*/
|
|
32
|
+
passwordResetBaseUrl?: string;
|
|
33
|
+
/**
|
|
34
|
+
* Skip database checks during module setup to prevent hanging
|
|
35
|
+
* @default false
|
|
36
|
+
*/
|
|
37
|
+
auth?: {
|
|
38
|
+
/**
|
|
39
|
+
* Whitelisted routes that do not require authentication
|
|
40
|
+
* @default ['/login']
|
|
41
|
+
* @example ['/login', '/register']
|
|
42
|
+
*/
|
|
43
|
+
whitelist?: string[];
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* Password validation configuration
|
|
47
|
+
*/
|
|
48
|
+
passwordValidation?: {
|
|
49
|
+
/**
|
|
50
|
+
* Minimum password length
|
|
51
|
+
* @default 8
|
|
52
|
+
*/
|
|
53
|
+
minLength?: number;
|
|
54
|
+
/**
|
|
55
|
+
* Require uppercase letters
|
|
56
|
+
* @default true
|
|
57
|
+
*/
|
|
58
|
+
requireUppercase?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Require lowercase letters
|
|
61
|
+
* @default true
|
|
62
|
+
*/
|
|
63
|
+
requireLowercase?: boolean;
|
|
64
|
+
/**
|
|
65
|
+
* Require numbers
|
|
66
|
+
* @default true
|
|
67
|
+
*/
|
|
68
|
+
requireNumbers?: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* Require special characters
|
|
71
|
+
* @default true
|
|
72
|
+
*/
|
|
73
|
+
requireSpecialChars?: boolean;
|
|
74
|
+
/**
|
|
75
|
+
* Prevent common passwords
|
|
76
|
+
* @default true
|
|
77
|
+
*/
|
|
78
|
+
preventCommonPasswords?: boolean;
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
interface ModuleOptions extends Omit<RuntimeModuleOptions, 'tables' | 'auth' | 'passwordValidation'> {
|
|
82
|
+
connector: {
|
|
83
|
+
name: DatabaseType;
|
|
84
|
+
options: DatabaseConfig;
|
|
85
|
+
};
|
|
86
|
+
tables: {
|
|
87
|
+
migrations: string;
|
|
88
|
+
users: string;
|
|
89
|
+
personalAccessTokens: string;
|
|
90
|
+
passwordResetTokens: string;
|
|
91
|
+
};
|
|
92
|
+
auth: {
|
|
93
|
+
whitelist: string[];
|
|
94
|
+
};
|
|
95
|
+
passwordValidation: {
|
|
96
|
+
minLength: number;
|
|
97
|
+
requireUppercase: boolean;
|
|
98
|
+
requireLowercase: boolean;
|
|
99
|
+
requireNumbers: boolean;
|
|
100
|
+
requireSpecialChars: boolean;
|
|
101
|
+
preventCommonPasswords: boolean;
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
interface MailerOptions {
|
|
105
|
+
host: string;
|
|
106
|
+
port: number;
|
|
107
|
+
secure?: boolean;
|
|
108
|
+
auth: {
|
|
109
|
+
user: string;
|
|
110
|
+
pass: string;
|
|
111
|
+
};
|
|
112
|
+
defaults?: {
|
|
113
|
+
from: string;
|
|
114
|
+
};
|
|
115
|
+
}
|
|
3
116
|
|
|
4
117
|
declare const defaultOptions: ModuleOptions;
|
|
5
118
|
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions, ModuleOptions, false>;
|
package/dist/module.json
CHANGED
package/dist/runtime/plugin.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { addRouteMiddleware, defineNuxtPlugin, useRuntimeConfig, navigateTo } from "#app";
|
|
2
|
-
import { checkTableExists } from "
|
|
2
|
+
import { checkTableExists } from "./server/utils/index.js";
|
|
3
3
|
import { useAuth } from "./composables/useAuth.js";
|
|
4
4
|
export default defineNuxtPlugin(async (_nuxtApp) => {
|
|
5
5
|
const { nuxtUsers } = useRuntimeConfig();
|
|
@@ -2,7 +2,7 @@ import { createError, defineEventHandler, readBody, setCookie } from "h3";
|
|
|
2
2
|
import bcrypt from "bcrypt";
|
|
3
3
|
import crypto from "node:crypto";
|
|
4
4
|
import { useRuntimeConfig } from "#imports";
|
|
5
|
-
import { useDb } from "
|
|
5
|
+
import { useDb } from "../utils/index.js";
|
|
6
6
|
export default defineEventHandler(async (event) => {
|
|
7
7
|
const body = await readBody(event);
|
|
8
8
|
const { email, password } = body;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { defineEventHandler, getCookie, setCookie } from "h3";
|
|
2
2
|
import { useRuntimeConfig } from "#imports";
|
|
3
|
-
import { useDb } from "
|
|
3
|
+
import { useDb } from "../utils/index.js";
|
|
4
4
|
export default defineEventHandler(async (event) => {
|
|
5
5
|
const { nuxtUsers } = useRuntimeConfig();
|
|
6
6
|
const options = nuxtUsers;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { createError, defineEventHandler, getCookie } from "h3";
|
|
2
2
|
import { useRuntimeConfig } from "#imports";
|
|
3
|
-
import { getCurrentUserFromToken } from "
|
|
3
|
+
import { getCurrentUserFromToken } from "../utils/index.js";
|
|
4
4
|
export default defineEventHandler(async (event) => {
|
|
5
5
|
const { nuxtUsers } = useRuntimeConfig();
|
|
6
6
|
const options = nuxtUsers;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { createError, defineEventHandler, getCookie, readBody } from "h3";
|
|
2
2
|
import bcrypt from "bcrypt";
|
|
3
3
|
import { useRuntimeConfig } from "#imports";
|
|
4
|
-
import { getCurrentUserFromToken, updateUserPassword
|
|
4
|
+
import { getCurrentUserFromToken, updateUserPassword } from "../utils/index.js";
|
|
5
|
+
import { validatePassword, getPasswordValidationOptions } from "../../../utils";
|
|
5
6
|
export default defineEventHandler(async (event) => {
|
|
6
7
|
const { nuxtUsers } = useRuntimeConfig();
|
|
7
8
|
const options = nuxtUsers;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createTransport } from "nodemailer";
|
|
2
2
|
import crypto from "node:crypto";
|
|
3
3
|
import bcrypt from "bcrypt";
|
|
4
|
-
import { findUserByEmail, updateUserPassword, useDb } from "
|
|
4
|
+
import { findUserByEmail, updateUserPassword, useDb } from "../utils/index.js";
|
|
5
5
|
const TOKEN_EXPIRATION_HOURS = 1;
|
|
6
6
|
export const sendPasswordResetLink = async (email, options) => {
|
|
7
7
|
const user = await findUserByEmail(email, options);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { useDb } from "./db.js";
|
|
2
|
+
export const createMigrationsTable = async (options) => {
|
|
3
|
+
const connectorName = options.connector.name;
|
|
4
|
+
const db = await useDb(options);
|
|
5
|
+
const tableName = "migrations";
|
|
6
|
+
console.log(`[Nuxt Users] DB:Create Migrations ${connectorName} Table Creating ${tableName} table with ${connectorName} connector...`);
|
|
7
|
+
if (connectorName === "sqlite") {
|
|
8
|
+
await db.sql`
|
|
9
|
+
CREATE TABLE IF NOT EXISTS {${tableName}} (
|
|
10
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
11
|
+
name TEXT NOT NULL UNIQUE,
|
|
12
|
+
executed_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
13
|
+
)
|
|
14
|
+
`;
|
|
15
|
+
}
|
|
16
|
+
if (connectorName === "mysql") {
|
|
17
|
+
await db.sql`
|
|
18
|
+
CREATE TABLE IF NOT EXISTS {${tableName}} (
|
|
19
|
+
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
20
|
+
name VARCHAR(255) NOT NULL UNIQUE,
|
|
21
|
+
executed_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
22
|
+
)
|
|
23
|
+
`;
|
|
24
|
+
}
|
|
25
|
+
if (connectorName === "postgresql") {
|
|
26
|
+
await db.sql`
|
|
27
|
+
CREATE TABLE IF NOT EXISTS {${tableName}} (
|
|
28
|
+
id SERIAL PRIMARY KEY,
|
|
29
|
+
name VARCHAR(255) NOT NULL UNIQUE,
|
|
30
|
+
executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
31
|
+
)
|
|
32
|
+
`;
|
|
33
|
+
}
|
|
34
|
+
console.log(`[Nuxt Users] DB:Create Migrations ${connectorName} Table successfull \u2705`);
|
|
35
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useDb } from "./db.js";
|
|
2
|
+
export const createPasswordResetTokensTable = async (options) => {
|
|
3
|
+
const connectorName = options.connector.name;
|
|
4
|
+
const db = await useDb(options);
|
|
5
|
+
const tableName = options.tables.passwordResetTokens;
|
|
6
|
+
console.log(`[Nuxt Users] DB:Create Password Reset Tokens ${connectorName} Table Creating ${tableName} table with ${connectorName} connector...`);
|
|
7
|
+
if (connectorName === "sqlite") {
|
|
8
|
+
await db.sql`
|
|
9
|
+
CREATE TABLE IF NOT EXISTS {${tableName}} (
|
|
10
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
11
|
+
email TEXT NOT NULL,
|
|
12
|
+
token TEXT NOT NULL UNIQUE,
|
|
13
|
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
14
|
+
)
|
|
15
|
+
`;
|
|
16
|
+
}
|
|
17
|
+
if (connectorName === "mysql") {
|
|
18
|
+
await db.sql`
|
|
19
|
+
CREATE TABLE IF NOT EXISTS {${tableName}} (
|
|
20
|
+
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
21
|
+
email VARCHAR(255) NOT NULL,
|
|
22
|
+
token VARCHAR(255) NOT NULL UNIQUE,
|
|
23
|
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
24
|
+
)
|
|
25
|
+
`;
|
|
26
|
+
}
|
|
27
|
+
if (connectorName === "postgresql") {
|
|
28
|
+
await db.sql`
|
|
29
|
+
CREATE TABLE IF NOT EXISTS {${tableName}} (
|
|
30
|
+
id SERIAL PRIMARY KEY,
|
|
31
|
+
email VARCHAR(255) NOT NULL,
|
|
32
|
+
token VARCHAR(255) NOT NULL UNIQUE,
|
|
33
|
+
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
|
|
34
|
+
)
|
|
35
|
+
`;
|
|
36
|
+
}
|
|
37
|
+
await db.sql`CREATE INDEX IF NOT EXISTS idx_password_reset_tokens_email ON {${tableName}} (email)`;
|
|
38
|
+
await db.sql`CREATE INDEX IF NOT EXISTS idx_password_reset_tokens_token ON {${tableName}} (token)`;
|
|
39
|
+
console.log(`[Nuxt Users] DB:Create Password Reset Tokens ${connectorName} Table Fields: id, email, token, created_at \u2705`);
|
|
40
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { useDb } from "./db.js";
|
|
2
|
+
export const createPersonalAccessTokensTable = async (options) => {
|
|
3
|
+
const connectorName = options.connector.name;
|
|
4
|
+
const db = await useDb(options);
|
|
5
|
+
const tableName = options.tables.personalAccessTokens;
|
|
6
|
+
console.log(`[Nuxt Users] DB:Create Personal Access Tokens ${connectorName} Table Creating ${tableName} table with ${connectorName} connector...`);
|
|
7
|
+
if (connectorName === "sqlite") {
|
|
8
|
+
await db.sql`
|
|
9
|
+
CREATE TABLE IF NOT EXISTS {${tableName}} (
|
|
10
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
11
|
+
tokenable_type TEXT NOT NULL,
|
|
12
|
+
tokenable_id INTEGER NOT NULL,
|
|
13
|
+
name TEXT NOT NULL,
|
|
14
|
+
token TEXT NOT NULL UNIQUE,
|
|
15
|
+
abilities TEXT,
|
|
16
|
+
last_used_at DATETIME,
|
|
17
|
+
expires_at DATETIME,
|
|
18
|
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
19
|
+
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
20
|
+
)
|
|
21
|
+
`;
|
|
22
|
+
}
|
|
23
|
+
if (connectorName === "mysql") {
|
|
24
|
+
await db.sql`
|
|
25
|
+
CREATE TABLE IF NOT EXISTS {${tableName}} (
|
|
26
|
+
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
27
|
+
tokenable_type VARCHAR(255) NOT NULL,
|
|
28
|
+
tokenable_id INT NOT NULL,
|
|
29
|
+
name VARCHAR(255) NOT NULL,
|
|
30
|
+
token VARCHAR(255) NOT NULL UNIQUE,
|
|
31
|
+
abilities TEXT,
|
|
32
|
+
last_used_at DATETIME,
|
|
33
|
+
expires_at DATETIME,
|
|
34
|
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
35
|
+
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
|
36
|
+
)
|
|
37
|
+
`;
|
|
38
|
+
}
|
|
39
|
+
if (connectorName === "postgresql") {
|
|
40
|
+
await db.sql`
|
|
41
|
+
CREATE TABLE IF NOT EXISTS {${tableName}} (
|
|
42
|
+
id SERIAL PRIMARY KEY,
|
|
43
|
+
tokenable_type VARCHAR(255) NOT NULL,
|
|
44
|
+
tokenable_id INT NOT NULL,
|
|
45
|
+
name VARCHAR(255) NOT NULL,
|
|
46
|
+
token VARCHAR(255) NOT NULL UNIQUE,
|
|
47
|
+
abilities TEXT,
|
|
48
|
+
last_used_at TIMESTAMPTZ,
|
|
49
|
+
expires_at TIMESTAMPTZ,
|
|
50
|
+
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
|
|
51
|
+
updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
|
|
52
|
+
)
|
|
53
|
+
`;
|
|
54
|
+
}
|
|
55
|
+
console.log(`[Nuxt Users] DB:Create Personal Access Tokens ${connectorName} Table Fields: id, tokenable_type, tokenable_id, name, token, abilities, last_used_at, expires_at, created_at, updated_at \u2705`);
|
|
56
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createUser } from './user.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createUser } from "./user.js";
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { useDb } from "./db.js";
|
|
2
|
+
export const createUsersTable = async (options) => {
|
|
3
|
+
const connectorName = options.connector.name;
|
|
4
|
+
const db = await useDb(options);
|
|
5
|
+
const tableName = options.tables.users;
|
|
6
|
+
console.log(`[Nuxt Users] DB:Create ${connectorName} Users Table Creating ${tableName}...`);
|
|
7
|
+
if (connectorName === "sqlite") {
|
|
8
|
+
await db.sql`
|
|
9
|
+
CREATE TABLE IF NOT EXISTS {${tableName}} (
|
|
10
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
11
|
+
email TEXT NOT NULL UNIQUE,
|
|
12
|
+
name TEXT NOT NULL,
|
|
13
|
+
password TEXT NOT NULL,
|
|
14
|
+
role TEXT NOT NULL DEFAULT 'user',
|
|
15
|
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
16
|
+
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
17
|
+
)
|
|
18
|
+
`;
|
|
19
|
+
}
|
|
20
|
+
if (connectorName === "mysql") {
|
|
21
|
+
await db.sql`
|
|
22
|
+
CREATE TABLE IF NOT EXISTS {${tableName}} (
|
|
23
|
+
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
24
|
+
email VARCHAR(255) NOT NULL UNIQUE,
|
|
25
|
+
name VARCHAR(255) NOT NULL,
|
|
26
|
+
password VARCHAR(255) NOT NULL,
|
|
27
|
+
role VARCHAR(32) NOT NULL DEFAULT 'user',
|
|
28
|
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
29
|
+
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
30
|
+
)
|
|
31
|
+
`;
|
|
32
|
+
}
|
|
33
|
+
if (connectorName === "postgresql") {
|
|
34
|
+
await db.sql`
|
|
35
|
+
CREATE TABLE IF NOT EXISTS {${tableName}} (
|
|
36
|
+
id SERIAL PRIMARY KEY,
|
|
37
|
+
email VARCHAR(255) NOT NULL UNIQUE,
|
|
38
|
+
name VARCHAR(255) NOT NULL,
|
|
39
|
+
password VARCHAR(255) NOT NULL,
|
|
40
|
+
role VARCHAR(32) NOT NULL DEFAULT 'user',
|
|
41
|
+
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
|
|
42
|
+
updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
|
|
43
|
+
)
|
|
44
|
+
`;
|
|
45
|
+
}
|
|
46
|
+
console.log(`[Nuxt Users] DB:Create ${connectorName} Users Table Fields: id, email, name, password, role, created_at, updated_at \u2705`);
|
|
47
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ModuleOptions } from '../../../types.js';
|
|
2
|
+
export declare const getConnector: (name: string) => Promise<typeof import("db0/connectors/mysql2").default | typeof import("db0/connectors/postgresql").default | typeof import("db0/connectors/better-sqlite3").default>;
|
|
3
|
+
export declare const useDb: (options: ModuleOptions) => Promise<import("db0").Database<import("db0").Connector<Database.Database>>>;
|
|
4
|
+
export declare const checkTableExists: (options: ModuleOptions, tableName: string) => Promise<boolean>;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { createDatabase } from "db0";
|
|
2
|
+
export const getConnector = async (name) => {
|
|
3
|
+
try {
|
|
4
|
+
switch (name) {
|
|
5
|
+
case "mysql":
|
|
6
|
+
return (await import("db0/connectors/mysql2")).default;
|
|
7
|
+
case "postgresql":
|
|
8
|
+
return (await import("db0/connectors/postgresql")).default;
|
|
9
|
+
case "sqlite":
|
|
10
|
+
return (await import("db0/connectors/better-sqlite3")).default;
|
|
11
|
+
default:
|
|
12
|
+
throw new Error(`Unsupported database connector: ${name}`);
|
|
13
|
+
}
|
|
14
|
+
} catch (error) {
|
|
15
|
+
if (error instanceof Error && error.message.includes("Cannot resolve")) {
|
|
16
|
+
throw new Error(`Database connector "${name}" not found. Please install the required peer dependency:
|
|
17
|
+
- For sqlite: yarn add better-sqlite3
|
|
18
|
+
- For mysql: yarn add mysql2
|
|
19
|
+
- For postgresql: yarn add pg`);
|
|
20
|
+
}
|
|
21
|
+
throw error;
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
export const useDb = async (options) => {
|
|
25
|
+
const connectorName = options.connector.name;
|
|
26
|
+
const connector = await getConnector(connectorName);
|
|
27
|
+
const connectorOptions = { ...options.connector.options };
|
|
28
|
+
if (connectorName !== "sqlite") {
|
|
29
|
+
delete connectorOptions.path;
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
return createDatabase(connector(connectorOptions));
|
|
33
|
+
} catch (error) {
|
|
34
|
+
console.warn(`[Nuxt Users] \u26A0\uFE0F Failed to connect to ${connectorName} database:`, error instanceof Error ? error.message : "Unknown error");
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
export const checkTableExists = async (options, tableName) => {
|
|
39
|
+
try {
|
|
40
|
+
const db = await useDb(options);
|
|
41
|
+
await db.sql`SELECT 1 FROM {${tableName}} LIMIT 1`;
|
|
42
|
+
return true;
|
|
43
|
+
} catch {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { useDb, checkTableExists } from './db.js';
|
|
2
|
+
export { createUser, findUserByEmail, updateUserPassword, getCurrentUserFromToken, hasAnyUsers } from './user.js';
|
|
3
|
+
export { createUsersTable } from './create-users-table.js';
|
|
4
|
+
export { createPersonalAccessTokensTable } from './create-personal-access-tokens-table.js';
|
|
5
|
+
export { createPasswordResetTokensTable } from './create-password-reset-tokens-table.js';
|
|
6
|
+
export { createMigrationsTable } from './create-migrations-table.js';
|
|
7
|
+
export { runMigrations } from './migrate.js';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { useDb, checkTableExists } from "./db.js";
|
|
2
|
+
export { createUser, findUserByEmail, updateUserPassword, getCurrentUserFromToken, hasAnyUsers } from "./user.js";
|
|
3
|
+
export { createUsersTable } from "./create-users-table.js";
|
|
4
|
+
export { createPersonalAccessTokensTable } from "./create-personal-access-tokens-table.js";
|
|
5
|
+
export { createPasswordResetTokensTable } from "./create-password-reset-tokens-table.js";
|
|
6
|
+
export { createMigrationsTable } from "./create-migrations-table.js";
|
|
7
|
+
export { runMigrations } from "./migrate.js";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ModuleOptions } from '../../../types.js';
|
|
2
|
+
export declare const getAppliedMigrations: (options: ModuleOptions) => Promise<string[]>;
|
|
3
|
+
export declare const markMigrationAsApplied: (options: ModuleOptions, migrationName: string) => Promise<void>;
|
|
4
|
+
export declare const runMigrations: (options: ModuleOptions) => Promise<void>;
|