我一直在尝试将Google的Oauth2添加到我的ReactJs应用中
但我有两个错误,可能来自语法错误
这是代码中我得到错误的部分
import React from 'react'
import { useGoogleLogin } from 'react-use-googlelogin'
const GoogleAuthContext = React.createContext()
export const GoogleAuthProvider = ({ children }) => {
const googleAuth = useGoogleLogin({
clientId: process.env.Google_Client_ID,
})
return (
<GoogleAuthContext.Provider value={googleAuth}>
{children}
</GoogleAuthContext.Provider>
)
}
export const useGoogleAuth = () => React.useContext(GoogleAuthContext);
当我用自己的ID替换Google_Client_ID时,我会得到这些错误
意外的令牌,应为""(8:25(
第二个是
意外的令牌,应为":"(8:37(
这就是我使用客户端ID 的方式
const googleAuth = useGoogleLogin({
clientId:process.env.658977310896-knrl3gka66fldh83dao2rhgbblmd4un9.apps.googleusercontent.com,
})
感谢您花时间阅读我的问题
我找到了问题的解决方案
我使用了一个变量,并将客户端id的值赋予它,而不是直接使用它。
import React from 'react'
import { useGoogleLogin } from 'react-use-googlelogin'
const GoogleAuthContext = React.createContext()
export const GoogleAuthProvider = ({ children }) => {
//assigning a variable the id value and using the variable instead solved the issue ^_^
const clientId =
'Google_Client_ID';
const googleAuth = useGoogleLogin({
clientId,
})