Skip to content

Commit

Permalink
refactor: login with microsoft
Browse files Browse the repository at this point in the history
  • Loading branch information
kenguyenduc committed May 26, 2022
1 parent 11a1297 commit 68d72da
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 291 deletions.
124 changes: 32 additions & 92 deletions lib/core/firebase_login.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import 'dart:async';

// import 'package:apple_sign_in/apple_sign_in.dart';
import 'package:demo_login_office/utils/app_logger.dart';
import 'package:firebase_auth/firebase_auth.dart';

// import 'package:flutter_facebook_auth/flutter_facebook_auth.dart';
import 'package:firebase_auth_oauth/firebase_auth_oauth.dart';
import 'package:flutter/services.dart';
import 'package:google_sign_in/google_sign_in.dart';

class AuthCanceled implements Exception {
Expand All @@ -27,10 +25,6 @@ class FirebaseLogin {
FirebaseAuth get auth => _auth;
final GoogleSignIn _googleSignIn = GoogleSignIn();

// final FacebookAuth _facebookAuth = FacebookAuth.instance;

// Future<bool> isAppleSignInAvailable() => AppleSignIn.isAvailable();

Future<User?> signInGoogle() async {
await logout();
final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
Expand All @@ -47,99 +41,45 @@ class FirebaseLogin {
return user;
}

// Future<User> signInApple() async {
// final AuthorizationResult loginResult = await AppleSignIn.performRequests([
// const AppleIdRequest(requestedScopes: [Scope.email, Scope.fullName])
// ]);
// switch (loginResult.status) {
// case AuthorizationStatus.authorized:
// // here we're going to sign in the user within firebase
// break;
// case AuthorizationStatus.error:
// // do something
// throw loginResult.error;
//
// case AuthorizationStatus.cancelled:
// throw AuthCanceled();
// }
//
// final AppleIdCredential appleIdCredential = loginResult.credential;
//
// final AuthCredential credential = OAuthProvider('apple.com').credential(
// accessToken: String.fromCharCodes(appleIdCredential.authorizationCode),
// idToken: String.fromCharCodes(appleIdCredential.identityToken),
// );
//
// final UserCredential result = await _auth.signInWithCredential(credential);
// final User user = result.user;
// logger.d('Signed in with apple ${user.uid}');
// return user;
// }

// Future<User> signInFacebook() async {
// await logout();
// final LoginResult result = await _facebookAuth.login(loginBehavior: LoginBehavior.WEB_VIEW_ONLY);
// final String accessToken = result?.accessToken?.token;
// if (accessToken == null) {
// return null;
// }
// final AuthCredential facebookAuthCred = FacebookAuthProvider.credential(accessToken);
// final User user = (await _auth.signInWithCredential(facebookAuthCred)).user;
// return user;
// }
Future<User?> signInMicrosoft() async {
const String tenantId = 'cd61f176-1d9b-44fb-94a3-563affcaf249';
String provider = "microsoft.com";
List<String> scopes = [
"openid",
"email",
"offline_access",
"profile",
"User.ReadBasic.All",
"User.Read",
// "User.Read.All",
// "Directory.Read.All",
// "Directory.AccessAsUser.All"
];
Map<String, String> parameters = {'tenant': tenantId, "locale": "en"};
try {
await logout();
// User? user = FirebaseAuth.instance.currentUser;
User? user = await FirebaseAuthOAuth(app: _auth.app).openSignInFlow(
provider,
scopes,
parameters,
);
logger.d('--------Success------------/n$user');
return user;
} on PlatformException catch (error) {
logger.e("${error.code}: ${error.message}");
}
return null;
}

Future<void> logout() async {
try {
if (await _googleSignIn.isSignedIn()) {
await _googleSignIn.disconnect();
}
// if (await _facebookAuth.accessToken != null) {
// await _facebookAuth.logOut();
// }
await _auth.signOut();
} catch (e) {
logger.e(e);
}
}
}

// class UserSocialData {
// UserSocialData._({this.name, this.email, this.photoUrl});
//
// factory UserSocialData.fromApple(AuthorizationResult result, User user) {
// // final AppleIdCredential appleIdCredential = result.credential;
// // final PersonNameComponents nameComponents = appleIdCredential.fullName;
// final String name = <String>[
// nameComponents.namePrefix,
// nameComponents.givenName,
// nameComponents.middleName,
// nameComponents.familyName,
// nameComponents.nameSuffix,
// ].where((String element) => element != null).join(' ') ??
// 'Apple ID';
//
// final String email = appleIdCredential.email ?? user?.providerData?.first?.email;
// return UserSocialData._(name: name, email: email, photoUrl: null);
// }
//
// factory UserSocialData.fromGoogle(GoogleSignInAccount account) {
// return UserSocialData._(
// name: account.displayName,
// email: account.email,
// photoUrl: account.photoUrl,
// );
// }
//
// factory UserSocialData.fromFacebook(UserCredential result) {
// final Map<String, dynamic> profile = result.additionalUserInfo.profile;
// final String name = profile['name'] as String ?? result.user.displayName;
// final String email = profile['email'] as String ?? result.user.email;
// final String pictureData = (profile['picture'] as Map<dynamic, dynamic>)['data'] as String;
// final String photoUrl = (pictureData as Map<dynamic, dynamic>)['url'] as String;
// return UserSocialData._(name: name, email: email, photoUrl: photoUrl);
// }
//
// final String name;
// final String email;
// final String photoUrl;
// }
14 changes: 0 additions & 14 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,7 @@ class MyApp extends StatelessWidget {
theme: ThemeData(
primarySwatch: Colors.blue,
),
// home: const TestPage(),
home: const LoginPage(),
);
}
}

class TestPage extends StatelessWidget {
const TestPage({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
color: Colors.red,
),
);
}
}
44 changes: 25 additions & 19 deletions lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
const HomePage({
Key? key,
this.user,
}) : super(key: key);

final User? user;

@override
State<HomePage> createState() => _HomePageState();
Expand All @@ -17,30 +22,31 @@ class _HomePageState extends State<HomePage> {
appBar: AppBar(
title: const Text('Home page'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(
width: double.infinity,
child: ElevatedButton(
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
widget.user?.toString() ?? '',
style: const TextStyle(
color: Colors.green,
fontSize: 16,
),
),
const SizedBox(height: 16),
CustomButton(
title: 'LOGOUT',
onPressed: () async {
await FirebaseAuth.instance.signOut ().then((value) {
await FirebaseLogin.I.logout().then((value) {
Navigator.pop(context);
return null;
});
},
child: const Text('LOGOUT'),
),
),
const SizedBox(height: 16),
CustomButton(
title: 'Logout google',
onPressed: () {
FirebaseLogin.I.logout();
},
),
],
],
),
),
),
);
Expand Down
Loading

0 comments on commit 68d72da

Please sign in to comment.