@wrcb/cb-common 1.0.685 → 1.0.686
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.
|
@@ -5,76 +5,29 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.currentUser = void 0;
|
|
7
7
|
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
|
-
const currentUser = (req,
|
|
8
|
+
const currentUser = (req, _res, next) => {
|
|
9
9
|
var _a;
|
|
10
|
-
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
11
|
-
console.log('🔐 [currentUser MIDDLEWARE] START');
|
|
12
|
-
console.log('→ Path:', req.path);
|
|
13
|
-
console.log('→ Method:', req.method);
|
|
14
10
|
let token;
|
|
15
|
-
//
|
|
16
|
-
// STEP 1: Tentar pegar do header Authorization (mobile)
|
|
17
|
-
// ═══════════════════════════════════════════════════
|
|
11
|
+
// 1️⃣ Authorization header (Bearer token)
|
|
18
12
|
const authHeader = req.headers.authorization;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
console.log('→ Authorization header preview:', authHeader.substring(0, 50) + '...');
|
|
22
|
-
if (authHeader.startsWith('Bearer ')) {
|
|
23
|
-
token = authHeader.substring(7); // remove "Bearer "
|
|
24
|
-
console.log('✅ Token extracted from Authorization header');
|
|
25
|
-
console.log('→ Token length:', token.length);
|
|
26
|
-
console.log('→ Token preview:', token.substring(0, 50) + '...');
|
|
27
|
-
}
|
|
28
|
-
else {
|
|
29
|
-
console.log('⚠️ Authorization header exists but does NOT start with "Bearer "');
|
|
30
|
-
}
|
|
13
|
+
if (authHeader === null || authHeader === void 0 ? void 0 : authHeader.startsWith('Bearer ')) {
|
|
14
|
+
token = authHeader.slice(7);
|
|
31
15
|
}
|
|
32
|
-
//
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
if (!token) {
|
|
36
|
-
console.log('→ No token from header, checking cookie...');
|
|
37
|
-
if ((_a = req.session) === null || _a === void 0 ? void 0 : _a.jwt) {
|
|
38
|
-
token = req.session.jwt;
|
|
39
|
-
console.log('✅ Token extracted from cookie session');
|
|
40
|
-
console.log('→ Token length:', token === null || token === void 0 ? void 0 : token.length);
|
|
41
|
-
console.log('→ Token preview:', (token === null || token === void 0 ? void 0 : token.substring(0, 50)) + '...');
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
console.log('→ Cookie session:', req.session ? 'EXISTS but no jwt property' : 'NONE');
|
|
45
|
-
}
|
|
16
|
+
// 2️⃣ Fallback para session (web)
|
|
17
|
+
if (!token && ((_a = req.session) === null || _a === void 0 ? void 0 : _a.jwt)) {
|
|
18
|
+
token = req.session.jwt;
|
|
46
19
|
}
|
|
47
|
-
//
|
|
48
|
-
// STEP 3: Se não tem token em nenhum lugar
|
|
49
|
-
// ═══════════════════════════════════════════════════
|
|
20
|
+
// Sem token → segue request
|
|
50
21
|
if (!token) {
|
|
51
|
-
console.log('❌ NO TOKEN FOUND - continuing without authentication');
|
|
52
|
-
console.log('🔐 [currentUser MIDDLEWARE] END (unauthenticated)');
|
|
53
|
-
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
54
22
|
return next();
|
|
55
23
|
}
|
|
56
|
-
// ═══════════════════════════════════════════════════
|
|
57
|
-
// STEP 4: Validar o token JWT
|
|
58
|
-
// ═══════════════════════════════════════════════════
|
|
59
|
-
console.log('→ Validating JWT token...');
|
|
60
|
-
console.log('→ JWT_KEY exists:', !!process.env.JWT_KEY);
|
|
61
24
|
try {
|
|
62
|
-
|
|
63
|
-
req.currentUser = payload;
|
|
64
|
-
console.log('✅ TOKEN VALID');
|
|
65
|
-
console.log('→ User ID:', payload.id);
|
|
66
|
-
console.log('→ User email:', payload.email);
|
|
67
|
-
console.log('→ User tenant:', payload.tenant);
|
|
68
|
-
console.log('→ User role:', payload.role);
|
|
69
|
-
console.log('🔐 [currentUser MIDDLEWARE] END (authenticated)');
|
|
25
|
+
req.currentUser = jsonwebtoken_1.default.verify(token, process.env.JWT_KEY);
|
|
70
26
|
}
|
|
71
|
-
catch (
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
console.log('→ Error message:', error.message);
|
|
75
|
-
console.log('🔐 [currentUser MIDDLEWARE] END (invalid token)');
|
|
27
|
+
catch (_b) {
|
|
28
|
+
// Token inválido ou expirado → ignora silenciosamente
|
|
29
|
+
req.currentUser = undefined;
|
|
76
30
|
}
|
|
77
|
-
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
78
31
|
next();
|
|
79
32
|
};
|
|
80
33
|
exports.currentUser = currentUser;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Country, Language } from './country';
|
|
2
2
|
import { Tenant } from './tenant';
|
|
3
3
|
import { UserCategory } from './userCategory';
|
|
4
|
+
import { UserRole } from './userRole';
|
|
4
5
|
import { UserTags } from './userTags';
|
|
5
6
|
export interface UserPayload {
|
|
6
7
|
id: string;
|
|
@@ -18,7 +19,7 @@ export interface UserPayload {
|
|
|
18
19
|
category: UserCategory;
|
|
19
20
|
profilePhoto: string;
|
|
20
21
|
country: Country;
|
|
21
|
-
role:
|
|
22
|
+
role: UserRole;
|
|
22
23
|
myAffiliateCode: string;
|
|
23
24
|
affiliateCode?: string;
|
|
24
25
|
affiliateId?: string;
|