Skip to content

Commit

Permalink
create oauth exports, simplify naming
Browse files Browse the repository at this point in the history
  • Loading branch information
brandon-schabel committed Nov 18, 2023
1 parent 16ff81f commit cae6fd1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
4 changes: 2 additions & 2 deletions auth/example/google-oauth-server-example.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -12,7 +12,7 @@ const googleOAuthConfig = initGoogleOAuth({
clientSecret: googleClientSecret,
});

const googleOAuth = createOAuthFactory(googleOAuthConfig);
const googleOAuth = oAuthFactory(googleOAuthConfig);

const routes = {
"/login": {
Expand Down
6 changes: 5 additions & 1 deletion auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@ export {
createSecurityToken,
createToken,
getTokenExpireEpoch,
verifyToken,
verifyToken
} from "./security-token";

export { oAuthFactory } from "./oauth";

export { initGoogleOAuth, oAuthProviders } from "./oauth-providers";
10 changes: 5 additions & 5 deletions auth/oauth-providers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { OAuthConfig, OAuthProviderFn } from "./oauth-types";

export type ProvidersConfig = Record<
export type ProvidersConfigRecord = Record<
string,
Omit<OAuthConfig, "clientId" | "clientSecret">
>;

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",
Expand All @@ -17,16 +17,16 @@ 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 },
options
) => {
const redirectUrl = options?.redirectUrl;
return {
...bnkProviders.google,
redirectUri: redirectUrl ? redirectUrl : bnkProviders.google.redirectUri,
...oAuthProviders.google,
redirectUri: redirectUrl ? redirectUrl : oAuthProviders.google.redirectUri,
clientId,
clientSecret,
};
Expand Down
26 changes: 13 additions & 13 deletions auth/oauth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,6 @@ export async function getOAuthToken<T extends OAuthToken>({
return oAuthFetcher<T>(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
Expand Down Expand Up @@ -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);
},
};
};

0 comments on commit cae6fd1

Please sign in to comment.