无法使用Auth0 / Angular

时间:2016-01-30 21:23:09

标签: javascript angularjs node.js authentication express

我使用auth0作为管理员面板的登录信息,并且运行良好。我遇到的一个问题是,在节点中,我无法出于某种原因访问' req.user'因为它返回不明身份。这是一个非常基本的设置。我有控制台记录的req.headers并设置了身份验证标头。

这是节点app.js文件

    var express = require('express');
    var path = require('path');
    var favicon = require('serve-favicon');
    var logger = require('morgan');
    var cookieParser = require('cookie-parser');
    var bodyParser = require('body-parser');
    var routes = require('./routes/index');
    var users = require('./routes/users');
    var app = express();
    var Parse = require('node-parse-api').Parse;

    var expressJwt = require('express-jwt');
    var jwt = require('jsonwebtoken');
    // view engine setup
    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine', 'jade');

    // uncomment after placing your favicon in /public
    //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
    app.use(logger('dev'));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(cookieParser());
    app.use(express.static(path.join(__dirname, 'public')));
    app.use('/bower_components',  express.static(__dirname + '/bower_components'));
    app.Parse = new Parse(options.app_id, options.api_key);
    app.use('/api', routes(app), expressJwt({secret: 'Mode'}));
    app.all('/*', function(req, res) {
        res.sendFile('index.html', { root: path.join(__dirname, '/public') });
    });

    module.exports = app;

这是AngularJS代码。

var app = angular.module('EnragedGamers', ['angularMoment', 'ngRoute', 'auth0', 'angular-storage', 'angular-jwt'])
.config(function(authProvider, $routeProvider, $locationProvider, $httpProvider, jwtInterceptorProvider) {

    $routeProvider
        .when('/', {
            templateUrl: 'home.html',
            controller: 'Home'
        })
        .when('/article/:article_id', {
            templateUrl: 'article.html',
            controller: 'Article'
        })
        .when('/admin-panel/login', {
            templateUrl: 'admin-login.html',
            controller: 'Admin-Login'
        })
        .when('/admin-panel', {
            templateUrl: 'admin.html',
            controller: 'Admin',
            requiresLogin: true
        });

    authProvider.init({
        domain: 'enragedgamers.auth0.com',
        clientID: 'MpTkAl4eosjl3SB682ZGSSrJYi03QiZp',
        loginUrl: '/admin-panel/login'
    });

    jwtInterceptorProvider.tokenGetter = ['store', function(store) {
        // Return the saved token
        return store.get('token');
    }];

    $httpProvider.interceptors.push('jwtInterceptor');

    $locationProvider.html5Mode({
        enabled: true,
        requireBase: false
    });

})
.run(function($rootScope, auth, store, jwtHelper, $location) {
    // This hooks al auth events to check everything as soon as the app starts
    $rootScope.$on('$locationChangeStart', function() {
        var token = store.get('token');
        if (token) {
          if (!jwtHelper.isTokenExpired(token)) {
            if (!auth.isAuthenticated) {
              auth.authenticate(store.get('profile'), token);
            }
          } else {
            // Either show the login page or use the refresh token to get a new idToken
            $location.path('/');
          }
        }
    });
});

这是路线文件代码

var express = require('express');
var router = express.Router();
module.exports = function(app){

    router.get('/admin/hello', function(req, res){
        console.log(req.user)
        res.status(201).json({'human': 'Hello'})
        console.log(req.body);
    });

    return router;
} 

1 个答案:

答案 0 :(得分:2)

您似乎错过了节点方面的秘密以及实际身份验证的中间人。

请参阅https://github.com/auth0/express-jwt

var jwt = require('express-jwt');

app.use(jwt({ secret: 'your secret key here'}));