create-nara 1.0.33 → 1.0.34
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.
Potentially problematic release.
This version of create-nara might be problematic. Click here for more details.
package/package.json
CHANGED
|
@@ -3,6 +3,7 @@ import type { NaraRequest, NaraResponse } from '@nara-web/core';
|
|
|
3
3
|
import { UserModel } from '../models/User.js';
|
|
4
4
|
import { db } from '../config/database.js';
|
|
5
5
|
import bcrypt from 'bcrypt';
|
|
6
|
+
import { randomUUID } from 'crypto';
|
|
6
7
|
|
|
7
8
|
export class UserController extends BaseController {
|
|
8
9
|
async index(req: NaraRequest, res: NaraResponse) {
|
|
@@ -86,13 +87,17 @@ export class UserController extends BaseController {
|
|
|
86
87
|
}
|
|
87
88
|
|
|
88
89
|
try {
|
|
90
|
+
// Generate UUID for user ID
|
|
91
|
+
const userId = randomUUID();
|
|
92
|
+
|
|
89
93
|
// Hash password if provided, otherwise generate random
|
|
90
94
|
const hashedPassword = password
|
|
91
95
|
? await bcrypt.hash(password, 10)
|
|
92
96
|
: await bcrypt.hash(Math.random().toString(36).slice(-8), 10);
|
|
93
97
|
|
|
94
98
|
// Create user in database
|
|
95
|
-
|
|
99
|
+
await UserModel.create({
|
|
100
|
+
id: userId,
|
|
96
101
|
name,
|
|
97
102
|
email,
|
|
98
103
|
password: hashedPassword,
|
|
@@ -137,14 +142,14 @@ export class UserController extends BaseController {
|
|
|
137
142
|
}
|
|
138
143
|
|
|
139
144
|
// Check if user exists
|
|
140
|
-
const existingUser = await UserModel.findById(
|
|
145
|
+
const existingUser = await UserModel.findById(id);
|
|
141
146
|
if (!existingUser) {
|
|
142
147
|
return jsonError(res, 'User not found', 404);
|
|
143
148
|
}
|
|
144
149
|
|
|
145
150
|
// Check if email is taken by another user
|
|
146
151
|
const emailUser = await UserModel.findByEmail(email);
|
|
147
|
-
if (emailUser && emailUser.id !==
|
|
152
|
+
if (emailUser && emailUser.id !== id) {
|
|
148
153
|
throw new ValidationError({ email: ['Email already registered'] });
|
|
149
154
|
}
|
|
150
155
|
|
|
@@ -164,10 +169,10 @@ export class UserController extends BaseController {
|
|
|
164
169
|
}
|
|
165
170
|
|
|
166
171
|
// Update user in database
|
|
167
|
-
await UserModel.update(
|
|
172
|
+
await UserModel.update(id, updateData);
|
|
168
173
|
|
|
169
174
|
// Fetch updated user
|
|
170
|
-
const user = await UserModel.findById(
|
|
175
|
+
const user = await UserModel.findById(id);
|
|
171
176
|
|
|
172
177
|
return jsonSuccess(res, {
|
|
173
178
|
user: {
|
|
@@ -18,7 +18,7 @@ export function authMiddleware(req: NaraRequest, res: NaraResponse, next: () =>
|
|
|
18
18
|
const token = authHeader.substring(7);
|
|
19
19
|
|
|
20
20
|
try {
|
|
21
|
-
const decoded = jwt.verify(token, JWT_SECRET) as { userId:
|
|
21
|
+
const decoded = jwt.verify(token, JWT_SECRET) as { userId: string; email: string };
|
|
22
22
|
req.user = { id: decoded.userId, email: decoded.email, name: '' };
|
|
23
23
|
next();
|
|
24
24
|
} catch (error) {
|
|
@@ -50,7 +50,7 @@ export async function webAuthMiddleware(req: NaraRequest, res: NaraResponse, nex
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
try {
|
|
53
|
-
const decoded = jwt.verify(token, JWT_SECRET) as { userId:
|
|
53
|
+
const decoded = jwt.verify(token, JWT_SECRET) as { userId: string; email: string; name: string };
|
|
54
54
|
|
|
55
55
|
// Fetch fresh user data from database to include avatar and other fields
|
|
56
56
|
const dbUser = await UserModel.findById(decoded.userId);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { db } from '../config/database.js';
|
|
2
2
|
|
|
3
3
|
export interface User {
|
|
4
|
-
id:
|
|
4
|
+
id: string;
|
|
5
5
|
name: string;
|
|
6
6
|
email: string;
|
|
7
7
|
password: string;
|
|
@@ -16,7 +16,7 @@ export interface User {
|
|
|
16
16
|
export class UserModel {
|
|
17
17
|
static tableName = 'users';
|
|
18
18
|
|
|
19
|
-
static async findById(id:
|
|
19
|
+
static async findById(id: string): Promise<User | undefined> {
|
|
20
20
|
return db(this.tableName).where({ id }).first();
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -28,11 +28,11 @@ export class UserModel {
|
|
|
28
28
|
return db(this.tableName).insert(data);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
static async update(id:
|
|
31
|
+
static async update(id: string, data: Partial<User>): Promise<number> {
|
|
32
32
|
return db(this.tableName).where({ id }).update(data);
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
static async delete(id:
|
|
35
|
+
static async delete(id: string): Promise<number> {
|
|
36
36
|
return db(this.tableName).where({ id }).delete();
|
|
37
37
|
}
|
|
38
38
|
|