diff --git a/auth/example/google-oauth-server-example.ts b/auth/example/google-oauth-server-example.ts index 1aa44f8..7b951c5 100644 --- a/auth/example/google-oauth-server-example.ts +++ b/auth/example/google-oauth-server-example.ts @@ -1,4 +1,4 @@ -import { createOAuthFactory } from "auth/oauth"; +import { oAuthFactory } from "auth/oauth"; import { initGoogleOAuth } from "auth/oauth-providers"; import type { Routes } from "server"; import { serverFactory } from "server"; @@ -12,7 +12,7 @@ const googleOAuthConfig = initGoogleOAuth({ clientSecret: googleClientSecret, }); -const googleOAuth = createOAuthFactory(googleOAuthConfig); +const googleOAuth = oAuthFactory(googleOAuthConfig); const routes = { "/login": { diff --git a/auth/index.ts b/auth/index.ts index e3edcab..626a2e0 100644 --- a/auth/index.ts +++ b/auth/index.ts @@ -2,5 +2,9 @@ export { createSecurityToken, createToken, getTokenExpireEpoch, - verifyToken, + verifyToken } from "./security-token"; + +export { oAuthFactory } from "./oauth"; + +export { initGoogleOAuth, oAuthProviders } from "./oauth-providers"; diff --git a/auth/oauth-providers.ts b/auth/oauth-providers.ts index 8e298d9..cd308b9 100644 --- a/auth/oauth-providers.ts +++ b/auth/oauth-providers.ts @@ -1,11 +1,11 @@ import { OAuthConfig, OAuthProviderFn } from "./oauth-types"; -export type ProvidersConfig = Record< +export type ProvidersConfigRecord = Record< string, Omit >; -export const bnkProviders = { +export const oAuthProviders = { google: { redirectUri: "http://localhost:3000/callback", // just a default placeholder authReqUrl: "https://accounts.google.com/o/oauth2/v2/auth", @@ -17,7 +17,7 @@ export const bnkProviders = { "https://login.microsoftonline.com/common/oauth2/v2.0/authorize", tokenUrl: "http://needtofind", }, -} satisfies ProvidersConfig; +} satisfies ProvidersConfigRecord; export const initGoogleOAuth: OAuthProviderFn = ( { clientId, clientSecret }, @@ -25,8 +25,8 @@ export const initGoogleOAuth: OAuthProviderFn = ( ) => { const redirectUrl = options?.redirectUrl; return { - ...bnkProviders.google, - redirectUri: redirectUrl ? redirectUrl : bnkProviders.google.redirectUri, + ...oAuthProviders.google, + redirectUri: redirectUrl ? redirectUrl : oAuthProviders.google.redirectUri, clientId, clientSecret, }; diff --git a/auth/oauth.ts b/auth/oauth.ts index 6a1b931..4f3968c 100644 --- a/auth/oauth.ts +++ b/auth/oauth.ts @@ -43,19 +43,6 @@ export async function getOAuthToken({ return oAuthFetcher(config.tokenUrl, params); } -export const createOAuthFactory = (config: OAuthConfig) => { - const provider = initProvider(config); - - return { - handleRedirect: async (code: string) => { - return await provider.getToken(code, config); - }, - initiateOAuthFlow: () => { - return provider.getAuthorizationUrl(config); - }, - }; -}; - export const initProvider: OAuthProviderInitializer = (config) => { return { // TODO add options to be able to change response_type/scope, etc @@ -83,3 +70,16 @@ export const initProvider: OAuthProviderInitializer = (config) => { }, }; }; + +export const oAuthFactory = (config: OAuthConfig) => { + const provider = initProvider(config); + + return { + handleRedirect: async (code: string) => { + return await provider.getToken(code, config); + }, + initiateOAuthFlow: () => { + return provider.getAuthorizationUrl(config); + }, + }; +};