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 Korsholm
2024-02-02 10:45:32 +00:00
committed by GitHub
co-authored by Sebastian Rindom
parent 061c449179
commit 9fda6a6824
31 changed files with 302 additions and 146 deletions
@@ -1,8 +1,9 @@
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
import { MedusaRequest, MedusaResponse } from "../../../../types/routing"
import { ModuleRegistrationName } from "@medusajs/modules-sdk"
export const GET = async (req: MedusaRequest, res: MedusaResponse) => {
const id = req.auth_user!.app_metadata.customer_id
const id = req.auth_user!.app_metadata?.customer_id
const customerModule = req.scope.resolve(ModuleRegistrationName.CUSTOMER)
@@ -7,9 +7,10 @@ import {
StorePostCustomersMeAddressesAddressReq,
StoreGetCustomersMeAddressesParams,
} from "./validators"
import authenticate from "../../../utils/authenticate-middleware"
import * as QueryConfig from "./query-config"
import { authenticate } from "../../../utils/authenticate-middleware"
export const storeCustomerRoutesMiddlewares: MiddlewareRoute[] = [
{
method: "ALL",
@@ -17,7 +17,7 @@ export const defaultStoreCustomersFields: (keyof CustomerDTO)[] = [
]
export const retrieveTransformQueryConfig = {
defaultFields: defaultStoreCustomersFields,
defaultFields: defaultStoreCustomersFields as string[],
defaultRelations: defaultStoreCustomersRelations,
allowedRelations: allowedStoreCustomersRelations,
isList: false,
@@ -1,8 +1,30 @@
import { MedusaRequest, MedusaResponse } from "../../../types/routing"
import { createCustomerAccountWorkflow } from "@medusajs/core-flows"
import {
ContainerRegistrationKeys,
remoteQueryObjectFromString,
} from "@medusajs/utils"
import { CreateCustomerDTO } from "@medusajs/types"
import { createCustomerAccountWorkflow } from "@medusajs/core-flows"
export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
if (req.auth_user?.app_metadata?.customer_id) {
const remoteQuery = req.scope.resolve(
ContainerRegistrationKeys.REMOTE_QUERY
)
const query = remoteQueryObjectFromString({
entryPoint: "customer",
variables: { id: req.auth_user.app_metadata.customer_id },
fields: [],
})
const [customer] = await remoteQuery(query)
res.status(200).json({ customer })
return
}
const createCustomers = createCustomerAccountWorkflow(req.scope)
const customersData = req.validatedBody as CreateCustomerDTO
@@ -10,5 +32,9 @@ export const POST = async (req: MedusaRequest, res: MedusaResponse) => {
input: { customersData, authUserId: req.auth_user!.id },
})
// Set customer_id on session user if we are in session
if (req.session.auth_user) {
req.session.auth_user.app_metadata.customer_id = result.id
}
res.status(200).json({ customer: result })
}