api end fixes

This commit is contained in:
2026-06-19 23:45:56 -07:00
parent 84f894c356
commit e3e520f7a9
6 changed files with 30 additions and 15 deletions
@@ -20,7 +20,7 @@ export type AuthorizationRequest = JwtRequest & {
* *
* NOTE: must be kept in sync with api/tests/support/mock-current-user.ts * NOTE: must be kept in sync with api/tests/support/mock-current-user.ts
*/ */
export async function findAndAuthorizeCurrentUserMiddleware( export async function authorizationMiddleware(
req: AuthorizationRequest, req: AuthorizationRequest,
res: Response, res: Response,
next: NextFunction next: NextFunction
@@ -38,7 +38,7 @@ export async function findAndAuthorizeCurrentUserMiddleware(
try { try {
const token = req.headers.authorization || "" const token = req.headers.authorization || ""
const user = await Users.FindFromAuth0TokenService.perform(token) const user = await Users.EnsureFromAuth0TokenService.perform(token)
req.currentUser = user req.currentUser = user
return next() return next()
} catch (error) { } catch (error) {
+1 -1
View File
@@ -1,3 +1,3 @@
export { findAndAuthorizeCurrentUserMiddleware } from "./find-and-authorize-current-user-middleware" export { authorizationMiddleware } from "./authorization-middleware"
export { jwtMiddleware } from "./jwt-middleware" export { jwtMiddleware } from "./jwt-middleware"
export { requestLoggerMiddleware } from "./request-logger-middleware" export { requestLoggerMiddleware } from "./request-logger-middleware"
+2 -2
View File
@@ -14,7 +14,7 @@ import { template } from "lodash"
import { APPLICATION_NAME, GIT_COMMIT_HASH, NODE_ENV, RELEASE_TAG } from "@/config" import { APPLICATION_NAME, GIT_COMMIT_HASH, NODE_ENV, RELEASE_TAG } from "@/config"
import { logger } from "@/utils/logger" import { logger } from "@/utils/logger"
import { jwtMiddleware, findAndAuthorizeCurrentUserMiddleware } from "@/middlewares" import { jwtMiddleware, authorizationMiddleware } from "@/middlewares"
import { CurrentUserController, UsersController } from "@/controllers" import { CurrentUserController, UsersController } from "@/controllers"
@@ -31,7 +31,7 @@ router.route("/_status").get((_req: Request, res: Response) => {
// external (public) routes - no authentication required // external (public) routes - no authentication required
// api routes // api routes
router.use("/api", jwtMiddleware, findAndAuthorizeCurrentUserMiddleware) router.use("/api", jwtMiddleware, authorizationMiddleware)
router.route("/api/current-user").get(CurrentUserController.show) router.route("/api/current-user").get(CurrentUserController.show)
@@ -2,14 +2,17 @@ import { auth0Integration } from "@/integrations"
import { User } from "@/models" import { User } from "@/models"
import { Op } from "@sequelize/core" import { Op } from "@sequelize/core"
import BaseService from "@/services/base-service" import BaseService from "@/services/base-service"
import { Users } from "@/services"
export class FindFromAuth0TokenService extends BaseService { export class EnsureFromAuth0TokenService extends BaseService {
constructor(private token: string) { constructor(private token: string) {
super() super()
} }
async perform(): Promise<User> { async perform(): Promise<User> {
const { auth0Subject, email } = await auth0Integration.getUserInfo(this.token) const { auth0Subject, email, firstName, lastName } = await auth0Integration.getUserInfo(
this.token
)
const existingUser = await User.withScope(["asCurrentUser"]).findOne({ const existingUser = await User.withScope(["asCurrentUser"]).findOne({
where: { auth0Subject }, where: { auth0Subject },
@@ -28,8 +31,20 @@ export class FindFromAuth0TokenService extends BaseService {
return firstTimeUser return firstTimeUser
} }
throw new Error("No user found for this token.") await Users.CreateService.perform({
auth0Subject,
email,
firstName,
lastName,
})
const newUser = await User.withScope(["asCurrentUser"]).findOne({
where: { auth0Subject },
rejectOnEmpty: true,
})
return newUser
} }
} }
export default FindFromAuth0TokenService export default EnsureFromAuth0TokenService
+1 -1
View File
@@ -3,4 +3,4 @@ export { UpdateService } from "./update-service"
export { DestroyService } from "./destroy-service" export { DestroyService } from "./destroy-service"
// Special Services // Special Services
export { FindFromAuth0TokenService } from "./find-from-auth0-token-service" export { EnsureFromAuth0TokenService } from "./ensure-from-auth0-token-service"
+5 -5
View File
@@ -1,9 +1,9 @@
import { Request, Response, NextFunction } from "express" import { Request, Response, NextFunction } from "express"
import { import {
findAndAuthorizeCurrentUserMiddleware, authorizationMiddleware,
type AuthorizationRequest, type AuthorizationRequest,
} from "@/middlewares/find-and-authorize-current-user-middleware" } from "@/middlewares/authorization-middleware"
import { User } from "@/models" import { User } from "@/models"
@@ -23,9 +23,9 @@ export function mockCurrentUser(newCurrentUser: User) {
jwtMiddleware: async (_req: Request, _res: Response, next: NextFunction) => next(), jwtMiddleware: async (_req: Request, _res: Response, next: NextFunction) => next(),
})) }))
vi.mock("@/middlewares/find-and-authorize-current-user-middleware") vi.mock("@/middlewares/authorization-middleware")
const findAndAuthorizeCurrentUserMiddlewareMock = vi.mocked(findAndAuthorizeCurrentUserMiddleware) const authorizationMiddlewareMock = vi.mocked(authorizationMiddleware)
findAndAuthorizeCurrentUserMiddlewareMock.mockImplementation( authorizationMiddlewareMock.mockImplementation(
async (req: AuthorizationRequest, _res: Response, next: NextFunction) => { async (req: AuthorizationRequest, _res: Response, next: NextFunction) => {
const currentUser = await User.withScope(["asCurrentUser"]).findByPk(newCurrentUser.id, { const currentUser = await User.withScope(["asCurrentUser"]).findByPk(newCurrentUser.id, {
rejectOnEmpty: true, rejectOnEmpty: true,