✨ add new oauth provider
add google idconnect as new auth provider
This commit is contained in:
36
src/modules/auth/services/googleCallback.service.ts
Normal file
36
src/modules/auth/services/googleCallback.service.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import { AppError } from "../../../helpers/error/instances/app";
|
||||
import { googleProvider } from "../providers/google.provider";
|
||||
import { redis } from "../../../utils/databases/redis/connection";
|
||||
import { ErrorForwarder } from "../../../helpers/error/instances/forwarder";
|
||||
|
||||
export const googleCallbackService = async (query: {
|
||||
state: string;
|
||||
code: string;
|
||||
}) => {
|
||||
try {
|
||||
const state = query.state;
|
||||
const codeVerifier = await redis.get(
|
||||
`${process.env.APP_NAME}:pkce:${state}`
|
||||
);
|
||||
if (!codeVerifier) throw new AppError(408, "Request timeout");
|
||||
await redis.del(`${process.env.APP_NAME}:pkce:${state}`);
|
||||
|
||||
const google = googleProvider();
|
||||
const tokens = await google.validateAuthorizationCode(
|
||||
query.code,
|
||||
codeVerifier
|
||||
);
|
||||
const accessToken = tokens.accessToken();
|
||||
const response = await fetch(
|
||||
"https://openidconnect.googleapis.com/v1/userinfo",
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
ErrorForwarder(error, 500, "Authentication service error");
|
||||
}
|
||||
};
|
||||
28
src/modules/auth/services/googleRequest.service.ts
Normal file
28
src/modules/auth/services/googleRequest.service.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import * as arctic from "arctic";
|
||||
import { AppError } from "../../../helpers/error/instances/app";
|
||||
import { googleProvider } from "../providers/google.provider";
|
||||
import { redis } from "../../../utils/databases/redis/connection";
|
||||
|
||||
export const googleRequestService = async () => {
|
||||
try {
|
||||
const google = googleProvider();
|
||||
const state = arctic.generateState();
|
||||
const codeVerifier = arctic.generateCodeVerifier();
|
||||
const scopes = ["openid", "profile", "email"];
|
||||
const url = google.createAuthorizationURL(state, codeVerifier, scopes);
|
||||
|
||||
await redis.setex(
|
||||
`${process.env.APP_NAME}:pkce:${state}`,
|
||||
300,
|
||||
codeVerifier
|
||||
);
|
||||
|
||||
return url;
|
||||
} catch (error) {
|
||||
throw new AppError(
|
||||
500,
|
||||
"Google Auth provider is experiencing issues.",
|
||||
error
|
||||
);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user