小编Sar*_*rah的帖子

如何在使用 Passport(react、react-router、express、passport)进行社交身份验证后重定向到正确的客户端路由

我有一个 React/Redux/React 路由器前端,Node/Express 后端。我正在使用 Passport(各种策略,包括 Facebook、Google 和 Github)进行身份验证。

我想要发生的事情:

  1. 未经身份验证的用户尝试访问受保护的客户端路由(类似于/posts/:postid,并被重定向到/login。(反应路由器正在处理这部分)

  2. 用户单击“使用 Facebook 登录”按钮(或其他社交身份验证服务)

  3. 身份验证后,用户会自动重定向回他们在步骤 1 中尝试访问的路由。

正在发生的事情:

我发现使用 React 前端成功处理 Passport 社交身份验证的唯一方法是将“使用 Facebook 登录”按钮包装在<a>标签中:

<a href="http://localhost:8080/auth/facebook">Facebook Login</a>

如果我尝试将其作为 API 调用而不是链接来执行,我总是会收到一条错误消息(这里更详细地解释了这个问题:Authentication with Passport + Facebook + Express + create-react-app + React-Router +代理)

因此,用户点击链接,命中 Express API,成功通过 Passport 进行身份验证,然后 Passport 重定向到回调路由 ( http://localhost:8080/auth/facebook/callback)。

在回调函数中,我需要 (1) 将用户对象和令牌返回给客户端,以及 (2) 重定向到客户端路由——要么是他们在重定向到之前尝试访问的受保护路由/login,要么是一些默认路由,如//dashboard

但是由于在 Express 中没有办法同时做这两件事(我不能res.sendres.redirect,我必须选择一个),我一直在以一种笨拙的方式处理它: res.redirect(`${CLIENT_URL}/user/${userId}`)

这会在/user …

node.js express reactjs passport.js react-router

8
推荐指数
1
解决办法
1万
查看次数

如果选中单选按钮,则切换表单字段的可见性

我最初试图隐藏一组表单字段.然后,如果选择了单选按钮,则显示该组字段.然后,如果取消选择该无线电,则再次隐藏它们.

前两个工作正常(最初隐藏字段,如果选择了无线电则显示它们).但是当取消选择无线电时,我无法让他们再次隐藏.我确定我错过了一些明显的东西,但我无法弄清楚它是什么.

http://jsfiddle.net/C5PMy/

$(document).ready(function() {

toggleFields(); //call this first so we start out with the correct visibility depending on the selected form values

    //Hide the fields initially
    $("#bsd-field-custom-105966-group").hide();
    $("#bsd-field-custom-105967-group").hide();
    $("#bsd-field-custom-105968-group").hide();
    $("#bsd-field-custom-105978").hide();
    $("#bsd-field-custom-105969-group").hide();
    $("#bsd-field-custom-105970").hide();
    $("#bsd-field-custom-105979").hide();
    $("#bsd-field-custom-105971-group").hide();
    $("#bsd-field-custom-105972").hide(); 


//Show the fields only if lodging is required
       $("#custom-105965_0").change(function () {
    toggleFields();
});


});
//this toggles the visibility of the other lodging fields depending on the current     selected value of the "lodging required" field
function toggleFields() {
if ($(("#custom-105965_0").checked)) {

    $("#bsd-field-custom-105966-group").show();
    $("#bsd-field-custom-105967-group").show();
    $("#bsd-field-custom-105968-group").show();
    $("#bsd-field-custom-105978").show(); …
Run Code Online (Sandbox Code Playgroud)

jquery toggle radio-button

1
推荐指数
1
解决办法
1885
查看次数