使用Oauth ID令牌在Firebase中注册用户

时间:2020-06-26 15:24:10

标签: javascript firebase oauth google-signin google-identity-toolkit

我正在尝试为React Native应用开发身份验证系统。我已经能够在前端为Google登录执行Oauth部分并检索ID令牌。现在,我想在后端Firebase用户中注册该用户。我本质上想将Oauth ID令牌转换为Firebase可以接受的Firebase Google标志令牌。

//front end code 
const webid= "client id"

          let redirectUrl = AuthSession.getRedirectUrl();
           let response = await AuthSession.startAsync({
               authUrl:
                   `https://accounts.google.com/o/oauth2/v2/auth?` +
                   `&client_id=${webid}` +
                   `&redirect_uri=${encodeURIComponent(redirectUrl)}` +
                   `&response_type=id_token` +
                   `&access_type=offline` +
                   `&nonce=nonce`+
                   `&prompt=consent` +
                   `&scope=${encodeURIComponent('profile openid')}`
           });

        const queryParams = this.toQueryString({
            client_id: webid,
            redirect_uri: redirectUrl,
            response_type: 'id_token', // id_token will return a JWT token
            scope: 'openid profile', // retrieve the user's profile
            nonce: 'nonce', // ideally, this will be a random value
        });
          console.log(queryParams)
        
        console.log('Authentication response', response);
         console.log(redirectUrl)
        const uri = `http://${manifest.debuggerHost.split(':').shift()}:3300`;
        console.log(uri)
        await axios.post(uri+"/api/v1/auth/google", {response: response, redirectUrl: redirectUrl})


后端代码:

 const {response, redirect} = req.body;
   console.log(response);
     const {OAuth2Client} = require('google-auth-library');
     const client = new OAuth2Client("client_id");
     async function verify() {
         const ticket = await client.verifyIdToken({
             idToken: response.params.id_token,
             audience: "Front_end_client_id",  // Specify the CLIENT_ID of the app that accesses the backend
             
         });
         const payload = ticket.getPayload();
         const userid = payload['sub'];
         console.log(payload)
         
     }
     verify().catch(console.error);
    
     axios.post("https://identitytoolkit.googleapis.com/v1/accounts:signInWithIdp?key=MY_API_KEY",
         {
             postBody:
                 {
                     id_token:response.params.id_token,
                     providerId:"google.com",

                 },
             requestUri: "http://localhost",
             returnIdpCredential: true,
             returnSecureToken: true

         }
     ).then(r => console.log(r,console.log( r))).catch( reason => console.log(reason))

因此,我试图识别它并获得返回的令牌,该令牌将被firebase singin选项接受。 似乎我的posst请求做错了,因为我一直收到错误的请求回复。

0 个答案:

没有答案
相关问题