feat: Add successRedirectUrl to auth options (#6792)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
+15
-10
@@ -1,11 +1,11 @@
|
||||
import jwt from "jsonwebtoken"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
|
||||
import { AuthenticationInput, IAuthModuleService } from "@medusajs/types"
|
||||
import { MedusaError } from "@medusajs/utils"
|
||||
import jwt from "jsonwebtoken"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
|
||||
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const { scope, authProvider } = req.params
|
||||
const { scope, auth_provider } = req.params
|
||||
|
||||
const service: IAuthModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
@@ -20,18 +20,23 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
protocol: req.protocol,
|
||||
} as AuthenticationInput
|
||||
|
||||
const authResult = await service.validateCallback(authProvider, authData)
|
||||
const authResult = await service.validateCallback(auth_provider, authData)
|
||||
|
||||
const { success, error, authUser, location } = authResult
|
||||
if (location) {
|
||||
res.redirect(location)
|
||||
return
|
||||
}
|
||||
const { success, error, authUser, successRedirectUrl } = authResult
|
||||
|
||||
if (success) {
|
||||
const { jwt_secret } = req.scope.resolve("configModule").projectConfig
|
||||
|
||||
const token = jwt.sign(authUser, jwt_secret)
|
||||
return res.status(200).json({ token })
|
||||
|
||||
if (successRedirectUrl) {
|
||||
const url = new URL(successRedirectUrl!)
|
||||
url.searchParams.append("auth_token", token)
|
||||
|
||||
return res.redirect(url.toString())
|
||||
}
|
||||
|
||||
return res.json({ token })
|
||||
}
|
||||
|
||||
throw new MedusaError(
|
||||
+2
-2
@@ -5,7 +5,7 @@ import jwt from "jsonwebtoken"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const { scope, authProvider } = req.params
|
||||
const { scope, auth_provider } = req.params
|
||||
|
||||
const service: IAuthModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
@@ -20,7 +20,7 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
protocol: req.protocol,
|
||||
} as AuthenticationInput
|
||||
|
||||
const authResult = await service.authenticate(authProvider, authData)
|
||||
const authResult = await service.authenticate(auth_provider, authData)
|
||||
|
||||
const { success, error, authUser, location } = authResult
|
||||
|
||||
@@ -7,4 +7,14 @@ export const authRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
matcher: "/auth/session",
|
||||
middlewares: [authenticate(/.*/, "bearer")],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/auth/:scope/:auth_provider/callback",
|
||||
middlewares: [],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/auth/:scope/:auth_provider",
|
||||
middlewares: [],
|
||||
},
|
||||
]
|
||||
|
||||
@@ -24,6 +24,11 @@ export type AuthenticationResponse = {
|
||||
* Redirect location. Location takes precedence over success.
|
||||
*/
|
||||
location?: string
|
||||
|
||||
/**
|
||||
* Redirect url for successful authentication.
|
||||
*/
|
||||
successRedirectUrl?: string
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user