在我的 Ionic 5 電容器應用程式中,我有一個呼叫此函式的按鈕
import {
signInWithEmailAndPassword, signOut,
User, UserCredential,
} from '@angular/fire/auth';
//...等等等等等等
async signIn(value)
{
try {alert('signing in')
return (await signInWithEmailAndPassword(this.auth, value.email, value.password)).user
} catch (error) {
alert('what the heck?' error)
}
}
這在 web 和 Android 上運行良好(它回傳物件并繼續)。在 iOS 模擬器和設備上,它與 livereload 一起作業,但沒有 livereload,它什么都不做,甚至不回傳任何東西。“登錄”彈出,但不會從那里繼續。
知道為什么會這樣嗎?
uj5u.com熱心網友回復:
在您的app.module.ts檔案中,將以下內容添加到您的匯入陣列中。
provideAuth(() => {
if (Capacitor.isNativePlatform()) {
return initializeAuth(getApp(), {
persistence: indexedDBLocalPersistence,
});
} else {
return getAuth();
}
}),
并從@angular/fire/auth.
這是與 Puneet Kushway 類似的解決方案。我發現這個解決方案更好,因為它使您的app.component.ts檔案保持整潔。
uj5u.com熱心網友回復:
經過掙扎,幸運的是我找到了需要做的事情。
在您的app.component.ts 中添加以下代碼:
import { Component } from '@angular/core';
import { Capacitor } from '@capacitor/core';
import { initializeApp } from 'firebase/app';
import { indexedDBLocalPersistence, initializeAuth } from 'firebase/auth';
import { environment } from 'src/environments/environment';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.scss'],
})
export class AppComponent {
constructor() {
const app = initializeApp(environment.firebase);
if (Capacitor.isNativePlatform) {
initializeAuth(app, {
persistence: indexedDBLocalPersistence
});
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/401977.html
