reactjs - Google OAuth2 无法与 React Redux 一起使用
问题描述
有人可以帮我弄清楚为什么我的 Google OAuth 不能与我的应用程序一起使用吗?我正在使用我过去构建的另一个应用程序运行完全相同的代码,并且在该应用程序上似乎可以顺利登录和退出,但与我目前正在使用的应用程序不同。除非我完全删除 mapStateToProps 函数,否则我什至看不到登录按钮。在这种情况下,按钮会出现,但显然没有按预期工作。有任何想法吗?我正在使用 Redux 和 action creators 和 reducers 来反映用户是否已登录。
GoogleOath.js
import React from 'react';
import { connect } from 'react-redux';
import { signIn, signOut } from '../actions';
class GoogleAuth extends React.Component {
componentDidMount() {
window.gapi.load('client:auth2', () => {
window.gapi.client.init({
clientId: '//// hidden ',
scope: 'email'
}).then(() => {
this.auth = window.gapi.auth2.getAuthInstance();
this.onAuthChange(this.auth.isSignedIn.get());
this.auth.isSignedIn.listen(this.onAuthChange);
});
});
}
onAuthChange = isSignedIn => {
if (isSignedIn) {
this.props.signIn(this.auth.currentUser.get().getId());
} else {
this.props.signOut();
}
};
onSignIn = () => {
this.auth.signIn();
};
onSignOut = () => {
this.auth.signOut();
};
renderAuthButton() {
if (this.props.isSignedIn === null) {
return null;
} else if (this.props.isSignedIn) {
return (
<button onClick={this.onSignOut} className='waves-effect waves-light social-icon btn google'>
<i className="fab fa-google"></i> Sign out</button>
);
} else {
return (
<button onClick={this.onSignIn} className='waves-effect waves-light social-icon btn google'>
<i className="fab fa-google"></i> Sign in</button>
);
}
}
render() {
return <div>{this.renderAuthButton()}</div>
}
};
const mapStateToProps = state => {
return { isSignedIn: state.auth.isSignedIn };
};
export default connect(
mapStateToProps,
{ signIn, signOut }
)(GoogleAuth);
Actions Index.js
import { SIGN_IN, SIGN_OUT } from './types';
export const signIn = (userId) => {
return {
type: SIGN_IN,
payload: userId
};
};
export const signOut = () => {
return {
type: SIGN_OUT
}
};
Reducers Index.js
import { combineReducers } from 'redux';
import authReducer from './authReducer';
export default combineReducers({
auth: authReducer
});
Reducers authReducer.js
import { SIGN_IN, SIGN_OUT } from '../actions/types';
const INITIAL_STATE = {
isSignedIn: null,
userId: null
};
export default (state = INITIAL_STATE, action) => {
switch (action.type) {
case SIGN_IN:
return { ...state, isSignedIn: true, userId: action.payload };
case SIGN_OUT:
return { ...state, IsSignedIn: false, userId: null };
default:
return state;
}
};
解决方案
如果该init
方法失败,那么您将始终只有一个空白屏幕。state.auth.isSignedIn
is的初始值,null
并且您的渲染表示null
在这种情况下返回。所以你需要isSignedIn
成为一个boolean
。
向后工作,isSignedIn
将从中调度的动作中设置,该动作onAuthChange
称为监听器this.auth.isSignedIn
。该侦听器 和this.auth
都在.then()
回调中设置。只有成功时才会调用该回调window.gapi.client.init()
。
您可以添加.catch(console.error)
回调以查看您的问题。在您的 UI 中可以更好地处理失败案例,但最终您希望获得一个有效的身份验证实例!
推荐阅读
- npm - 在 npm install 上:未处理的拒绝错误:EACCES:权限被拒绝
- android - SimpleDateFormat:在 android 6 设备上解析异常
- windows - UWP 单个 UI 线程中的多个视图
- jmeter - 如何将 Jmeter 与 Adobe Analytics 集成
- syntax-error - VHDL 通用组件声明语法错误
- c# - ASP.net Foreach 循环并将数据存储在 viewbag 中以显示在视图中
- php - GCP BigQuery PHP 客户端获取项目列表
- c - 从文件中读取直到找到空间
- php - 如何在 mysql 数据库中存储泰米尔语字体
- java - 如何使用 ThreeTenABP 将 java.util.Date 更改为 ISO 字符串