📝 creating inline documentation

This commit is contained in:
Rafi Arrafif
2025-08-11 00:15:16 +07:00
parent 2ccb95e50f
commit 7ce0e44389

View File

@ -8,18 +8,24 @@ export const googleCallbackService = async (query: {
code: string; code: string;
}) => { }) => {
try { try {
// get code and state for validation from params and search for state in redis cache
const state = query.state; const state = query.state;
const codeVerifier = await redis.get( const codeVerifier = await redis.get(
`${process.env.APP_NAME}:pkce:${state}` `${process.env.APP_NAME}:pkce:${state}`
); );
// return error if the state for validation is not found in redis, and delete if found
if (!codeVerifier) throw new AppError(408, "Request timeout"); if (!codeVerifier) throw new AppError(408, "Request timeout");
await redis.del(`${process.env.APP_NAME}:pkce:${state}`); await redis.del(`${process.env.APP_NAME}:pkce:${state}`);
// create access token with the result of validating the authorization code that compares access code with validator state
const google = googleProvider(); const google = googleProvider();
const tokens = await google.validateAuthorizationCode( const tokens = await google.validateAuthorizationCode(
query.code, query.code,
codeVerifier codeVerifier
); );
// get user data from Google using the access token that has been created.
const accessToken = tokens.accessToken(); const accessToken = tokens.accessToken();
const response = await fetch( const response = await fetch(
"https://openidconnect.googleapis.com/v1/userinfo", "https://openidconnect.googleapis.com/v1/userinfo",
@ -29,6 +35,7 @@ export const googleCallbackService = async (query: {
}, },
} }
); );
return await response.json(); return await response.json();
} catch (error) { } catch (error) {
ErrorForwarder(error, 500, "Authentication service error"); ErrorForwarder(error, 500, "Authentication service error");