nuxt-users 1.6.3 → 1.6.5
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 +36 -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/module.mjs +10 -1
- package/dist/runtime/middleware/auth.client.d.ts +2 -0
- package/dist/runtime/middleware/auth.client.js +20 -0
- package/dist/runtime/plugin.js +2 -47
- package/dist/runtime/server/api/auth/reset-password.post.js +0 -3
- 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.d.ts +1 -10
- package/dist/runtime/server/api/profile.get.js +2 -3
- package/dist/runtime/server/api/update-password.post.js +3 -2
- package/dist/runtime/server/middleware/auth.server.d.ts +2 -0
- package/dist/runtime/server/middleware/auth.server.js +31 -0
- 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 +99 -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,42 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
3
|
|
|
4
|
+
## v1.6.5
|
|
5
|
+
|
|
6
|
+
[compare changes](https://github.com/rrd108/nuxt-users/compare/v1.6.4...v1.6.5)
|
|
7
|
+
|
|
8
|
+
### 🩹 Fixes
|
|
9
|
+
|
|
10
|
+
- Authentication middleware ([c7de8a4](https://github.com/rrd108/nuxt-users/commit/c7de8a4))
|
|
11
|
+
- Lint and type errors ([dd6ee40](https://github.com/rrd108/nuxt-users/commit/dd6ee40))
|
|
12
|
+
- Type errors ([65c81fb](https://github.com/rrd108/nuxt-users/commit/65c81fb))
|
|
13
|
+
|
|
14
|
+
### 🏡 Chore
|
|
15
|
+
|
|
16
|
+
- Fix formatting ([fdec442](https://github.com/rrd108/nuxt-users/commit/fdec442))
|
|
17
|
+
- **release:** V1.6.4 ([c395d20](https://github.com/rrd108/nuxt-users/commit/c395d20))
|
|
18
|
+
|
|
19
|
+
### ❤️ Contributors
|
|
20
|
+
|
|
21
|
+
- Rrd108 ([@rrd108](https://github.com/rrd108))
|
|
22
|
+
|
|
23
|
+
## v1.6.4
|
|
24
|
+
|
|
25
|
+
[compare changes](https://github.com/rrd108/nuxt-users/compare/v1.6.3...v1.6.4)
|
|
26
|
+
|
|
27
|
+
### 💅 Refactors
|
|
28
|
+
|
|
29
|
+
- Move utils to runtime/server ([78a5b50](https://github.com/rrd108/nuxt-users/commit/78a5b50))
|
|
30
|
+
|
|
31
|
+
### 🏡 Chore
|
|
32
|
+
|
|
33
|
+
- **release:** V1.6.3 ([365cfe2](https://github.com/rrd108/nuxt-users/commit/365cfe2))
|
|
34
|
+
- Fix formatting ([fdec442](https://github.com/rrd108/nuxt-users/commit/fdec442))
|
|
35
|
+
|
|
36
|
+
### ❤️ Contributors
|
|
37
|
+
|
|
38
|
+
- Rrd108 ([@rrd108](https://github.com/rrd108))
|
|
39
|
+
|
|
4
40
|
## v1.6.3
|
|
5
41
|
|
|
6
42
|
[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/module.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { defineNuxtModule, createResolver, addPlugin, addImportsDir, addServerHandler, addComponent } from '@nuxt/kit';
|
|
1
|
+
import { defineNuxtModule, createResolver, addPlugin, addImportsDir, addServerHandler, addRouteMiddleware, addComponent } from '@nuxt/kit';
|
|
2
2
|
import { defu } from 'defu';
|
|
3
3
|
|
|
4
4
|
const defaultOptions = {
|
|
@@ -83,6 +83,15 @@ const module = defineNuxtModule({
|
|
|
83
83
|
mode: "server"
|
|
84
84
|
});
|
|
85
85
|
addImportsDir(resolver.resolve("./runtime/composables"));
|
|
86
|
+
addServerHandler({
|
|
87
|
+
middleware: true,
|
|
88
|
+
handler: resolver.resolve("./runtime/server/middleware/auth.server")
|
|
89
|
+
});
|
|
90
|
+
addRouteMiddleware({
|
|
91
|
+
name: "auth.client",
|
|
92
|
+
path: resolver.resolve("./runtime/middleware/auth.client"),
|
|
93
|
+
global: true
|
|
94
|
+
});
|
|
86
95
|
addServerHandler({
|
|
87
96
|
route: "/api/login",
|
|
88
97
|
method: "post",
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { defineNuxtRouteMiddleware, navigateTo, useRuntimeConfig } from "#app";
|
|
2
|
+
import { useAuth } from "../composables/useAuth.js";
|
|
3
|
+
export default defineNuxtRouteMiddleware((to) => {
|
|
4
|
+
const { public: { nuxtUsers: publicNuxtUsers } } = useRuntimeConfig();
|
|
5
|
+
const publicOptions = publicNuxtUsers;
|
|
6
|
+
if (to.path === "/login") {
|
|
7
|
+
console.log("[Nuxt Users] client.middleware.auth.global: /login");
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
if (publicOptions.auth.whitelist.includes(to.path)) {
|
|
11
|
+
console.log(`[Nuxt Users] client.middleware.auth.global: Whitelisted: ${to.path}`);
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
const { user } = useAuth();
|
|
15
|
+
if (!user.value?.id) {
|
|
16
|
+
console.log("[Nuxt Users] client.middleware.auth.global: Unauthenticated, redirecting to /login");
|
|
17
|
+
return navigateTo("/login");
|
|
18
|
+
}
|
|
19
|
+
console.log("[Nuxt Users] client.middleware.auth.global", { user: user.value?.id, to: to.path });
|
|
20
|
+
});
|
package/dist/runtime/plugin.js
CHANGED
|
@@ -1,55 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { checkTableExists } from "
|
|
3
|
-
import { useAuth } from "./composables/useAuth.js";
|
|
1
|
+
import { defineNuxtPlugin, useRuntimeConfig } from "#app";
|
|
2
|
+
import { checkTableExists } from "./server/utils/index.js";
|
|
4
3
|
export default defineNuxtPlugin(async (_nuxtApp) => {
|
|
5
4
|
const { nuxtUsers } = useRuntimeConfig();
|
|
6
|
-
const { public: { nuxtUsers: publicNuxtUsers } } = useRuntimeConfig();
|
|
7
5
|
const options = nuxtUsers;
|
|
8
|
-
const publicOptions = publicNuxtUsers;
|
|
9
6
|
const hasMigrationsTable = await checkTableExists(options, options.tables.migrations);
|
|
10
7
|
if (!hasMigrationsTable) {
|
|
11
8
|
console.warn("[Nuxt Users] \u26A0\uFE0F Migrations table does not exist, you should run the migration script to create it by running: npx nuxt-users migrate");
|
|
12
9
|
}
|
|
13
|
-
const { initializeUser } = useAuth();
|
|
14
|
-
if (import.meta.client) {
|
|
15
|
-
await initializeUser();
|
|
16
|
-
}
|
|
17
|
-
addRouteMiddleware(
|
|
18
|
-
"auth.global",
|
|
19
|
-
(to, from) => {
|
|
20
|
-
if (to.path === "/login") {
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
if (import.meta.server) {
|
|
24
|
-
console.log("[Nuxt Users] \u{1F510} Auth middleware: SSR - allowing request to proceed");
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
const { user } = useAuth();
|
|
28
|
-
if (user.value || publicOptions.auth?.whitelist?.includes(to.path)) {
|
|
29
|
-
console.log("[Nuxt Users] \u{1F510} Auth middleware: Access allowed");
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
console.log(`[Nuxt Users] \u{1F510} Auth middleware: Redirecting from ${from.path} to login - no user and not whitelisted`);
|
|
33
|
-
return navigateTo("/login");
|
|
34
|
-
},
|
|
35
|
-
{ global: true }
|
|
36
|
-
);
|
|
37
|
-
if (import.meta.client) {
|
|
38
|
-
addRouteMiddleware(
|
|
39
|
-
"auth.client",
|
|
40
|
-
(to, from) => {
|
|
41
|
-
if (to.path === "/login") {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
const { user } = useAuth();
|
|
45
|
-
if (user.value || publicOptions.auth?.whitelist?.includes(to.path)) {
|
|
46
|
-
console.log("[Nuxt Users] \u{1F510} Client auth middleware: Access allowed");
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
console.log(`[Nuxt Users] \u{1F510} Client auth middleware: Redirecting from ${from.path} to login - no user and not whitelisted`);
|
|
50
|
-
return navigateTo("/login");
|
|
51
|
-
},
|
|
52
|
-
{ global: true }
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
10
|
});
|
|
@@ -5,9 +5,6 @@ import { validatePassword, getPasswordValidationOptions } from "../../../../util
|
|
|
5
5
|
export default defineEventHandler(async (event) => {
|
|
6
6
|
const body = await readBody(event);
|
|
7
7
|
const { token, email, password, password_confirmation } = body;
|
|
8
|
-
if (!token || typeof token !== "string") {
|
|
9
|
-
throw createError({ statusCode: 400, statusMessage: "Token is required." });
|
|
10
|
-
}
|
|
11
8
|
if (!email || typeof email !== "string") {
|
|
12
9
|
throw createError({ statusCode: 400, statusMessage: "Email is required." });
|
|
13
10
|
}
|
|
@@ -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,11 +1,2 @@
|
|
|
1
|
-
declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<
|
|
2
|
-
user: {
|
|
3
|
-
id: number;
|
|
4
|
-
email: string;
|
|
5
|
-
name: string;
|
|
6
|
-
role: string;
|
|
7
|
-
created_at: string;
|
|
8
|
-
updated_at: string;
|
|
9
|
-
};
|
|
10
|
-
}>>;
|
|
1
|
+
declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<import("../../../types.js").UserWithoutPassword>>;
|
|
11
2
|
export default _default;
|
|
@@ -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;
|
|
@@ -18,6 +18,5 @@ export default defineEventHandler(async (event) => {
|
|
|
18
18
|
statusMessage: "Unauthorized - Invalid authentication token"
|
|
19
19
|
});
|
|
20
20
|
}
|
|
21
|
-
|
|
22
|
-
return { user: userWithoutPassword };
|
|
21
|
+
return user;
|
|
23
22
|
});
|
|
@@ -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;
|
|
@@ -12,7 +13,7 @@ export default defineEventHandler(async (event) => {
|
|
|
12
13
|
statusMessage: "Unauthorized - No authentication token found"
|
|
13
14
|
});
|
|
14
15
|
}
|
|
15
|
-
const user = await getCurrentUserFromToken(token, options);
|
|
16
|
+
const user = await getCurrentUserFromToken(token, options, true);
|
|
16
17
|
if (!user) {
|
|
17
18
|
throw createError({
|
|
18
19
|
statusCode: 401,
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { defineEventHandler, getCookie, sendRedirect } from "h3";
|
|
2
|
+
import { useRuntimeConfig } from "#imports";
|
|
3
|
+
import { getCurrentUserFromToken } from "../utils/index.js";
|
|
4
|
+
export default defineEventHandler(async (event) => {
|
|
5
|
+
const { nuxtUsers } = useRuntimeConfig();
|
|
6
|
+
const options = nuxtUsers;
|
|
7
|
+
const isPageOrApiRoute = !event.path.includes(".") && (event.path === "/" || event.path.startsWith("/api/") || !event.path.startsWith("/_"));
|
|
8
|
+
if (!isPageOrApiRoute) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
if (event.path === "/login" || event.path === "/api/login") {
|
|
12
|
+
console.log("[Nuxt Users] server.middleware.auth.global: /login");
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
if (options.auth.whitelist.includes(event.path)) {
|
|
16
|
+
console.log(`[Nuxt Users] server.middleware.auth.global: whitelisted: ${event.path}`);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const token = getCookie(event, "auth_token");
|
|
20
|
+
if (!token) {
|
|
21
|
+
console.log(`[Nuxt Users] server.middleware.auth.global: ${event.path} No token found redirecting to login`);
|
|
22
|
+
return sendRedirect(event, "/login");
|
|
23
|
+
}
|
|
24
|
+
const user = await getCurrentUserFromToken(token, options);
|
|
25
|
+
if (!user) {
|
|
26
|
+
console.log(`[Nuxt Users] server.middleware.auth.global: ${event.path} No user found redirecting to login`);
|
|
27
|
+
return sendRedirect(event, "/login");
|
|
28
|
+
}
|
|
29
|
+
console.log(`[Nuxt Users] server.middleware.auth.global: Authenticated request for ${user.id}`);
|
|
30
|
+
return;
|
|
31
|
+
});
|
|
@@ -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
|
+
};
|