简体   繁体   English

使用React-Router和Mixins的服务器端身份验证

[英]Server Side Auth with React-Router and Mixins

I'm trying to use React-Router's willTransitionTo for Auth management. 我正在尝试使用React-Router的willTransitionTo进行Auth管理。

My flow is 我的流程是

Component (Auth Mixin) -> WillTransitionTo (checks if logged in / saves transition) -> Login Component (on success, retries old transition) 组件(Auth Mixin)-> WillTransitionTo(检查是否已登录/保存过渡)->登录组件(成功后,重试旧的过渡)

This works for the client side, however I'm having trouble authorizing users on the server side. 这适用于客户端,但是我无法在服务器端授权用户。

Component 零件

React.createClass({
    mixins: [Authentication]
    render: function() {
    return <h1>Blah</h1>;
  }
})

Authentication Miixin 认证密信

var Authentication = {
    statics: {
        willTransitionTo: function(transition) {
            if (!auth.loggedIn()) {
                PromptLogin.attemptedTransition = transition;
                transition.redirect('/login');
            }
        }
    }
};

Auth Service 验证服务

module.exports = {
    loggedIn: function() {
        if (typeof(window) !== 'undefined' && window.flux) {
            return flux.store('UserStore').state.currentUser.loggedIn
        } else {
            return false;
        }
    }
};

Server 服务器

Router.create({
    routes: AppRoutes,
    location: req.url,

    onAbort: function (reason) {
        if (reason instanceof Error) {
            next(reason)
        } else if (reason.constructor.name === 'Redirect') {
            // next(null, newRouter, {redirect: reason})
            res.redirect('/login');
        } else {
           next(null, newRouter, reason)
        }
    },

    onError: function (err) {
        next(err)
    }
});

Router.run....

I want to somehow pass Flux to my Auth Service so I can check my stores to see if a user is logged in or not. 我想以某种方式将Flux传递到我的Auth Service,以便可以检查我的商店以查看用户是否登录。 I've tried passing in flux through the Mixin definition, but that doesn't work :(. 我尝试过通过Mixin定义传递通量,但这不起作用:(。

Thanks! 谢谢!

Apparently it's a wanted feature to be able to add context / pass params into the transition object (for willTransitionTo and willTransitionFrom). 显然,这是一个想要的功能,它能够向过渡对象中添加上下文/传递参数(对于willTransitionTo和willTransitionFrom)。

Here's a pull request with lots of comments discussing the issue / use cases 这是一个请求请求,其中包含大量讨论该问题/用例的评论

https://github.com/rackt/react-router/pull/590 https://github.com/rackt/react-router/pull/590

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

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