feat: Add successRedirectUrl to auth options (#6792)

This commit is contained in:
Oli Juhl
2024-03-25 08:55:21 +01:00
committed by GitHub
parent aa154665de
commit 0deb2776ad
11 changed files with 147 additions and 87 deletions
+18 -13
View File
@@ -1,8 +1,4 @@
import {
AuthenticationInput,
AuthenticationResponse,
ModulesSdkTypes,
} from "@medusajs/types"
import { AuthenticationInput, AuthenticationResponse } from "@medusajs/types"
import { AbstractAuthModuleProvider, MedusaError } from "@medusajs/utils"
import { AuthUserService } from "@services"
import jwt, { JwtPayload } from "jsonwebtoken"
@@ -12,13 +8,13 @@ import url from "url"
type InjectedDependencies = {
authUserService: AuthUserService
authProviderService: ModulesSdkTypes.InternalModuleService<any>
}
type ProviderConfig = {
clientID: string
clientSecret: string
callbackURL: string
successRedirectUrl?: string
}
class GoogleProvider extends AbstractAuthModuleProvider {
@@ -26,16 +22,14 @@ class GoogleProvider extends AbstractAuthModuleProvider {
public static DISPLAY_NAME = "Google Authentication"
protected readonly authUserService_: AuthUserService
protected readonly authProviderService_: ModulesSdkTypes.InternalModuleService<any>
constructor({ authUserService, authProviderService }: InjectedDependencies) {
constructor({ authUserService }: InjectedDependencies) {
super(arguments[0], {
provider: GoogleProvider.PROVIDER,
displayName: GoogleProvider.DISPLAY_NAME,
})
this.authUserService_ = authUserService
this.authProviderService_ = authProviderService
}
async authenticate(
@@ -112,7 +106,10 @@ class GoogleProvider extends AbstractAuthModuleProvider {
}
}
return { success: true, authUser }
return {
success: true,
authUser,
}
}
// abstractable
@@ -130,7 +127,17 @@ class GoogleProvider extends AbstractAuthModuleProvider {
try {
const accessToken = await client.getToken(tokenParams)
return await this.verify_(accessToken.token.id_token)
const { authUser, success } = await this.verify_(
accessToken.token.id_token
)
const { successRedirectUrl } = this.getConfigFromScope()
return {
success,
authUser,
successRedirectUrl,
}
} catch (error) {
return { success: false, error: error.message }
}
@@ -165,8 +172,6 @@ class GoogleProvider extends AbstractAuthModuleProvider {
private async getProviderConfig(
req: AuthenticationInput
): Promise<ProviderConfig> {
await this.authProviderService_.retrieve(GoogleProvider.PROVIDER)
const config = this.getConfigFromScope()
const callbackURL = config.callbackURL
+5 -2
View File
@@ -14,6 +14,7 @@ import {
import { AuthUser } from "@models"
type InjectedDependencies = {
baseRepository: DAL.RepositoryService
authUserRepository: DAL.RepositoryService
}
@@ -23,11 +24,13 @@ export default class AuthUserService<
AuthUser
)<TEntity> {
protected readonly authUserRepository_: RepositoryService<TEntity>
protected baseRepository_: DAL.RepositoryService
constructor(container: InjectedDependencies) {
// @ts-ignore
super(...arguments)
this.authUserRepository_ = container.authUserRepository
this.baseRepository_ = container.baseRepository
}
@InjectManager("authUserRepository_")
@@ -36,7 +39,7 @@ export default class AuthUserService<
provider: string,
config: FindConfig<TEntityMethod> = {},
@MedusaContext() sharedContext: Context = {}
): Promise<TEntity> {
): Promise<AuthTypes.AuthUserDTO> {
const queryConfig = ModulesSdkUtils.buildQuery<TEntity>(
{ entity_id: entityId, provider },
{ ...config, take: 1 }
@@ -53,6 +56,6 @@ export default class AuthUserService<
)
}
return result
return await this.baseRepository_.serialize<AuthTypes.AuthUserDTO>(result)
}
}