Skip to content

Commit

Permalink
Merge pull request #7 from crunchloop/injectable
Browse files Browse the repository at this point in the history
Add dependency injection for auth
  • Loading branch information
pmaite88 authored Mar 8, 2023
2 parents 96c38ea + cb612ea commit 5b7a2fb
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 12 deletions.
6 changes: 5 additions & 1 deletion lib/components/app_drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';

import '../app_router.dart';
import '../data/repositories/auth_facade.dart';

// statefull app drawer
class AppDrawer extends StatefulWidget {
Expand All @@ -13,8 +14,11 @@ class AppDrawer extends StatefulWidget {
}

class _AppDrawerState extends State<AppDrawer> {
final _authFacade = AuthFacade();

void signOut(BuildContext context) async {
await Auth.signOut();
await _authFacade.signOut();

context.router.push(const LoginRoute());
}

Expand Down
20 changes: 20 additions & 0 deletions lib/data/models/user.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:equatable/equatable.dart';
import 'package:json_annotation/json_annotation.dart';

part 'user.g.dart';

@JsonSerializable()
class User extends Equatable {
final String id;
final String? email;
final String? username;

const User({required this.id, required this.email, required this.username});

@override
List<Object?> get props => [id, email, username];

factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);

Map<String, dynamic> toJson() => _$UserToJson(this);
}
19 changes: 19 additions & 0 deletions lib/data/models/user.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions lib/data/repositories/auth_facade.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:auth/auth.dart';
import 'package:injectable/injectable.dart';

import '../models/user.dart';

@Injectable()
class AuthFacade {
Future<void> signIn({
required String email,
required String password,
}) {
return Auth.signInWithEmailAndPassword(email: email, password: password);
}

Future<void> register(
{required String username,
required String email,
required String password}) {
return Auth.createUserWithEmailAndPassword(
email: email, password: password);
}

User? getUser() {
final user = Auth.currentUser;

return user == null
? null
: User(id: user.uid, email: user.email, username: user.displayName);
}

Future<void> signOut() {
return Auth.signOut();
}
}
19 changes: 17 additions & 2 deletions lib/injection.config.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion lib/injection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:get_it/get_it.dart';
import 'package:injectable/injectable.dart';

import 'injection.config.dart';
import 'services/firebase_auth.dart';

final getIt = GetIt.instance;

Expand All @@ -10,4 +11,10 @@ final getIt = GetIt.instance;
preferRelativeImports: true,
asExtension: false,
)
void configureDependencies() => $initGetIt(getIt);
Future<void> configureDependencies() async => await $initGetIt(getIt);

@module
abstract class AppModule {
@preResolve
Future<FirebaseAuthService> get authService => FirebaseAuthService.init();
}
8 changes: 4 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import 'package:flutter/material.dart';

import 'package:auth/auth.dart';
import 'package:flutter_bootstrap/data/repositories/auth_facade.dart';

import 'injection.dart';
import 'app_router.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();

configureDependencies();
await Auth.initialize();
await configureDependencies();

// TODO: use a guard to redirect to home if logged in
await Auth.signOut();
final authFacade = AuthFacade();
await authFacade.signOut();

runApp(MyApp());
}
Expand Down
10 changes: 7 additions & 3 deletions lib/pages/login.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// import 'dart:convert';

import 'package:auth/auth.dart';
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bootstrap/data/repositories/auth_facade.dart';
import 'package:injectable/injectable.dart';

import '../app_router.dart';

@Injectable()
class LoginPage extends StatefulWidget {
const LoginPage({Key? key}) : super(key: key);

Expand All @@ -15,16 +17,18 @@ class LoginPage extends StatefulWidget {

// login form in dart
class _LoginState extends State<LoginPage> {
final _authFacade = AuthFacade();

final _formKey = GlobalKey<FormState>();
final _emailController = TextEditingController();
final _passwordController = TextEditingController();

// async function to login
Future<void> login(BuildContext context) async {
await Auth.signInWithEmailAndPassword(
await _authFacade.signIn(
email: _emailController.text, password: _passwordController.text);

if (Auth.currentUser != null) {
if (_authFacade.getUser() != null) {
context.router.push(const HomeRoute());
} else {
ScaffoldMessenger.of(context).showSnackBar(
Expand Down
9 changes: 9 additions & 0 deletions lib/services/firebase_auth.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:auth/auth.dart';

class FirebaseAuthService {
static Future<FirebaseAuthService> init() async {
await Auth.initialize();

return FirebaseAuthService();
}
}
10 changes: 9 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.2.4"
equatable:
dependency: "direct main"
description:
name: equatable
sha256: c2b87cb7756efdf69892005af546c56c0b5037f54d2a88269b4f347a505e3ca2
url: "https://pub.dev"
source: hosted
version: "2.0.5"
fake_async:
dependency: transitive
description:
Expand Down Expand Up @@ -408,7 +416,7 @@ packages:
source: hosted
version: "0.6.5"
json_annotation:
dependency: transitive
dependency: "direct main"
description:
name: json_annotation
sha256: c33da08e136c3df0190bd5bbe51ae1df4a7d96e7954d1d7249fea2968a72d317
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ dependencies:
sdk: flutter

auto_route: ^5.0.4
equatable: ^2.0.5
freezed_annotation: ^2.2.0
get_it: ^7.2.0
json_annotation: ^4.8.0
injectable: ^2.1.0
logger: ^1.2.2

Expand Down

0 comments on commit 5b7a2fb

Please sign in to comment.