Ionic应用Firebase认证后数据库HTTPS请求移动端异常求助
Ionic 移动端自有数据库请求失败问题
我在使用Ionic开发应用时遇到了请求异常:登录流程为先通过Firebase完成用户认证,再利用返回的localId和email向自有数据库发起第二次请求,获取AccessToken和RefreshToken用于后续接口交互。
该流程在浏览器环境下运行完全正常(已在多款浏览器测试),但一旦在模拟器或真机上运行应用,自有数据库的请求就会失败。
错误截图

接口可用性证明
为证明自有数据库的登录接口本身可用,我提供Postman测试截图(登录成功后应用会直接跳转,无法在应用内展示接口正常调用状态):

相关代码
AuthService 登录逻辑
login(email:string, password:string){ return this.httpClient.post<AuthResponseData>(`https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=${environment.firebaseAuthApiKey}`, { email:email, password:password, returnSecureToken:true }).pipe(tap(this.setUserData.bind(this)), switchMap(firebaseResponse =>{ return this.databaseService.loginUser(firebaseResponse.localId, firebaseResponse.email); }), tap((response:LoginResponseData) => { this.databaseService.setCredentials(response); })); }
DatabaseService 自有数据库请求逻辑
loginUser(userId:string, email:string){ const headers = new HttpHeaders({ 'Content-Type': 'application/json', }); return this.httpClient.post<LoginResponseData>(this.url + "login",{"userId": userId, "email": email}, { headers }); }
API 跨域配置(index.php 开头部分)
if (isset($_SERVER['HTTP_ORIGIN'])) { header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}"); header('Access-Control-Allow-Credentials: true'); header('Access-Control-Max-Age: 86400'); // cache for 1 day } if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) header("Access-Control-Allow-Methods: DELETE, PATCH, GET, POST, OPTIONS"); if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}"); exit(0); }
AndroidManifest.xml 配置
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" android:usesCleartextTraffic="true"> <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode" android:name=".MainActivity" android:label="@string/title_activity_main" android:theme="@style/AppTheme.NoActionBarLaunch" android:launchMode="singleTask" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data> </provider> </application> <!-- Permissions --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <!-- GeoLocation --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-feature android:name="android.hardware.location.gps" /> <!-- Camera --> <uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> </manifest>
内容的提问来源于stack exchange,提问作者Pascal von Flüe
相关产品推荐
相关产品推荐

