简体   繁体   English

如何将Firebaseui auth与Redux集成

[英]How to integrate Firebaseui auth with Redux

I've got a working auth configuration set up using firebaseui. 我有一个使用firebaseui设置的工作auth配置。 I have a private landing page that I'd like to redirect the user to, but I'm not sure how to pass the credentialed response into my redux store. 我有一个私人登录页面,我想将用户重定向到,但我不知道如何将凭证响应传递到我的redux商店。

I basically want to call the handleClickLogin method (currently hooked to a dummy button) of my Home component from my signInSuccess callback. 我基本上想从我的signInSuccess回调调用我的Home组件的handleClickLogin方法(当前挂钩到一个虚拟按钮)。 In other words I'm trying to dispatch(login()); 换句话说,我正在尝试dispatch(login()); when I get a successfull signin, which in turn adds the flag to my redux store which I can then use to gate my private landing page. 当我成功登录时,它会将标志添加到我的redux商店,然后我可以用它来访问我的私人登录页面。 Since firebase.js is not in the component tree, I don't have access to dispatch here, so how do I get the response hooked in to my store? 由于firebase.js是不是在组件树,我没有获得dispatch这里,所以我如何才能在挂接到我店里的反应?

firebase.js firebase.js

const uiConfig = ({
  // signInSuccessUrl: '/',
  signInOptions: [
    firebase.auth.EmailAuthProvider.PROVIDER_ID,
    ],
    callbacks: {
        signInSuccess: (resp) => <<<???>>>,
    },
});

firebase.initializeApp(config);
const ui = new firebaseui.auth.AuthUI(firebase.auth());

export const startFirebaseUI = elementId => {
  ui.start(elementId, uiConfig);
};

Home.jsx (stripped down) Home.jsx (剥离)


export class Home extends React.PureComponent {
  static propTypes = {
    dispatch: PropTypes.func.isRequired,
    user: PropTypes.object.isRequired,
  };

  componentDidMount = () => {
    startFirebaseUI('#firebaseui-auth-container');
  }

  handleClickLogin = () => {
    const { dispatch } = this.props;

    dispatch(login());
  };

  render() {
    const { user } = this.props;

    return (
      <Background>
        <HomeContainer>
          <Button
            onClick={this.handleClickLogin}
          >
            <Text ml={2}>Start</Text>
          </Button>
          <div id="firebaseui-auth-container" />
        </HomeContainer>
      </Background>
    );
  }
}

function mapStateToProps(state) {
  return { user: state.user };
}

export default connect(mapStateToProps)(Home);


Somehow typing the question helped me figured it out. 以某种方式输入问题帮助我弄明白了。 Just needed to import the store and the appropriate action, then dispatch it directly. 只需要导入商店和相应的操作,然后直接发送。

import { store } from 'store/index';
import { login } from 'actions/index';

callbacks: {
    signInSuccess: (resp) => store.dispatch(login(resp)),
}

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

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