【问题标题】:integrate auth0 in a web app express / node js将 auth0 集成到 web 应用 express / node js
【发布时间】:2018-09-02 04:17:46
【问题描述】:

我正在尝试将 auth0 添加到我的网络应用程序中,我已经按照他们的教程和网络上的其他教程进行操作,包括创建帐户/客户端和其他所有内容,但是我一直收到通常的白页加载屏幕,并且在几次之后分钟我收到此错误:

ERR_EMPTY_RESPONSE

这些是我的代码的一部分:

app.js

...
var cookieParser = require('cookie-parser');
var session = require('express-session');
var passport = require('passport');
var Auth0Strategy = require('passport-auth0');
...
// Configure Passport to use Auth0
var strategy = new Auth0Strategy(
 {
  domain: process.env.AUTH0_DOMAIN,
  clientID: process.env.AUTH0_CLIENT_ID,
  clientSecret: process.env.AUTH0_CLIENT_SECRET,
  callbackURL: process.env.AUTH0_CALLBACK_URL
  },
  (accessToken, refreshToken, extraParams, profile, done) => {
  return done(null, profile);
  }
 );

passport.use(strategy);

passport.serializeUser(function(user, done) {
 done(null, user);
});

passport.deserializeUser(function(user, done) {
 done(null, user);
});
...
app.use(cookieParser());
app.use(
  session(
    {
     secret: uuid(),
     resave: false,
     saveUninitialized: false
    }
  )
);
app.use(passport.initialize());
app.use(passport.session());
...
app.get('/', routes.index);
app.get('/home', routes.home);
...
http.createServer(app).listen(app.get('port'), function(){
  console.log('Server listening on port: ' + app.get('port'));
});
module.exports = app;

index.js

exports.home = function(req, res){
 res.render('home', { title: ' homepage ' });
};

exports.index = function(req, res){
 var express = require('express');
 var passport = require('passport');
 var router = express.Router();

var env = {
  AUTH0_CLIENT_ID: process.env.AUTH0_CLIENT_ID,
  AUTH0_DOMAIN: process.env.AUTH0_DOMAIN,
  AUTH0_CALLBACK_URL: process.env.AUTH0_CALLBACK_URL 
};

// GET home page. 
router.get('/', function(req, res, next) {
  res.render('home', { title: ' homepage ' });
});

// Perform the login
router.get(
  '/login',
  passport.authenticate('auth0', {
    clientID: env.AUTH0_CLIENT_ID,
    domain: env.AUTH0_DOMAIN,
    redirectUri: env.AUTH0_CALLBACK_URL,
    audience: 'https://' + env.AUTH0_DOMAIN + '/userinfo',
    responseType: 'code',
    scope: 'openid'
  }),
  function(req, res) {
    res.redirect('/');
  }
);

// Perform session logout and redirect to homepage
router.get('/logout', (req, res) => {
  req.logout();
  res.redirect('/');
});

// Perform the final stage of authentication and redirect to '/home'
router.get(
  '/callback',
  passport.authenticate('auth0', {
    failureRedirect: '/'
  }),
  function(req, res) {
    res.redirect(req.session.returnTo || '/home');
  }
);
}

有些部分我不清楚,或者我想确认一下: 1)回调地址一定是我的首页(180.180.180.180/home)还是真正的首页(180.180.180.180)? auth0 仪表板中应该包含哪一个?

2) 在路由器中,我应该同时指定 /login 和 /logout 字段还是应该由 auth0 API 直接管理这些字段?

对不起,我的无知,但我有这个问题的日子,我不明白这是 auth0 帐户的授权错误还是其他什么。 我在 .env 文件中有凭据,但它们应该不是问题,因为我可以访问其中的其他数据以连接到我的 MySQL 数据库。

【问题讨论】:

  • env.AUTH0_CLIENT_ID。你从局部变量中得到这个吗?你设置了吗?
  • 是的,没错,它都在我的 .env 文件中。

标签: node.js express authentication auth0


【解决方案1】:

根据 auth0 The callback URL is not necessarily the same URL to which you want users redirected after authentication.的文档

redirect_uri field is used as a callback URL. Auth0 invokes callback URLs after the authentication process and are where your application gets routed.

在对用户进行身份验证并将相同的 URL 存储在 Web 存储中后,您可以将用户重定向到非回调 URL。 在 app.get(/login)...>authenticated>>登陆页面>>存储访问令牌 因此,发布身份验证后,它应该登录到您的登录页面(主页)。

在 app.get(/logout) 上,您可以清除访问令牌或使其在所需时间可用,并让它在一定时间后过期。

【讨论】:

  • 感谢您的回复,我会记住这一点,但这并不能解决我的主要登录问题。你知道吗?
  • 使用第三方 auth0 提供程序,例如护照,以避免从头开始编写所有内容并偶然发现令人不安的错误。请找到此链接:github.com/auth0/passport-auth0。您可以参考示例代码并自行格式化作为替代。我使用 github /passport 策略。在此处找到链接 (github.com/jaredhanson/passport)
  • 感谢您的回复,我只对使用 auth0 工作及其与非常高效的 android 系统的集成感兴趣。对于 passport-auth0,我已经在使用它了。
  • 这是您遇到的唯一错误吗?我可以看到您没有在 app.js 文件中处理 CORS。是否与那个 x-access control 错误有关?
  • 是的,这很奇怪,我只是在浏览器尝试加载我的登录页面一段时间后才收到此错误。几天后,我直接问他们,但我仍然没有得到任何答复。对不起,但我对它很陌生,我从未听说过 CORS,你认为这取决于这个吗?没有足够的 auth0 凭据和包来允许交换信息?
猜你喜欢
  • 2020-03-02
  • 2021-04-18
  • 1970-01-01
  • 2022-08-19
  • 1970-01-01
  • 2017-04-03
  • 2018-07-02
  • 1970-01-01
  • 2014-07-24
相关资源
最近更新 更多