将Passport的“isAuthenticated()”暴露给视图

时间:2016-12-04 09:38:16

标签: javascript angularjs express oauth-2.0 mean-stack

我正在使用OAuth 2.0登录构建一个MEAN-stack webapp。我希望能够隐藏/显示视图中的内容,具体取决于用户是否登录。

在路由中执行此操作非常简单,因为我可以访问passport的“isAuthenticated()”方法,但我无法在视图中执行此操作(隐藏/显示HTML内容,具体取决于用户是否经过身份验证)。

我希望能够做的是:

<ul>
  <li ng-show="isAuthenticated()">Profile</li>
  <li ng-hide="isAuthenticated()">Sign in</li>
</ul>

有没有办法将此功能公开给视图,以便我可以在那里使用它?还是有其他简洁的解决方案吗?

编辑:请求我的路由器

OAuth 2.0路由器

var express = require('express');
var router = express.Router();


module.exports = function(passport) {

    router.get('/bnet',
        passport.authenticate('bnet', {scope: 'wow.profile'}));

    router.get('/bnet/callback',
        passport.authenticate('bnet', {
            failureRedirect: '/' }),
        function(req, res){
            console.log('Authenticated: ' + req.isAuthenticated())
            res.redirect('https://localhost:3000/');
        });

    return router;
}

API路由器

var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var News = mongoose.model('News');
var Application = mongoose.model('Application');

isAdmin = function(){
    /*TODO create function*/
}
/*------------------ NEWS API------------------  */
router.route('/news')
        .get(function(req, res) {
            News.find(function(err, news){
                if(err){
                    return res.send(500, err);
                }
                return res.send(200, news)
            });
        })

        .post(function(req, res) {
            var news = new News();
            var origin = req.get('referer').replace('https://localhost:3000/', '')
            news.title = req.body.title;
            news.content = req.body.content;
            if(origin == 'admin/guildnews') {
                news.guild = true
            }
            news.save(function(err, news){
                if(err) {
                    return res.send(500, err);
                }
                return res.json(news);
            });
        });

router.route('/news/:id')

        .put(function(req, res) {
            News.findById(req.params.id, function(err, article) {
                if(err)
                    res.send(err)


                article.title = req.body.title;
                article.content = req.body.content;
                article.last_modified = new Date();

                article.save(function(err, article){
                    if(err)
                        res.send(err);

                    res.json(article);
                });
            });
        })

        .delete(function(req, res) {
            News.remove({
                _id: req.params.id
            }, function(err) {
                if(err)
                    res.send(err)
                res.json("deleted");
            });
        });

/*------------------NEXT API---------------------------------------*/
router.route('/applications')

        .get(function(req, res) {
            Application.find(function(err, applications){
                if(err){
                    return res.send(500, err);
                }
                return res.send(200, applications)
            });
        })

        .post(function(req, res) {
            var application = new Application();
            application.charName = req.body.charName;
            application.age = req.body.age;
            application.country = req.body.country;
            application.class = req.body.class;
            application.spec = req.body.spec;
            application.prevRaidExp = req.body.prevRaidExp;
            application.mic = req.body.mic;
            application.armoryLink = req.body.armoryLink;
            application.logsLink = req.body.logsLink;
            application.comment = req.body.comment;
            application.save(function(err, application){
                if(err) {
                    return res.send(500, err);
                }
                return res.json(application);
            });
        });

router.route('/applications/:id')

        .put(function(req, res) {
            Application.findById(req.params.id, function(err, application) {
                if(err)
                    res.send(err)


                application.charName = req.body.charName;
                application.age = req.body.age;
                application.country = req.body.country;
                application.class = req.body.class;
                application.spec = req.body.spec;
                application.prevRaidExp = req.body.prevRaidExp;
                application.mic = req.body.mic;
                application.armoryLink = req.body.armoryLink;
                application.logsLink = req.body.logsLink;
                application.comment = req.body.comment;
                application.approved = req.body.approved;
                application.declined = req.body.declined;
                application.last_modified = new Date();

                application.save(function(err, application){
                    if(err)
                        res.send(err);

                    res.json(application);
                });
            });
        })

        .delete(function(req, res) {
            Application.remove({
                _id: req.params.id
            }, function(err) {
                if(err)
                    res.send(err)
                res.json("deleted");
            });
        })

        .get(function(req, res) {
            Application.findById(req.params.id, function(err, application) {
                if(err){
                    return res.send(500, err);
                }
                return res.json(200, application)
            })
        });

function isLoggedIn(req, res, next) {

    if(req.isAuthenticated())
        return next();


    res.redirect('https://localhost:3000/');
}

module.exports = router;

0 个答案:

没有答案