ionic 6 google account firebase

7uhlpewt  于 2023-04-27  发布在  Ionic
关注(0)|答案(1)|浏览(140)

我使用离子6.当使用功能loginwithgoogle使用firebase,它的工作完美没有任何错误在te web.它带我到正确的页面“主要”,但任务是设置应用程序在Android移动的.把谷歌帐户信息和点击登录后,它带我到其他页面localhost/登录未找到.

async loginWithGoogle() {
  try {
    const provider = new GoogleAuthProvider();
    const auth = getAuth();
    signInWithPopup(auth, provider).then(async (result: any) => {
      //console.log(result)
      const credential =
        GoogleAuthProvider.credentialFromResult(result);
      //console.log(credential)
      const googleCredentials = {
        id_token: result.user.accessToken,
        email: result.user.email,
        name: result.user.displayName
      };
      //console.log(googleCredentials)
      const body = JSON.stringify({
        google_credentials: googleCredentials
      });
      const parsedBody = JSON.parse(body);
      const idToken = parsedBody.google_credentials.id_token;
      const headers = new HttpHeaders({
        'Authorization': idToken
      });
      const response: any = await this.http.post(`${this.apiUrl}/login`,
        body, {
          headers
        }).toPromise();
      this.token.setToken(response.token);
      await this.storage.set('auth-token', response.token);
      this.router.navigate(['/principal']);
    })
    //await this.afAuth.signInWithRedirect(new  firebase.auth.GoogleAuthProvider());
  } catch (error) {
    console.error('Error logging in with Google:', error);
  }
}
9lowa7mx

9lowa7mx1#

在Android上,你必须使用Firebase Android SDK。已经有电容器插件用于此。

示例

import { FirebaseAuthentication } from '@capacitor-firebase/authentication';
import {
  getAuth,
  GoogleAuthProvider,
  signInWithCredential,
} from 'firebase/auth';

const signInWithGoogle = async () => {
  // 1. Create credentials on the native layer
  const result = await FirebaseAuthentication.signInWithGoogle();
  // 2. Sign in on the web layer using the id token
  const credential = GoogleAuthProvider.credential(result.credential?.idToken);
  const auth = getAuth();
  await signInWithCredential(auth, credential);
};

Source

披露:我是@capacitor-firebase/authentication的维护者。

相关问题