feat: Add successRedirectUrl to auth options (#6792)
This commit is contained in:
+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: [],
|
||||
},
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user