mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-18 22:44:21 +01:00
Add AuthService Class for Google and GitHub Authentication
This commit introduces the AuthService class, which encapsulates the logic for signing in with Google and GitHub using Firebase Authentication. The class provides methods for initiating the OAuth flows for both providers and for signing out the user. - Implemented `signInWithGoogle()` to handle Google Sign-In. - Implemented `signInWithGitHub()` to handle GitHub Sign-In via popup. - Added `signOut()` method for logging out the user. - Added `getCurrentUser()` method to fetch the currently authenticated user.
This commit is contained in:
50
frontend/lib/services/auth_service.dart
Normal file
50
frontend/lib/services/auth_service.dart
Normal file
@@ -0,0 +1,50 @@
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:google_sign_in/google_sign_in.dart';
|
||||
|
||||
class AuthService {
|
||||
final FirebaseAuth _auth = FirebaseAuth.instance;
|
||||
final GoogleSignIn googleSignIn = GoogleSignIn(
|
||||
clientId:
|
||||
"387936576242-iejdacrjljds7hf99q0p6eqna8rju3sb.apps.googleusercontent.com");
|
||||
|
||||
// Sign in with Google
|
||||
Future<UserCredential?> signInWithGoogle() async {
|
||||
try {
|
||||
final GoogleSignInAccount? googleSignInAccount =
|
||||
await googleSignIn.signIn();
|
||||
if (googleSignInAccount != null) {
|
||||
final GoogleSignInAuthentication googleSignInAuthentication =
|
||||
await googleSignInAccount.authentication;
|
||||
final AuthCredential credential = GoogleAuthProvider.credential(
|
||||
accessToken: googleSignInAuthentication.accessToken,
|
||||
idToken: googleSignInAuthentication.idToken,
|
||||
);
|
||||
return await _auth.signInWithCredential(credential);
|
||||
}
|
||||
} catch (e) {
|
||||
print("Error during Google Sign-In: $e");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Sign in with GitHub
|
||||
Future<UserCredential?> signInWithGitHub() async {
|
||||
try {
|
||||
final GithubAuthProvider provider = GithubAuthProvider();
|
||||
return await _auth.signInWithPopup(provider);
|
||||
} catch (e) {
|
||||
print("Error during GitHub Sign-In: $e");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Sign out
|
||||
Future<void> signOut() async {
|
||||
await _auth.signOut();
|
||||
}
|
||||
|
||||
// Get current user
|
||||
User? getCurrentUser() {
|
||||
return _auth.currentUser;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user