-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_handler.js
More file actions
42 lines (37 loc) · 1.06 KB
/
Copy pathauth_handler.js
File metadata and controls
42 lines (37 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// auth_handler.js
const jwt = require('jsonwebtoken');
const AWS = require('aws-sdk');
const ddb = new AWS.DynamoDB.DocumentClient();
exports.handler = async (event) => {
const { cpf } = JSON.parse(event.body || '{}');
if (!cpf) {
return { statusCode: 400, body: JSON.stringify({ message: 'CPF is required' }) };
}
// Consulta DynamoDB pela chave 'cpf'
let customer;
try {
const result = await ddb
.get({
TableName: process.env.CUSTOMERS_TABLE,
Key: { cpf },
})
.promise();
customer = result.Item;
} catch (err) {
console.error('DynamoDB error', err);
return { statusCode: 500, body: JSON.stringify({ message: 'Internal server error' }) };
}
if (!customer) {
return { statusCode: 404, body: JSON.stringify({ message: 'Customer not found' }) };
}
// Gera JWT com sub = customer.id (ou cpf) e expira em 1h
const token = jwt.sign(
{ sub: customer.id || cpf, cpf },
process.env.JWT_SECRET,
{ expiresIn: '1h' }
);
return {
statusCode: 200,
body: JSON.stringify({ token }),
};
};