Skip to content

Commit

Permalink
Tap out
Browse files Browse the repository at this point in the history
  • Loading branch information
infomiho committed Aug 12, 2024
1 parent 1a36669 commit e11cb65
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ type OnAfterLoginHookParams = {
req: ExpressRequest
} & InternalAuthHookParams

// PRIVATE API
// PRIVATE API (server)
export type OAuthParams = {
/**
* Unique request ID that was generated during the OAuth flow.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import {
type UserSignupFields,
type ProviderConfig,
} from 'wasp/auth/providers/types'
import { callbackPath } from 'wasp/server/oauth'

import {
type OAuthType,
type OAuthStateFor,
Expand All @@ -17,12 +15,14 @@ import {
} from '../oauth/state.js'
import { finishOAuthFlowAndGetRedirectUri } from '../oauth/user.js'
import {
callbackPath,
loginPath,
handleOAuthErrorAndGetRedirectUri,
} from 'wasp/server/oauth'
import { OAuthParams } from 'wasp/server/auth'
import { onBeforeOAuthRedirectHook } from '../../hooks.js'

export function createOAuthProviderRouter<OT extends OAuthType>({
export function createOAuthProviderRouter<OT extends OAuthType, Tokens extends OAuthParams['tokens'] = never>({
provider,
oAuthType,
userSignupFields,
Expand Down Expand Up @@ -51,14 +51,12 @@ export function createOAuthProviderRouter<OT extends OAuthType>({
*/
getProviderTokens: (
oAuthState: OAuthStateWithCodeFor<OT>,
) => Promise<{
accessToken: string
}>
) => Promise<Tokens>
/*
The function that returns the user's profile and ID using the access
token.
*/
getProviderInfo: ({ accessToken }: { accessToken: string }) => Promise<{
getProviderInfo: (tokens: Tokens) => Promise<{
providerUserId: string
providerProfile: unknown
}>
Expand Down Expand Up @@ -94,17 +92,25 @@ export function createOAuthProviderRouter<OT extends OAuthType>({
})
const tokens = await getProviderTokens(oAuthState)

const { providerProfile, providerUserId } = await getProviderInfo({
accessToken: tokens.accessToken,
})
const { providerProfile, providerUserId } = await getProviderInfo(tokens)
try {
const redirectUri = await finishOAuthFlowAndGetRedirectUri({
provider,
providerProfile,
providerUserId,
userSignupFields,
req,
oauth: { uniqueRequestId: oAuthState.state, tokens, provider: provider.id },
oauth: {
uniqueRequestId: oAuthState.state,
// OAuth params are built as a discriminated union
// of provider names and their respective tokens.
// We are using a generic ProviderConfig and tokens type
// is inferred from the getProviderTokens function.
// Instead of building complex TS machinery to ensure that
// the providerName and tokens match, we are using any here.
providerName: provider.id as any,
tokens,
},
})
// Redirect to the client with the one time code
return redirect(res, redirectUri.toString())
Expand Down

0 comments on commit e11cb65

Please sign in to comment.