如何结合护照和棱角分明的UI路由 [英] How to combine passport and angular-ui routing

查看:132
本文介绍了如何结合护照和棱角分明的UI路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我怎么能结合的角度-UI-路由的护照。
所有的例子我发现使用Node.js的路由那里。

I'm wondering how I can combine angular-ui-routing with passport. All examples I find are using the node.js routing there for.

var routerApp = angular.module('routerApp', ['ui.router']);

routerApp.config(function($stateProvider, $urlRouterProvider) {

$urlRouterProvider.otherwise('/home');

$stateProvider

    .state('home', {
        url: '/home',
        templateUrl: 'partial-home.html'
    })

    .state('about', {
        // for example just show if is loggedIn       
    });

我怎么会在上面的代码中实现这个功能呢?

How would I implement this function in the above snippet?

function isLoggedIn(req, res, next) {

    // if user is authenticated in the session, carry on 
    if (req.isAuthenticated())
        return next();

    // if they aren't redirect them to the home page
    res.redirect('/');
}

感谢每一个提示

推荐答案

要开始一种方法是通过创建角使用的$ HTTP击中防爆preSS您的端点服务。 $ HTTP返回你可以用它来改变状态成功和错误方法的承诺。如果你正在构建一个单页应用(SPA),这可能是所有你需要知道的。例如:

One way to start is by creating a service in Angular that uses $http to hit your endpoint in Express. $http returns a promise with success and error methods you can use to change state. If you're building a Single Page App (SPA) this is probably all you need to know. For example:

// An Angular service that talks to Express
UserService.$inject = ['$http', '$state']; 

function UserService($http, $state) {

    this.loginUser = function (user) {
      $http.post("/api/login", user)
        .success(function (data, status) {
          console.log('Successful login.');
          console.log('data = ' + data); 
          console.log('status = ' + status); 
          $state.go('welcome'); // 
      })
        .error(function (data) {
          console.log('Error: ' + data);
          $state.go('somewhereelse'); 
      });
  }; 

$ state.go是一个UI路由器便捷方法,它会使您定义的状态。

$state.go is a UI Router convenience method that will render your defined state.

在防爆preSS你要编写自己的回调函数护照来控制返回什么。例如:

In Express you'll want to write your own callback function for Passport to control what is returned. For example:

 // Express Route with passport authentication and custom callback
  app.post('/api/login', function(req, res, next) {
      passport.authenticate('local-login', function(err, user, info) {
        if (err) {
        return next(err); 
      }
      if (user === false) {
        res.status(401).send(info.message);  
      } else {
        res.status(200).send(info.message); 
      }
    })(req, res, next); 
  });

在这个例子中,我使用的工作是在后台神奇本地登录护照策略。如果用户通过验证它将发送一个200回角,这将反过来触发.success。否则,它会发送一个401未授权,并火了.error。

In this example, I'm using a 'local-login' Passport strategy that's working it's magic in the background. If the user is authenticated it will send a 200 back to Angular, which will in turn trigger .success. Otherwise it will send a 401 Unauthorized and fire off .error.

这篇关于如何结合护照和棱角分明的UI路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆