-
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.
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import 'dart:developer'; | ||
|
||
import 'package:firebase_auth/firebase_auth.dart'; | ||
import 'package:google_sign_in/google_sign_in.dart'; | ||
|
||
enum SignInState { | ||
authenticated, | ||
disabled, | ||
notFound, | ||
} | ||
|
||
class AuthService { | ||
static final FirebaseAuth _auth = FirebaseAuth.instance; | ||
static final GoogleSignIn _googleSignIn = GoogleSignIn(); | ||
|
||
/// Returns currently signed up user credentials. | ||
static User? get getCurrentUser => _auth.currentUser; | ||
|
||
/// Returns an instance of `FirebaseAuth`. | ||
static FirebaseAuth? get getFirebaseAuthInstance => _auth; | ||
|
||
static Future<User?> logOut() async { | ||
try { | ||
await _googleSignIn.signOut(); | ||
await _auth.signOut(); | ||
} on FirebaseAuthException catch (e) { | ||
log(e.message!); | ||
} | ||
return getCurrentUser; | ||
} | ||
|
||
static Future<SignInState> signUp() async { | ||
try { | ||
GoogleSignInAccount? user = await _googleSignIn.signIn(); | ||
|
||
if (user != null) { | ||
GoogleSignInAuthentication auth = await user.authentication; | ||
|
||
AuthCredential credential = GoogleAuthProvider.credential( | ||
idToken: auth.idToken, | ||
accessToken: auth.accessToken, | ||
); | ||
|
||
await _auth.signInWithCredential(credential); | ||
} | ||
} on FirebaseAuthException catch (e) { | ||
switch (e.code) { | ||
case 'user-disabled': | ||
return SignInState.disabled; | ||
case 'user-not-found': | ||
return SignInState.notFound; | ||
} | ||
} | ||
return SignInState.authenticated; | ||
} | ||
} |