feat(auth): add authentication endpoints (#6265)

**What**
- Add authentication endpoints: 
  - `/auth/[scope]/[provider]` 
  - `/auth/[scope]/[provider]/callback`
- update authenticate-middleware handler
- Add scope field to user
- Add unique constraint on scope and entity_id

note: there's still some remaining work related to jwt auth to be handled, this is mainly focussed on session auth with endpoints



Co-authored-by: Sebastian Rindom <7554214+srindom@users.noreply.github.com>
This commit is contained in:
Philip KorsholmandSebastian Rindom authored and GitHub committed 2024-02-02 10:45:32 +00:00
1 parent 061c449179
commit 9fda6a6824
31 files changed
+302 -146

No files matched your search

@@ -1,22 +1,20 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { AuthUserDTO, IAuthModuleService } from "@medusajs/types"
import { NextFunction, RequestHandler } from "express"
import { MedusaRequest, MedusaResponse } from "../types/routing"
import { NextFunction, RequestHandler } from "express"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
const SESSION_AUTH = "session"
const BEARER_AUTH = "bearer"
type MedusaSession = {
auth: {
[authScope: string]: {
user_id: string
}
}
auth_user: AuthUserDTO
scope: string
}
type AuthType = "session" | "bearer"
export default (
export const authenticate = (
authScope: string,
authType: AuthType | AuthType[],
options: { allowUnauthenticated?: boolean } = {}
@@ -36,19 +34,18 @@ export default (
let authUser: AuthUserDTO | null = null
if (authTypes.includes(SESSION_AUTH)) {
if (session.auth && session.auth[authScope]) {
authUser = await authModule
.retrieveAuthUser(session.auth[authScope].user_id)
.catch(() => null)
if (session.auth_user && session.scope === authScope) {
authUser = session.auth_user
}
}
if (authTypes.includes(BEARER_AUTH)) {
if (!authUser && authTypes.includes(BEARER_AUTH)) {
const authHeader = req.headers.authorization
if (authHeader) {
const re = /(\S+)\s+(\S+)/
const matches = authHeader.match(re)
// TODO: figure out how to obtain token (and store correct data in token)
if (matches) {
const tokenType = matches[1]
const token = matches[2]