簡體   English   中英

使用React-Router和Mixins的服務器端身份驗證

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

我正在嘗試使用React-Router的willTransitionTo進行Auth管理。

我的流程是

組件(Auth Mixin)-> WillTransitionTo(檢查是否已登錄/保存過渡)->登錄組件(成功后,重試舊的過渡)

這適用於客戶端,但是我無法在服務器端授權用戶。

零件

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

認證密信

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

驗證服務

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

服務器

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....

我想以某種方式將Flux傳遞到我的Auth Service,以便可以檢查我的商店以查看用戶是否登錄。 我嘗試過通過Mixin定義傳遞通量,但這不起作用:(。

謝謝!

顯然,這是一個想要的功能,它能夠向過渡對象中添加上下文/傳遞參數(對於willTransitionTo和willTransitionFrom)。

這是一個請求請求,其中包含大量討論該問題/用例的評論

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM