Skip to content

Commit

Permalink
feat: add firebase auth services
Browse files Browse the repository at this point in the history
  • Loading branch information
mediocre9 committed Jul 4, 2023
1 parent a8af849 commit 58c5eca
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions lib/services/auth_service.dart
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;
}
}

0 comments on commit 58c5eca

Please sign in to comment.