@@ -2,7 +2,7 @@ const jwt = require('jsonwebtoken');
22
33const JWT_PRIVATE_KEY = `-----BEGIN RSA PRIVATE KEY-----\n${ process . env . JWT_PRIVATE_KEY } \n-----END RSA PRIVATE KEY-----` ;
44
5- const HOST = '' ;
5+ const BACKEND_URL = process . env . BACKEND_URL ; // Internal ALB for ECS
66
77/**
88 *
@@ -26,15 +26,12 @@ const getNationalIdFromEvent = (event) => {
2626} ;
2727
2828const getClientIdByNationalId = async ( nationalId ) => {
29- return 123 ;
30- const response = await fetch (
31- `${ HOST } /clients/identification?nationalId=${ nationalId } ` ,
29+ const { id } = await fetch (
30+ `${ BACKEND_URL } /clients/identification?nationalId=${ nationalId } ` ,
3231 { method : 'POST' }
33- ) . catch ( ( err ) => {
34- console . error ( 'Error during identification request' , err ) ;
35- } ) ;
32+ ) . then ( ( res ) => res . json ( ) ) ;
3633
37- return response . data . id ;
34+ return String ( id ) ;
3835} ;
3936
4037/**
@@ -52,17 +49,24 @@ exports.handler = async (event) => {
5249 const clientId = await getClientIdByNationalId ( nationalId ) ;
5350
5451 try {
55- const token = jwt . sign ( { sub : clientId } , JWT_PRIVATE_KEY , {
52+ const token = jwt . sign ( { sub : clientId . toString ( ) } , JWT_PRIVATE_KEY , {
5653 expiresIn : '1h' ,
5754 algorithm : 'RS256' ,
5855 } ) ;
5956
6057 return result ( 200 , { token } ) ;
6158 } catch ( error ) {
6259 console . error ( 'Error while generating token' , error ) ;
63- return result ( 500 , { message : 'Error while generating token' } ) ;
60+ return result ( 500 , {
61+ message : 'Error while generating token' ,
62+ error : error ?. message ,
63+ } ) ;
6464 }
6565 } catch ( error ) {
66- return result ( 500 , { message : 'Error while verifying national ID' } ) ;
66+ console . error ( 'Error while verifying national ID' , error ) ;
67+ return result ( 500 , {
68+ message : 'Error while verifying national ID' ,
69+ error : error ?. message ,
70+ } ) ;
6771 }
6872} ;
0 commit comments