-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make call and update UI if a user is not whitelisted with the respect…
…ive server
- Loading branch information
1 parent
0767fcc
commit ebda489
Showing
4 changed files
with
99 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 61 additions & 48 deletions
109
frontend/memoree_client/lib/app/services/firebase_auth.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,62 @@ | ||
import 'package:firebase_auth/firebase_auth.dart' as auth; | ||
import 'package:google_sign_in/google_sign_in.dart'; | ||
import 'package:memoree_client/app/models/user.dart'; | ||
|
||
class FirebaseAuthService { | ||
final auth.FirebaseAuth _firebaseAuth; | ||
final GoogleSignIn _googleSignIn; | ||
|
||
FirebaseAuthService({auth.FirebaseAuth firebaseAuth, GoogleSignIn googleSignin}) | ||
: _firebaseAuth = firebaseAuth ?? auth.FirebaseAuth.instance, | ||
_googleSignIn = googleSignin ?? GoogleSignIn(); | ||
|
||
User _userFromFirebase(auth.User user) { | ||
if (user == null) { | ||
return null; | ||
} | ||
return User( | ||
uid: user.uid, | ||
email: user.email, | ||
displayName: user.displayName, | ||
photoUrl: user.photoURL, | ||
); | ||
} | ||
|
||
Stream<User> get onAuthStateChanged { | ||
return _firebaseAuth.authStateChanges().map(_userFromFirebase); | ||
} | ||
|
||
Future<User> signInWithGoogle() async { | ||
final googleUser = await _googleSignIn.signIn(); | ||
final googleAuth = await googleUser.authentication; | ||
final credential = auth.GoogleAuthProvider.credential( | ||
accessToken: googleAuth.accessToken, | ||
idToken: googleAuth.idToken, | ||
); | ||
final authResult = await _firebaseAuth.signInWithCredential(credential); | ||
return _userFromFirebase(authResult.user); | ||
} | ||
|
||
Future<void> signOut() async { | ||
await _googleSignIn.disconnect(); | ||
return _firebaseAuth.signOut(); | ||
} | ||
|
||
Future<User> currentUser() async { | ||
final user = _firebaseAuth.currentUser; | ||
return _userFromFirebase(user); | ||
} | ||
import 'package:firebase_auth/firebase_auth.dart' as auth; | ||
import 'package:google_sign_in/google_sign_in.dart'; | ||
import 'package:memoree_client/app/models/constants.dart'; | ||
import 'package:memoree_client/app/models/user.dart'; | ||
import 'package:memoree_client/app/services/search.dart'; | ||
|
||
class FirebaseAuthService { | ||
final auth.FirebaseAuth _firebaseAuth; | ||
final GoogleSignIn _googleSignIn; | ||
|
||
FirebaseAuthService({auth.FirebaseAuth firebaseAuth, GoogleSignIn googleSignin}) | ||
: _firebaseAuth = firebaseAuth ?? auth.FirebaseAuth.instance, | ||
_googleSignIn = googleSignin ?? GoogleSignIn(); | ||
|
||
User _userFromFirebase(auth.User user) { | ||
if (user == null) { | ||
return null; | ||
} | ||
return User( | ||
uid: user.uid, | ||
email: user.email, | ||
displayName: user.displayName, | ||
photoUrl: user.photoURL, | ||
); | ||
} | ||
|
||
Stream<User> get onAuthStateChanged { | ||
return _firebaseAuth.authStateChanges().map(_userFromFirebase); | ||
} | ||
|
||
Future<User> signInWithGoogle() async { | ||
final googleUser = await _googleSignIn.signIn(); | ||
final googleAuth = await googleUser.authentication; | ||
final credential = auth.GoogleAuthProvider.credential( | ||
accessToken: googleAuth.accessToken, | ||
idToken: googleAuth.idToken, | ||
); | ||
final authResult = await _firebaseAuth.signInWithCredential(credential); | ||
User signedIn = _userFromFirebase(authResult.user); | ||
|
||
// Check if user is allowed to access the server | ||
final isAllowed = await SearchService.isWhitelisted(signedIn.email); | ||
if(!isAllowed) | ||
{ | ||
await _googleSignIn.disconnect(); | ||
await _firebaseAuth.signOut(); | ||
throw(signedIn.email + PageErrors.no_access); | ||
} | ||
|
||
return signedIn; | ||
} | ||
|
||
Future<void> signOut() async { | ||
await _googleSignIn.disconnect(); | ||
return _firebaseAuth.signOut(); | ||
} | ||
|
||
Future<User> currentUser() async { | ||
final user = _firebaseAuth.currentUser; | ||
return _userFromFirebase(user); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters