【问题标题】:redirect when user is connected to facebook/google using Reactjs当用户使用 Reactjs 连接到 facebook/google 时重定向
【发布时间】:2019-11-14 12:42:02
【问题描述】:

当他成功连接到 facebook 或 google 时,我需要重定向用户。 这是我的路线:

/*******************GOOGLE AUTH ROUTES********************* */
// auth with google+
router.get('/auth/google', passport.authenticate('google', {
    scope: [
        'https://www.googleapis.com/auth/userinfo.profile',
        'https://www.googleapis.com/auth/userinfo.email'
    ]
}));
// callback route for google to redirect to
// hand control to passport to use code to grab profile info
router.get('/auth/google/callback*', passport.authenticate('google'), (req, res) => {
    resultats = {
        "success": true,
        "message": 'SUCCESS',
        "result": req.user
    }
    res.json(resultats);
});
/*******************END GOOGLE AUTH********************* */

这是我的链接(fb/google):

 <div className="rs-btn">
                                    <a href={`${API}/auth/facebook`} target="_blank"className="btn facebook">
                                        <FontAwesomeIcon icon={faFacebookF} />
                                    </a>
                                    <a href={`${API}/auth/google`} target="_blank"className="btn google">
                                        <FontAwesomeIcon icon={faGoogle} />
                                    </a>
                                </div>

最后是我的 config.js:

module.exports = {
    jwtsecret : "fatboardauth",
    session : {
        cookieKey : "thisisthefatboardK2019eH67882019"
    },
    oauth : {
        facebook : {
            clientID : 737996303314074,
            clientSecret : "06a5efdc93ebe7b84567874e4438d44d",
            callbackURL : "http://localhost:3001/auth/facebook/callback"
        },
        google : {
            clientID : "8216500008636-529u2au433fk80los8dtl841mvlab15q.apps.googleusercontent.com",
            clientSecret : "XB7gZyHUKYGJFkZgo-YvITtDI",
            redirect : "http://localhost:3001/auth/google/callback"
        }
    },
    supportemail : "fatboarrestaurant@gmail.com",
    gmailPSW : "myapp123!",
    secretKey : "c2VjcmV0S2DFGBBN?ZmF0Ym9hcjIwMTk=",
    schemes : 'http://',
};

例如,当用户在谷歌中连接时,我的浏览器中有这个:

{"success":true,"message":"SUCCESS","result":{"google":{"id":"112886936395081056197","email":"meryem.achemlal@gmail.com","name":"Achemlal Meryem"},"prenom":"Mem","nom":"Ach","tel":null,"dateCreation":"2019-11-13T18:45:58.69Z","datenaissance":null,"resetpasswordtoken":null,"resetpasswordexpires":null,"role":"user","enable":true,"_id":"5dcc4f7jh68861887ac259a","email":"mem.ache@gmail.com","__v":0}}

但我想要的是用户重定向到主页。 我怎么能用 react 做到这一点?

谢谢

【问题讨论】:

    标签: node.js reactjs authentication routes


    【解决方案1】:

    你必须使用res.redirect('/path')

    router.get('/auth/google/callback*', passport.authenticate('google'), (req, res) => {
        resultats = {
            "success": true,
            "message": 'SUCCESS',
            "result": req.user
        }
        res.redirect('/');
    });
    

    【讨论】:

    • 您好,感谢您的回答,请问我该如何在前面(在我的组件中)而不是路由?
    • window.location.href = "http://www.google.com"; 用于客户端 javascript。
    • 我应该在 .. 中为它做一个点击功能吧?
    • 我做了这个,但它不起作用:localhost:3000"} href={${API}/auth/google} target="_blank"className= "btn google">
    猜你喜欢
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    相关资源
    最近更新 更多