@wrcb/cb-common 1.0.682 → 1.0.684
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.
|
@@ -7,31 +7,74 @@ exports.currentUser = void 0;
|
|
|
7
7
|
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
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);
|
|
10
14
|
let token;
|
|
11
|
-
//
|
|
15
|
+
// ═══════════════════════════════════════════════════
|
|
16
|
+
// STEP 1: Tentar pegar do header Authorization (mobile)
|
|
17
|
+
// ═══════════════════════════════════════════════════
|
|
12
18
|
const authHeader = req.headers.authorization;
|
|
13
|
-
|
|
14
|
-
|
|
19
|
+
console.log('→ Authorization header:', authHeader ? `EXISTS (${authHeader.length} chars)` : 'NONE');
|
|
20
|
+
if (authHeader) {
|
|
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
|
+
}
|
|
15
31
|
}
|
|
16
|
-
//
|
|
17
|
-
|
|
18
|
-
|
|
32
|
+
// ═══════════════════════════════════════════════════
|
|
33
|
+
// STEP 2: Se não achou no header, tentar pegar do cookie (web)
|
|
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
|
+
}
|
|
19
46
|
}
|
|
20
|
-
//
|
|
47
|
+
// ═══════════════════════════════════════════════════
|
|
48
|
+
// STEP 3: Se não tem token em nenhum lugar
|
|
49
|
+
// ═══════════════════════════════════════════════════
|
|
21
50
|
if (!token) {
|
|
51
|
+
console.log('❌ NO TOKEN FOUND - continuing without authentication');
|
|
52
|
+
console.log('🔐 [currentUser MIDDLEWARE] END (unauthenticated)');
|
|
53
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
22
54
|
return next();
|
|
23
55
|
}
|
|
24
|
-
//
|
|
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);
|
|
25
61
|
try {
|
|
26
|
-
// vamos apenas descobrir se o usuario esta logado. se estiver, vamos extrair o payload
|
|
27
|
-
// e setar no currentUser property
|
|
28
|
-
// se não tiver logado, outro middleware irá retornar um erro.
|
|
29
62
|
const payload = jsonwebtoken_1.default.verify(token, process.env.JWT_KEY);
|
|
30
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)');
|
|
31
70
|
}
|
|
32
71
|
catch (error) {
|
|
33
|
-
|
|
72
|
+
console.log('❌ TOKEN INVALID');
|
|
73
|
+
console.log('→ Error name:', error.name);
|
|
74
|
+
console.log('→ Error message:', error.message);
|
|
75
|
+
console.log('🔐 [currentUser MIDDLEWARE] END (invalid token)');
|
|
34
76
|
}
|
|
77
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
35
78
|
next();
|
|
36
79
|
};
|
|
37
80
|
exports.currentUser = currentUser;
|