简体   繁体   English

将auth0集成到Web应用程序Express / Node js中

[英]integrate auth0 in a web app express / node js

I'm trying to add auth0 to my web app, I've followed their tutorials and other tutorials on the web, including creating account/client and everything else, but I keep getting the usual white page loading screen and after several minutes I receive this error: 我正在尝试将auth0添加到我的Web应用程序中,我已经按照他们在网络上的教程和其他教程进行操作,包括创建帐户/客户和其他所有内容,但是我一直得到正常的白页加载屏幕,几分钟后,我收到了这个错误:

ERR_EMPTY_RESPONSE ERR_EMPTY_RESPONSE

These are parts of my code: 这些是我的代码的一部分:

app.js 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 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');
  }
);
}

There are some parts that are not clear to me or on which I would like to have a confirmation: 1) the callback URL must be my homepage (180.180.180.180/home) or the real first page (180.180.180.180)? 有一些不清楚的地方或需要确认的地方:1)回调URL必须是我的主页(180.180.180.180/home)或真实的首页(180.180.180.180)? Which one should be included in the auth0 dashboard? auth0仪表板中应包含哪一个?

2) In the router, should I also specify the / login and / logout fields or should these be managed directly by the auth0 API? 2)在路由器中,我还应该指定/ login和/ logout字段,还是应该直接由auth0 API管理这些字段?

Sorry for my ignorance but it's days I have this problem, I do not understand if it's an authorization error with the auth0 account or something else. 对不起,很抱歉,但是今天是我遇到了这个问题,我不知道这是auth0帐户还是其他原因的授权错误。 I have the credentials in a .env file, but they should not be the problem, as I can access other data in them to connect to my MySQL database. 我在.env文件中具有凭据,但是它们应该不是问题,因为我可以访问其中的其他数据以连接到我的MySQL数据库。

As per the documentation of auth0 The callback URL is not necessarily the same URL to which you want users redirected after authentication. 根据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.

You can redirect the user to a non callback URL after authenticating the user and storing the same url in web storage. 在对用户进行身份验证并将相同的URL存储在Web存储中之后,您可以将用户重定向到非回调URL。 On app.get(/login)...>authenticated>>landing page>>stores the access tokens So post authentication it should login to your landing page (home). 在app.get(/ login)...>经过身份验证的>>着陆页>>上存储访问令牌,因此在验证后,它应该登录到您的着陆页(主页)。

On app.get(/logout), you can clear the access tokens or make it available for desired time and let it get expired after certain time. 在app.get(/ logout)上,您可以清除访问令牌或使其在期望的时间可用,并使其在特定时间后过期。

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

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