福田哪家建设网站好,全民体育世界杯,南昌市建设工程质量监督网站,个人做网站多少钱Firebase提供了一套SDK用于抹平不用平台鉴权的差异性#xff0c;为了方便使用#xff0c;将基于firebase_auth、google_sign_in、sign_in_with_apple的依赖封装一套登录统一的功能集成。鉴权后将会获取idToken#xff0c;该数据需要传给服务器进行校验。步骤#xff1a;1.定…Firebase提供了一套SDK用于抹平不用平台鉴权的差异性为了方便使用将基于firebase_auth、google_sign_in、sign_in_with_apple的依赖封装一套登录统一的功能集成。鉴权后将会获取idToken该数据需要传给服务器进行校验。步骤1.定义模块最终返回的数据结构class FirebaseAuthInfo { String? idToken; String? providerId; FirebaseAuthInfo({this.idToken, this.providerId}); Map toJson() { return {idToken: idToken, providerId: providerId}; } }2. 定义工具类提供login和logout方法enum LoginType { google, apple, phone, email } class LoginMethod { late LoginType loginType; factory LoginMethod(LoginType loginType) LoginMethod._(loginType); LoginMethod._(this.loginType) { switch (loginType) { case LoginType.google: break; case LoginType.apple: break; case LoginType.phone: break; case LoginType.email: break; } } Future login() async { switch (loginType) { case LoginType.google: return await _googleSign(); case LoginType.apple: return await _appleSign(); case LoginType.phone: return; case LoginType.email: return; } } void logout() { switch (loginType) { case LoginType.google: _googleOut(); break; case LoginType.apple: break; case LoginType.phone: break; case LoginType.email: break; } } }3. 基于Firebase Email的鉴权方法该方法调用时Android和IOS需要配置对应SchemeUrl通过Applink才能实现FutureFirebaseAuthInfo? verifyEmail(String email, Uri uri) async { if (FirebaseAuth.instance.isSignInWithEmailLink(uri.toString())) { try { final firebaseAuth await FirebaseAuth.instance .signInWithEmailLink(email: email, emailLink: uri.toString()); var idToken await firebaseAuth.user?.getIdToken(); return Future.value(FirebaseAuthInfo( idToken: idToken, providerId: firebaseAuth.credential?.providerId)); } catch (error) { print(Error signing in with email link.); } return null; } else { return null; } }4. 基于Firebase Phone的鉴权方法验证码条数需要Firebase console中配置FutureFirebaseAuthInfo? phoneSign(String code, String phone) async { CompleterFirebaseAuthInfo? completer Completer(); await FirebaseAuth.instance.verifyPhoneNumber( phoneNumber: $code $phone, verificationCompleted: (PhoneAuthCredential credential) async { final firebaseAuth await FirebaseAuth.instance.signInWithCredential(credential); var idToken await firebaseAuth.user?.getIdToken(); if (!completer.isCompleted) { completer.complete(FirebaseAuthInfo( idToken: idToken, providerId: firebaseAuth.credential?.providerId)); } }, verificationFailed: (FirebaseAuthException e) { if (!completer.isCompleted) { completer.complete(null); } }, codeSent: (String verificationId, int? resendToken) {}, codeAutoRetrievalTimeout: (String verificationId) { if (!completer.isCompleted) { completer.complete(null); } }, ); return completer.future; }5. 基于Google Account的鉴权方法获取账户信息的作用域请查询官方文档如需要每次登录都可以选择指定的google账号的动作需要登录前先登出否则本地有缓存下次调用将直接复用上一次的账号信息。void _googleOut() { try { var googleSignIn GoogleSignIn( scopes: String[ https://www.googleapis.com/auth/userinfo.email, https://www.googleapis.com/auth/userinfo.profile ], ); googleSignIn.signOut(); } catch (e) {} } FutureFirebaseAuthInfo? _googleSign() async { try { var googleSignIn GoogleSignIn( scopes: String[ https://www.googleapis.com/auth/userinfo.email, https://www.googleapis.com/auth/userinfo.profile ], ); await googleSignIn.signOut(); var googleAccountInfo await googleSignIn.signIn(); var googleAuth await googleAccountInfo?.authentication; if (googleAuth ! null) { var firebaseAuth await FirebaseAuth.instance .signInWithCredential(GoogleAuthProvider.credential( accessToken: googleAuth.accessToken, idToken: googleAuth.idToken, )); var idToken await firebaseAuth.user?.getIdToken(); return FirebaseAuthInfo( idToken: idToken, providerId: firebaseAuth.credential?.providerId); } else { return Future.value(); } } catch (e) { debugPrint(Auth error !:${e.toString()}); } return null; } }6. 基于Apple Account的鉴权方法FutureFirebaseAuthInfo? _appleSign() async { try { final appleIdCredential await SignInWithApple.getAppleIDCredential( scopes: [ AppleIDAuthorizationScopes.email, ], ); final oAuthProvider OAuthProvider(apple.com); final credential oAuthProvider.credential( idToken: appleIdCredential.identityToken, accessToken: appleIdCredential.authorizationCode, ); var identify await FirebaseAuth.instance.signInWithCredential(credential); var idToken await identify.user?.getIdToken(); return FirebaseAuthInfo(idToken: idToken, providerId: apple.com); } catch (e) { debugPrint(Auth error !:${e.toString()}); } return null; }