-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
924a071
commit 5977d22
Showing
9 changed files
with
141 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 0 additions & 28 deletions
28
packages/medusa/src/api/auth/[actor_type]/[auth_provider]/update/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { | ||
AuthenticatedMedusaRequest, | ||
MedusaNextFunction, | ||
MedusaRequest, | ||
MedusaResponse, | ||
} from "@medusajs/framework" | ||
import { ConfigModule, IAuthModuleService } from "@medusajs/types" | ||
import { | ||
ContainerRegistrationKeys, | ||
ModuleRegistrationName, | ||
} from "@medusajs/utils" | ||
import { JwtPayload, verify } from "jsonwebtoken" | ||
|
||
// Middleware to validate that a token is valid | ||
export const validateToken = () => { | ||
return async ( | ||
req: MedusaRequest, | ||
res: MedusaResponse, | ||
next: MedusaNextFunction | ||
) => { | ||
const { actor_type } = req.params | ||
const { token } = req.query | ||
|
||
const req_ = req as AuthenticatedMedusaRequest | ||
|
||
if (!token) { | ||
return next() | ||
} | ||
|
||
// @ts-ignore | ||
const { http } = req_.scope.resolve<ConfigModule>( | ||
ContainerRegistrationKeys.CONFIG_MODULE | ||
).projectConfig | ||
|
||
let verified: JwtPayload | null = null | ||
|
||
try { | ||
verified = verify(token as string, http.jwtSecret!) as JwtPayload | ||
} catch (error) { | ||
return res.status(401).json({ message: "Invalid token" }) | ||
} | ||
|
||
if (!verified || !verified.provider_identity_id) { | ||
return res.status(401).json({ message: "Invalid token" }) | ||
} | ||
|
||
const authModule = req.scope.resolve<IAuthModuleService>( | ||
ModuleRegistrationName.AUTH | ||
) | ||
|
||
try { | ||
const providerIdentity = await authModule.retrieveProviderIdentity( | ||
verified.provider_identity_id, | ||
{ | ||
select: ["auth_identity_id", "entity_id"], | ||
} | ||
) | ||
|
||
req_.auth_context = { | ||
actor_type, | ||
auth_identity_id: providerIdentity.auth_identity_id!, | ||
actor_id: providerIdentity.entity_id, | ||
app_metadata: {}, | ||
} | ||
} catch (error) { | ||
return res.status(401).json({ message: "Unauthorized" }) | ||
} | ||
|
||
return next() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters