Passport-Local 和 Express 路由器导入问题

发布于 2025-01-14 23:13:57 字数 1930 浏览 4 评论 0原文

我有一个带有 Passport-local Auth 的 Express.js 应用程序,我想在我的应用程序中实现 Express 路由器,因为它很难调试

这是我的主文件 (app.js) 的一个示例

const express = require('express')
const app = express()
const passport = require('passport')
const testing = require('./routes/file')

app.use(passport.initialize())
app.use(passport.session())

app.use('/router', testing)

const initializePassport = require('./passport-config');
initializePassport.initialize(passport);

app.get('/', checkAuthenticated, (req, res) => {
    let user = data.key
    res.send(`Hi ${user}`)
})

app.post('/login', checkNotAuthenticated, passport.authenticate('local', {
    successRedirect: '/',
    failureRedirect: '/login',
    failureFlash: true
}))
  
function checkAuthenticated(req, res, next) {
    if (req.isAuthenticated()) {
        return next()
    }

    res.redirect('/login')
}
  
function checkNotAuthenticated(req, res, next) {
    if (req.isAuthenticated()) {
        return res.redirect('/')
    }
    next()
}

app.listen(3000)

这是我的主文件文件就像,这工作得很好 为了清楚起见,该功能是检查用户是否经过身份验证,我的护照配置也返回名为 data.key 的内容,它基本上是用户的用户名。

现在我在这里创建了一个路由器 app.use('/router',testing) ,路由为 /router 用于测试目的

现在这是我的 file.js 现在正如预期的那样

const express = require('express'), router = express.Router();
var bodyParser = require('body-parser');

// Handle POST requests
router.use(bodyParser.urlencoded({ extended: false }));
router.use(bodyParser.json());

router.get('/test', checkAuthenticated, (req, res) => {
    let user = data.key;
    res.send(`Hello ${user}, this is coming from the router`)
})

module.exports = router;

,当我在 localhost:3000/router/test 打开站点时,它会返回 data.key 的未定义错误,因为它没有导入,然后当我注释掉导致错误的行时, checkAuthenticated 不会'不工作,即使我没有登录,我可以访问该页面,现在这就是问题,我该如何解决它,我已经浏览了整个快速路由器文档,但我在那里找不到运气。

谢谢

I have an Express.js app with a Passport-local Auth, I'm tying to implement express router to my app since it's getting really hard to debug

This is an example of my main file (app.js)

const express = require('express')
const app = express()
const passport = require('passport')
const testing = require('./routes/file')

app.use(passport.initialize())
app.use(passport.session())

app.use('/router', testing)

const initializePassport = require('./passport-config');
initializePassport.initialize(passport);

app.get('/', checkAuthenticated, (req, res) => {
    let user = data.key
    res.send(`Hi ${user}`)
})

app.post('/login', checkNotAuthenticated, passport.authenticate('local', {
    successRedirect: '/',
    failureRedirect: '/login',
    failureFlash: true
}))
  
function checkAuthenticated(req, res, next) {
    if (req.isAuthenticated()) {
        return next()
    }

    res.redirect('/login')
}
  
function checkNotAuthenticated(req, res, next) {
    if (req.isAuthenticated()) {
        return res.redirect('/')
    }
    next()
}

app.listen(3000)

This is what my main file is like, this works perfectly fine
and just to make things clear, The function are to check if a user is authenticated or not, also my passport-config returns something called data.key which is basically the username of the user.

Now I've created a router here app.use('/router', testing) with the route as /router for testing purposes

Now this is my file.js that is being used by express

const express = require('express'), router = express.Router();
var bodyParser = require('body-parser');

// Handle POST requests
router.use(bodyParser.urlencoded({ extended: false }));
router.use(bodyParser.json());

router.get('/test', checkAuthenticated, (req, res) => {
    let user = data.key;
    res.send(`Hello ${user}, this is coming from the router`)
})

module.exports = router;

Now as expected when I open the site at localhost:3000/router/test it returns a undefined error for data.key since it's not imported and then when I comment that out the line causing the error then checkAuthenticated doesn't work, even when I'm not logged in I can get access to the page, now this is the problem, how do I fix it, I've gone through the entire express router docs I couldn't find luck there.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲鸠 2025-01-21 23:13:57

您的函数不在全局范围内,您需要将其设置为全局才能看到它,如下所示:

    GLOBAL.authFunction = function(){
    
    }

建议:

1-为身份验证创建一个模块,将其命名为 authModule。

2-在您想要的地方导入模块身份验证检查中间件。

Your function is not in the global scope you need to set it to global to be able to see it like:

    GLOBAL.authFunction = function(){
    
    }

Suggestion:

1- make a module for auth name it authModule.

2- import the module auth check middleware where ever you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文