feat: Revert to using app_metadata for authentication (#7433)
This commit is contained in:
1 parent
6ec6e2c7b6
commit
135772b27b
24 files changed
+149
-265
No files matched your search
@@ -73,6 +73,9 @@ export const POST = async (
|
||||
actor_id: result.id,
|
||||
actor_type: "user",
|
||||
auth_identity_id: req.auth_context.auth_identity_id,
|
||||
app_metadata: {
|
||||
user_id: result.id,
|
||||
},
|
||||
},
|
||||
{
|
||||
secret: jwtSecret,
|
||||
|
||||
@@ -4,17 +4,12 @@ import {
|
||||
IAuthModuleService,
|
||||
ConfigModule,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import { ContainerRegistrationKeys, MedusaError } from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../../types/routing"
|
||||
import { generateJwtToken } from "../../../../utils/auth/token"
|
||||
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const { actor_type, auth_provider } = req.params
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
const config: ConfigModule = req.scope.resolve(
|
||||
ContainerRegistrationKeys.CONFIG_MODULE
|
||||
)
|
||||
@@ -45,14 +40,8 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const { success, error, authIdentity, successRedirectUrl } =
|
||||
await service.validateCallback(auth_provider, authData)
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "auth_identity",
|
||||
fields: [`${actor_type}.id`],
|
||||
variables: { id: authIdentity.id },
|
||||
})
|
||||
const [actorData] = await remoteQuery(queryObject)
|
||||
const entityId = actorData?.[actor_type]?.id
|
||||
|
||||
const entityIdKey = `${actor_type}_id`
|
||||
const entityId = authIdentity.app_metadata?.[entityIdKey]
|
||||
if (success) {
|
||||
const { http } = req.scope.resolve(
|
||||
ContainerRegistrationKeys.CONFIG_MODULE
|
||||
@@ -64,6 +53,9 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
actor_id: entityId,
|
||||
actor_type,
|
||||
auth_identity_id: authIdentity.id,
|
||||
app_metadata: {
|
||||
[entityIdKey]: entityId,
|
||||
},
|
||||
},
|
||||
{
|
||||
secret: jwtSecret,
|
||||
|
||||
@@ -4,17 +4,12 @@ import {
|
||||
IAuthModuleService,
|
||||
ConfigModule,
|
||||
} from "@medusajs/types"
|
||||
import {
|
||||
ContainerRegistrationKeys,
|
||||
MedusaError,
|
||||
remoteQueryObjectFromString,
|
||||
} from "@medusajs/utils"
|
||||
import { ContainerRegistrationKeys, MedusaError } from "@medusajs/utils"
|
||||
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
|
||||
import { generateJwtToken } from "../../../utils/auth/token"
|
||||
|
||||
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
const { actor_type, auth_provider } = req.params
|
||||
const remoteQuery = req.scope.resolve(ContainerRegistrationKeys.REMOTE_QUERY)
|
||||
const config: ConfigModule = req.scope.resolve(
|
||||
ContainerRegistrationKeys.CONFIG_MODULE
|
||||
)
|
||||
@@ -57,20 +52,17 @@ export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
|
||||
ContainerRegistrationKeys.CONFIG_MODULE
|
||||
).projectConfig
|
||||
|
||||
const queryObject = remoteQueryObjectFromString({
|
||||
entryPoint: "auth_identity",
|
||||
fields: [`${actor_type}.id`],
|
||||
variables: { id: authIdentity.id },
|
||||
})
|
||||
const [actorData] = await remoteQuery(queryObject)
|
||||
const entityId = actorData?.[actor_type]?.id
|
||||
|
||||
const entityIdKey = `${actor_type}_id`
|
||||
const entityId = authIdentity.app_metadata?.[entityIdKey]
|
||||
const { jwtSecret, jwtExpiresIn } = http
|
||||
const token = generateJwtToken(
|
||||
{
|
||||
actor_id: entityId,
|
||||
actor_type,
|
||||
auth_identity_id: authIdentity.id,
|
||||
app_metadata: {
|
||||
entityIdKey: entityId,
|
||||
},
|
||||
},
|
||||
{
|
||||
secret: jwtSecret,
|
||||
|
||||
@@ -41,6 +41,9 @@ export const POST = async (
|
||||
actor_id: result.id,
|
||||
actor_type: "customer",
|
||||
auth_identity_id: req.auth_context.auth_identity_id,
|
||||
app_metadata: {
|
||||
customer_id: result.id,
|
||||
},
|
||||
},
|
||||
{
|
||||
secret: jwtSecret,
|
||||
|
||||
@@ -7,7 +7,6 @@ import { track } from "medusa-telemetry"
|
||||
import loaders from "../loaders"
|
||||
import Logger from "../loaders/logger"
|
||||
import { ModuleRegistrationName, Modules } from "@medusajs/modules-sdk"
|
||||
import { ContainerRegistrationKeys } from "@medusajs/utils"
|
||||
|
||||
export default async function ({
|
||||
directory,
|
||||
@@ -33,7 +32,6 @@ export default async function ({
|
||||
|
||||
const userService = container.resolve(ModuleRegistrationName.USER)
|
||||
const authService = container.resolve(ModuleRegistrationName.AUTH)
|
||||
const remoteLink = container.resolve(ContainerRegistrationKeys.REMOTE_LINK)
|
||||
|
||||
const provider = "emailpass"
|
||||
|
||||
@@ -58,16 +56,12 @@ export default async function ({
|
||||
throw new Error(error)
|
||||
}
|
||||
|
||||
await remoteLink.create([
|
||||
{
|
||||
[Modules.USER]: {
|
||||
user_id: user.id,
|
||||
},
|
||||
[Modules.AUTH]: {
|
||||
auth_identity_id: authIdentity.id,
|
||||
},
|
||||
await authService.update({
|
||||
id: authIdentity.id,
|
||||
app_metadata: {
|
||||
user_id: user.id,
|
||||
},
|
||||
])
|
||||
})
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
|
||||
@@ -63,6 +63,7 @@ export interface AuthContext {
|
||||
actor_id: string
|
||||
actor_type: string
|
||||
auth_identity_id: string
|
||||
app_metadata: Record<string, unknown>
|
||||
}
|
||||
|
||||
export interface AuthenticatedMedusaRequest<Body = never>
|
||||
|
||||
@@ -48,6 +48,7 @@ export const authenticate = (
|
||||
actor_id: apiKey.id,
|
||||
actor_type: "api-key",
|
||||
auth_identity_id: "",
|
||||
app_metadata: {},
|
||||
}
|
||||
|
||||
return next()
|
||||
|
||||
Reference in new issue
Block a user