詳解如何在Flutter中集成華為認(rèn)證服務(wù)
最近發(fā)現(xiàn)華為AGC認(rèn)證服務(wù)支持Flutter框架了,期待這個(gè)平臺(tái)的支持已經(jīng)很久了,所以迫不及待接入了,關(guān)聯(lián)了自己的郵箱等賬號(hào)。
集成步驟
安裝flutter環(huán)境
a) 下載Flutter sdk包,地址:https://flutter.dev/docs/get-started/install/windows
將壓縮包解壓到任意文件夾,例如D:\Flutter
b) 將flutter命令文件添加到環(huán)境變量中,此處我添加的Path為D:\Flutter\flutter_windows_1.22.2-stable\flutter\bin。
c) 在Android Studio中點(diǎn)擊“File-Settings-Plugins”,下載Flutter和Dart插件,重啟Android Studio使插件生效。
開(kāi)通服務(wù)&創(chuàng)建工程
a) 在AGC創(chuàng)建Android應(yīng)用并開(kāi)通認(rèn)證服務(wù)
b) 開(kāi)啟認(rèn)證服務(wù)中的匿名帳號(hào),手機(jī)帳號(hào),郵箱帳號(hào)
c) 在Android Studio中新建Flutter工程
d) 將agconnect-services.json文件放入Android/app目錄下
e) 配置Maven倉(cāng)地址和AGC插件地址。
a. 打開(kāi)Flutter項(xiàng)目android文件夾下的build.gradle文件。
b. 在allprojects ->repositories里面配置maven倉(cāng)地址。
c. 在buildscript->repositories中配置maven倉(cāng)地址。
d. 在buildscript->dependencies中配置AppGallery Connect插件地址。
添加編譯依賴(lài)和AGC插件地址。
a. 打開(kāi)Flutter項(xiàng)目android/app文件夾下的build.gradle文件。
b. 在文件中添加如下配置。
集成SDK
在Flutter項(xiàng)目的pubspec.yaml文件中添加依賴(lài):
dependencies: flutter: sdk: flutter # Add the following line: agconnect_auth: ^1.1.0
然后點(diǎn)擊Pub get進(jìn)行同步
接入功能
匿名帳號(hào)登錄
匿名帳號(hào)只需要調(diào)用signInAnonymously接口進(jìn)行登錄
_signIn() async { AGCAuth.instance.signInAnonymously().then((value) { setState(() { _log = 'signInAnonymously = ${value.user.uid} , ${value.user.providerId}'; }); }); }
通過(guò)value,我們可以獲取到用戶(hù)信息,例如這里我們獲取到了user的id。
手機(jī)號(hào)&郵箱認(rèn)證
手機(jī)號(hào)郵箱賬號(hào)認(rèn)證首先需要發(fā)送驗(yàn)證碼,
手機(jī)號(hào)請(qǐng)求驗(yàn)證碼,即調(diào)用requestVerifyCode方法,傳入手機(jī)號(hào)、國(guó)家碼、設(shè)置項(xiàng)作為參數(shù):
_requestPhoneVerifyCode(VerifyCodeAction action) { String countryCode = _countryCodeController.text; String phoneNumber = _phoneNumberController.text; VerifyCodeSettings settings = VerifyCodeSettings(action, sendInterval: 30); PhoneAuthProvider.requestVerifyCode(countryCode, phoneNumber, settings).then((value) => print(value.validityPeriod)); }
郵箱請(qǐng)求驗(yàn)證碼,即調(diào)用requestVerifyCode方法,傳入郵箱、設(shè)置項(xiàng)作為參數(shù):
_requestEmailVerifyCode(VerifyCodeAction action) { String email = _emailController.text; VerifyCodeSettings settings = VerifyCodeSettings(action, sendInterval: 30); EmailAuthProvider.requestVerifyCode(email, settings) .then((value) => print(value.validityPeriod)); }
而后是創(chuàng)建用戶(hù)的操作:
創(chuàng)建手機(jī)賬號(hào)用戶(hù),需要調(diào)用createPhoneUser方法,傳入封裝好的PhoneUser對(duì)象
_createPhoneUser() async { bool result = await _showPhoneDialog(VerifyCodeAction.registerLogin); if (result == null) { print("cancel"); return; } String countryCode = _countryCodeController.text; String phoneNumber = _phoneNumberController.text; String verifyCode = _verifyCodeController.text; String password = _passwordController.text; AGCAuth.instance.createPhoneUser(PhoneUser(countryCode, phoneNumber, verifyCode, password: password)) .then((value) { setState(() { _log = 'createPhoneUser = ${value.user.uid} , ${value.user.providerId}'; }); }).catchError((error)=>print(error)); }
創(chuàng)建郵箱賬號(hào)用戶(hù),需要調(diào)用createEmailUser方法,傳入封裝好的EmailUser對(duì)象。
_createEmailUser() async { bool result = await _showEmailDialog(VerifyCodeAction.registerLogin); if (result == null) { print("cancel"); return; } String email = _emailController.text; String verifyCode = _verifyCodeController.text; String password = _passwordController.text; AGCAuth.instance .createEmailUser(EmailUser(email, verifyCode, password: password)) .then((value) { setState(() { _log = 'createEmailUser = ${value.user.uid} , ${value.user.providerId}'; }); }).catchError((error) => print(error)); }
最后是登錄功能,有兩種登錄模式,一種是密碼登錄方式:
手機(jī)賬號(hào),調(diào)用signIn方法,傳入使用手機(jī)號(hào)等生成的認(rèn)證憑據(jù)。
_signInWithPassword() async { bool result = await _showPhoneDialog(VerifyCodeAction.registerLogin); if (result == null) { print("cancel"); return; } String countryCode = _countryCodeController.text; String phoneNumber = _phoneNumberController.text; String password = _passwordController.text; AGCAuthCredential credential = PhoneAuthProvider.credentialWithPassword(countryCode, phoneNumber, password); AGCAuth.instance.signIn(credential).then((value) { setState(() { _log = 'signInWithPassword = ${value.user.uid} , ${value.user.providerId}'; }); }); }
郵箱賬號(hào):調(diào)用signIn方法,傳入通過(guò)郵箱和密碼生成的認(rèn)證憑據(jù)。
_signInWithPassword() async { bool result = await _showEmailDialog(VerifyCodeAction.registerLogin); if (result == null) { print("cancel"); return; } String email = _emailController.text; String password = _passwordController.text; AGCAuthCredential credential = EmailAuthProvider.credentialWithPassword(email, password); AGCAuth.instance.signIn(credential).then((value) { setState(() { _log = 'signInWithPassword = ${value.user.uid} , ${value.user.providerId}'; }); }); }
另一種是驗(yàn)證碼登錄方式:
手機(jī)賬號(hào):調(diào)用signIn方法,傳入通過(guò)手機(jī)、驗(yàn)證碼和密碼生成的認(rèn)證憑據(jù)。
_signInWithVerifyCode() async { bool result = await _showPhoneDialog(VerifyCodeAction.registerLogin); if (result == null) { print("cancel"); return; } String countryCode = _countryCodeController.text; String phoneNumber = _phoneNumberController.text; String verifyCode = _verifyCodeController.text; String password = _passwordController.text; AGCAuthCredential credential = PhoneAuthProvider.credentialWithVerifyCode(countryCode, phoneNumber, verifyCode, password: password); AGCAuth.instance.signIn(credential).then((value) { setState(() { _log = 'signInWithVerifyCode = ${value.user.uid} , ${value.user.providerId}'; }); }); }
郵箱賬號(hào):調(diào)用signIn方法,傳入通過(guò)郵箱、驗(yàn)證碼和密碼生成的認(rèn)證憑據(jù)。
_signInWithVerifyCode() async { bool result = await _showEmailDialog(VerifyCodeAction.registerLogin); if (result == null) { print("cancel"); return; } String email = _emailController.text; String verifyCode = _verifyCodeController.text; String password = _passwordController.text; AGCAuthCredential credential = EmailAuthProvider.credentialWithVerifyCode( email, verifyCode, password: password); AGCAuth.instance.signIn(credential).then((value) { setState(() { _log = 'signInWithVerifyCode = ${value.user.uid} , ${value.user.providerId}'; }); }); }
自有賬號(hào)
自有賬號(hào)創(chuàng)建jwt獲取token等步驟為server端步驟,端側(cè)只需要取到token進(jìn)行登錄即可。
_signIn() async { bool result = await _showSelfBuildDialog(VerifyCodeAction.registerLogin); if (result == null) { print("cancel"); return; } String token = _selfBuildController.text; AGCAuthCredential credential = SelfBuildAuthProvider.credentialWithToken(token); AGCAuth.instance.signIn(credential).then((value) { setState(() { _log = 'signIn = ${value.user.uid} , ${value.user.providerId}'; }); }); }
打包
與Android相同,只需要在Android Studio中點(diǎn)擊運(yùn)行即可
欲了解更多詳情請(qǐng)參見(jiàn):
認(rèn)證服務(wù)開(kāi)發(fā)指南:
認(rèn)證服務(wù)codelab(Android):
https://developer.huawei.com/consumer/cn/codelab/AuthenticationService/index.html#0
原文鏈接:https://developer.huawei.com/consumer/cn/forum/topic/0201436847294530241?fid=0101271690375130218
原作者:Mayism
到此這篇關(guān)于詳解如何在Flutter中集成華為認(rèn)證服務(wù)的文章就介紹到這了,更多相關(guān)Flutter集成華為認(rèn)證服務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android實(shí)現(xiàn)讀寫(xiě)SD卡
本文給大家分享的是Android實(shí)現(xiàn)讀寫(xiě)SD卡的代碼以及使用的時(shí)候的注意事項(xiàng),非常的實(shí)用,有需要的小伙伴可以參考下。2016-05-05Android XmlResourceParser出錯(cuò)解決辦法
這篇文章主要介紹了Android XmlResourceParser出錯(cuò)解決辦法的相關(guān)資料,需要的朋友可以參考下2017-05-05Android使用GridView實(shí)現(xiàn)橫向滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android使用GridView實(shí)現(xiàn)橫向滾動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android實(shí)現(xiàn)listview滑動(dòng)時(shí)漸隱漸現(xiàn)頂部欄實(shí)例代碼
android中實(shí)現(xiàn)listview滑動(dòng)時(shí)漸隱漸現(xiàn)頂部欄只是在獲取listview的滑動(dòng)距離上可能沒(méi)法直接獲取,需要?jiǎng)討B(tài)的去計(jì)算。感興趣的朋友一起看看吧2016-10-10Android開(kāi)發(fā)之AlarmManager的用法詳解
這篇文章主要介紹了Android開(kāi)發(fā)之AlarmManager的用法,是Android應(yīng)用開(kāi)發(fā)中非常實(shí)用的技能,需要的朋友可以參考下2014-07-07Android實(shí)現(xiàn)熱門(mén)標(biāo)簽的流式布局
這篇文章主要介紹了Android實(shí)現(xiàn)熱門(mén)標(biāo)簽的流式布局的詳細(xì)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-12-12Android實(shí)現(xiàn)ListView異步加載的方法(改進(jìn)版)
這篇文章主要介紹了Android實(shí)現(xiàn)ListView異步加載的方法,針對(duì)前面介紹的方法進(jìn)行了線(xiàn)程操作的改進(jìn),具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08