[英]Initializing auth2 for signing in users in React
I am trying to manage a users authentication in a React App. 我正在尝试在React App中管理用户身份验证。
I keep getting errors related to dealing with getAuthInstance()
and trying to signOut()
a user. 我不断收到有关处理错误getAuthInstance()
并试图signOut()
的用户。
First I am trying t load the gapi.auth2
首先,我正在尝试加载gapi.auth2
componentDidMount() {
var auth2;
var googleUser;
var appStart = function() {
gapi.load('auth2', initSigninV2);
}
var initSigninV2 = function() {
auth2 = gapi.auth2.init({
client_id: '817677528939-dss5sreclldv1inb26tb3tueac98d24r.apps.googleusercontent.com',
scope: 'profile email'
});
}
googleUser = auth2.currentUser.get()
console.log(googleUser)
When the page loads, I have everything under componentDidMount()
页面加载时,所有内容都在componentDidMount()
However, auth2
keeps returning as undefined and thus getting an error. 但是, auth2
继续返回未定义状态,从而收到错误消息。 What am I doing wrong? 我究竟做错了什么?
Why isn't auth2 initializing properly? 为什么auth2无法正确初始化?
There are a few issues which I can see: 我可以看到一些问题:
auth2.currentUser.get()
before auth2 = gapi.auth2.init
is executed 您正在执行auth2 = gapi.auth2.init
之前调用auth2.currentUser.get()
appStart
您永远不会调用函数appStart
This should get it working for you 这应该可以为您工作
function componentDidMount() {
var auth2;
var googleUser;
var appStart = function () {
gapi.load('auth2', initSigninV2);
}
var initSigninV2 = function () {
auth2 = gapi.auth2.init({
client_id: '817677528939-dss5sreclldv1inb26tb3tueac98d24r.apps.googleusercontent.com',
scope: 'profile email'
}).then((auth2) => {
googleUser = auth2.currentUser.get();
console.log(googleUser)
})
}
appStart();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.