feat: Separate registration from authentication in auth domain (#8683)
* wip * feat: Introduce register * fix: user command * fix: Invite HTTP tests * fix: Auth tests * fix: Invite modules tests
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import {
|
||||
AuthenticationInput,
|
||||
ConfigModule,
|
||||
IAuthModuleService,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
ModuleRegistrationName,
|
||||
} from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
|
||||
import { generateJwtTokenForAuthIdentity } from "../../../utils/generate-jwt-token"
|
||||
|
||||
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const { actor_type, auth_provider } = req.params
|
||||
const config: ConfigModule = req.scope.resolve(
|
||||
ContainerRegistrationKeys.CONFIG_MODULE
|
||||
)
|
||||
|
||||
const authMethodsPerActor =
|
||||
config.projectConfig?.http?.authMethodsPerActor ?? {}
|
||||
// Not having the config defined would allow for all auth providers for the particular actor.
|
||||
if (authMethodsPerActor[actor_type]) {
|
||||
if (!authMethodsPerActor[actor_type].includes(auth_provider)) {
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.NOT_ALLOWED,
|
||||
`The actor type ${actor_type} is not allowed to use the auth provider ${auth_provider}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const service: IAuthModuleService = req.scope.resolve(
|
||||
ModuleRegistrationName.AUTH
|
||||
)
|
||||
|
||||
const authData = {
|
||||
url: req.url,
|
||||
headers: req.headers,
|
||||
query: req.query,
|
||||
body: req.body,
|
||||
protocol: req.protocol,
|
||||
} as AuthenticationInput
|
||||
|
||||
const { success, error, authIdentity } = await service.register(
|
||||
auth_provider,
|
||||
authData
|
||||
)
|
||||
|
||||
if (success) {
|
||||
const { http } = config.projectConfig
|
||||
|
||||
const token = generateJwtTokenForAuthIdentity(
|
||||
{
|
||||
authIdentity,
|
||||
actorType: actor_type,
|
||||
},
|
||||
{
|
||||
secret: http.jwtSecret,
|
||||
expiresIn: http.jwtExpiresIn,
|
||||
}
|
||||
)
|
||||
|
||||
return res.status(200).json({ token })
|
||||
}
|
||||
|
||||
throw new MedusaError(
|
||||
MedusaError.Types.UNAUTHORIZED,
|
||||
error || "Authentication failed"
|
||||
)
|
||||
}
|
||||
@@ -7,9 +7,9 @@ import {
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
ModuleRegistrationName,
|
||||
generateJwtToken,
|
||||
} from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||
import { generateJwtTokenForAuthIdentity } from "../../utils/generate-jwt-token"
|
||||
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const { actor_type, auth_provider } = req.params
|
||||
@@ -52,30 +52,16 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
}
|
||||
|
||||
if (success) {
|
||||
const { http } = req.scope.resolve(
|
||||
ContainerRegistrationKeys.CONFIG_MODULE
|
||||
).projectConfig
|
||||
const { http } = config.projectConfig
|
||||
|
||||
const entityIdKey = `${actor_type}_id`
|
||||
const entityId = authIdentity?.app_metadata?.[entityIdKey] as
|
||||
| string
|
||||
| undefined
|
||||
const { jwtSecret, jwtExpiresIn } = http
|
||||
|
||||
const token = generateJwtToken(
|
||||
const token = generateJwtTokenForAuthIdentity(
|
||||
{
|
||||
actor_id: entityId ?? "",
|
||||
actor_type,
|
||||
auth_identity_id: authIdentity?.id ?? "",
|
||||
app_metadata: {
|
||||
[entityIdKey]: entityId,
|
||||
},
|
||||
authIdentity,
|
||||
actorType: actor_type,
|
||||
},
|
||||
{
|
||||
// @ts-expect-error
|
||||
secret: jwtSecret,
|
||||
// @ts-expect-error
|
||||
expiresIn: jwtExpiresIn,
|
||||
secret: http.jwtSecret,
|
||||
expiresIn: http.jwtExpiresIn,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -19,6 +19,16 @@ export const authRoutesMiddlewares: MiddlewareRoute[] = [
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/auth/:actor_type/:auth_provider/register",
|
||||
middlewares: [],
|
||||
},
|
||||
{
|
||||
method: ["POST"],
|
||||
matcher: "/auth/:actor_type/:auth_provider",
|
||||
middlewares: [],
|
||||
},
|
||||
{
|
||||
method: ["GET"],
|
||||
matcher: "/auth/:actor_type/:auth_provider",
|
||||
middlewares: [],
|
||||
},
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import { generateJwtToken } from "@medusajs/utils"
|
||||
|
||||
export function generateJwtTokenForAuthIdentity(
|
||||
{ authIdentity, actorType },
|
||||
{ secret, expiresIn }
|
||||
) {
|
||||
const entityIdKey = `${actorType}_id`
|
||||
const entityId = authIdentity?.app_metadata?.[entityIdKey] as
|
||||
| string
|
||||
| undefined
|
||||
|
||||
return generateJwtToken(
|
||||
{
|
||||
actor_id: entityId ?? "",
|
||||
actor_type: actorType,
|
||||
auth_identity_id: authIdentity?.id ?? "",
|
||||
app_metadata: {
|
||||
[entityIdKey]: entityId,
|
||||
},
|
||||
},
|
||||
{
|
||||
secret,
|
||||
expiresIn,
|
||||
}
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user