我想在此 flutter 应用程序上添加 Microsoft 登录。我的学校正在举办一场竞赛,我们都有 Microsoft 帐户,如果我能让它在移动设备上运行,这些帐户就能很好地集成。我已经实现了它的网络版本,并且它通过 Firebase 进行身份验证。我想实现一个移动版本,该版本还可以通过 Microsoft 帐户使用 Firebase 进行身份验证。我该怎么做?是否可以通过移动设备上的弹出屏幕(如网络版本)来执行此操作?下面是使其在网络上运行的代码,以及我想在其中实现移动版本的代码。现在,该功能通过单击按钮打开弹出窗口,您可以在其中跟踪 Microsoft 的登录信息,但还有一个我可以在移动设备中使用的电子邮件和密码字段。
_loginWithMicrosoft() async {
try {
final provider = OAuthProvider("microsoft.com");
provider.setCustomParameters({
"tenant": "common",
"prompt": "select_account",
});
// MicrosoftAuthCredential mAuth = Credential();
debugPrint("Trying to pop-up microsoft login");
UserCredential cred;
if (kIsWeb) {
debugPrint("On the web");
FirebaseAuth.instance.signOut();
cred = await FirebaseAuth.instance.signInWithPopup(provider);
debugPrint(cred.user?.displayName);
debugPrint(cred.user?.email);
} else {
debugPrint("Not on web");
try {
// TODO implement normal sign in without microsoft
// given a username & password, send info and authenticate // firebase with microsoft login info. If possible w/ popup
// cred = await FirebaseAuth.instance.whateverMethodGetsLoginAtMicroSoft
} on FirebaseAuthException catch (e) {
errorMessage = "${e.code} - ${e.message}";
debugPrint(errorMessage);
}
}
} on FirebaseAuthException catch (e) {
errorMessage = "${e.code} - ${e.message}";
debugPrint(errorMessage);
}
}
import 'package:firebase_auth/firebase_auth.dart';
Future<UserCredential> signInWithMicrosoft() async {
final microsoftProvider = MicrosoftAuthProvider();
if (kIsWeb) {
await FirebaseAuth.instance.signInWithPopup(microsoftProvider);
} else {
await FirebaseAuth.instance.signInWithProvider(microsoftProvider);
}
}