简体   繁体   English

将 Auth0 API 保护路由与 Express 路由处理程序一起使用

[英]Use Auth0 API protected route with Express route handlers

I implemented protected API routes in Express using the Auth0 quick start .我使用Auth0 quick start在 Express 中实现了受保护的 API 路由。 Protected routes are handled with a middleware function called checkJwt , provided by Auth0 .受保护的路由由 Auth0 提供的名为checkJwt的中间件 function 处理。 It runs whenever one of the GET methods is called.每当调用 GET 方法之一时,它就会运行。 This process works well if I manage all my routes in server.js .如果我在server.js中管理所有路由,则此过程运行良好。

How can I separate out the route handling and still preserve the protected API routes?如何分离路由处理并仍然保留受保护的 API 路由?

server.js working code with protected routes .带有受保护路由的server.js工作代码。

 import colors from 'colors' import cors from 'cors' import express from 'express' import morgan from 'morgan' import dotenv from 'dotenv' import connectDB from './db.js' import checkJwt from './middleware/auth.middleware.js' import { getStudents, getStudent } from './controllers/students.controller.js' dotenv.config() connectDB() export const app = express() app.use(cors()) app.use(express.json({ limit: '50mb' })) if (process.env.NODE_ENV === 'development') { app.use(morgan('dev')) } //handle routing internally app.get('/api/students/:id', checkJwt, getStudent) app.get('/api/students', checkJwt, getStudents) const PORT = process.env.PORT || 6000 app.listen(PORT, () => console.log( `Server running in ${process.env.NODE_ENV} mode on port ${PORT}`.yellow.bold ) )

I want to divorce out the route handling as my code grows.随着代码的增长,我想脱离路由处理。 I can't figure out where to put checkJwt once I separate out the routing.一旦我分离出路由,我就无法弄清楚将checkJwt放在哪里。

server.js desired code structure is: server.js所需的代码结构是:

 import colors from 'colors' import cors from 'cors' import express from 'express' import morgan from 'morgan' import dotenv from 'dotenv' import connectDB from './db.js' import studentsRouter from './routes/students.routes.js' dotenv.config() connectDB() const app = express() app.use(cors()) app.use(express.json({ limit: '50mb' })) if (process.env.NODE_ENV === 'development') { app.use(morgan('dev')) } // handle routing externally const apiRouter = express.Router() app.use('/api', apiRouter) apiRouter.use('/students', studentsRouter) const PORT = process.env.PORT || 6000 app.listen(PORT, () => console.log( `Server running in ${process.env.NODE_ENV} mode on port ${PORT}`.yellow.bold ) )

Routes are implemented in the students.routes.js Route handler.路由在students.routes.js路由处理程序中实现。

 import express from 'express' import { getStudent, getStudents } from '../controllers/students.controller.js' const router = express.Router() // where can I implement checkJwt? router.route('/').get(getStudents) router.route('/:id').get(getStudent) export default router

Is it possible to simplify the code by moving the routing, and still protect my routes?是否可以通过移动路由来简化代码,并且仍然保护我的路由?

you can use any middleware like this in your router:您可以在路由器中使用任何这样的中间件:

const router = express.Router();

router.route('/').get([checkJwt, secondMiddleware, ...] , getStudents);

based on documentation: express-routing基于文档:快速路由

You can provide multiple callback functions that behave like middleware to handle a request.您可以提供多个回调函数,它们的行为类似于中间件来处理请求。 The only exception is that these callbacks might invoke next('route') to bypass the remaining route callbacks.唯一的例外是这些回调可能会调用 next('route') 来绕过剩余的路由回调。 You can use this mechanism to impose pre-conditions on a route, then pass control to subsequent routes if there's no reason to proceed with the current route.您可以使用此机制对路由施加先决条件,然后如果没有理由继续当前路由,则将控制权传递给后续路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM