implementFirebase static method
Implementation
static Future<void> implementFirebase(String projectName) async {
// Import firebase core and firebase auth
final ProcessResult addFirebaseCore =
await Process.run('dart', ['pub', 'add', 'firebase_core']);
final ProcessResult addFirebaseAuth =
await Process.run('dart', ['pub', 'add', 'firebase_auth']);
final ProcessResult addGoogleSignIn =
await Process.run('dart', ['pub', 'add', 'google_sign_in']);
if (addFirebaseCore.exitCode != 0) {
print(
'Error adding packages. Check your internet connection and try again.');
print(addFirebaseCore.stderr);
return;
}
if (addFirebaseAuth.exitCode != 0) {
print(
'Error adding packages. Check your internet connection and try again.');
print(addFirebaseAuth.stderr);
return;
}
if (addGoogleSignIn.exitCode != 0) {
print(
'Error adding packages. Check your internet connection and try again.');
print(addGoogleSignIn.stderr);
return;
}
print('added firebase packages');
// Create a login page
// create lib/features/auth/ui
final authFolder = Directory('lib/features/auth');
authFolder.createSync();
final authUiFolder = Directory('lib/features/auth/ui');
authUiFolder.createSync();
// Create login page inside lib/features/auth/ui/login_screen.dart
final File loginScreen = File('lib/features/auth/ui/auth_screen.dart');
if (!loginScreen.existsSync()) {
loginScreen.createSync();
loginScreen.writeAsStringSync('''
import 'package:flutter/material.dart';
import 'package:$projectName/features/auth/service/auth_service.dart';
import 'package:$projectName/shared/custom_button.dart';
class AuthScreen extends StatefulWidget {
const AuthScreen({super.key});
@override
State<AuthScreen> createState() => _AuthScreenState();
}
class _AuthScreenState extends State<AuthScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: CustomButton(
text: 'Continue with google',
function: () {
AuthService().continueWithGoogle(context);
}),
);
}
}
''');
}
final authServiceFolder = Directory('lib/features/auth/service');
authServiceFolder.createSync();
// create auth service inside lib/features/auth/service/auth_service.dart
final File authService =
File('lib/features/auth/service/auth_service.dart');
if (!authService.existsSync()) {
authService.createSync();
authService.writeAsStringSync('''
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:$projectName/shared/nav_bar.dart';
class AuthService {
final FirebaseAuth _auth = FirebaseAuth.instance;
continueWithGoogle(BuildContext context) async {
final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
if (googleUser != null) {
final GoogleSignInAuthentication googleAuth =
await googleUser.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
// Once signed in navigate to the home screen
final UserCredential userCredential =
await _auth.signInWithCredential(credential);
final User? user = userCredential.user;
if (user != null) {
// ignore: use_build_context_synchronously
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (context) => const NavigationScreen()));
}
return user;
} else {
// handle signin errors
return null;
}
}
}
''');
}
}