-
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.
feat: add login and sign up poc (#2)
- Loading branch information
Showing
30 changed files
with
1,125 additions
and
85 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,32 @@ | ||
# Description | ||
|
||
Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change. | ||
|
||
Fixes # (issue) | ||
|
||
## Type of change | ||
|
||
Please delete options that are not relevant. | ||
|
||
- [ ] Bug fix (non-breaking change which fixes an issue) | ||
- [ ] New feature (non-breaking change which adds functionality) | ||
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) | ||
- [ ] This change requires a documentation update | ||
|
||
# How Has This Been Tested? | ||
|
||
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration | ||
|
||
- [ ] Manual Testing | ||
- [ ] Test B | ||
|
||
# Checklist: | ||
|
||
- [ ] My code follows the style guidelines of this project | ||
- [ ] I have performed a self-review of my code | ||
- [ ] I have commented my code, particularly in hard-to-understand areas | ||
- [ ] I have made corresponding changes to the documentation | ||
- [ ] My changes generate no new warnings | ||
- [ ] I have added tests that prove my fix is effective or that my feature works | ||
- [ ] New and existing unit tests pass locally with my changes | ||
- [ ] Any dependent changes have been merged and published in downstream modules |
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
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
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
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,32 @@ | ||
import 'package:logger/logger.dart'; | ||
|
||
var logger = Logger( | ||
printer: PrettyPrinter( | ||
methodCount: 2, // Number of method calls to be displayed | ||
errorMethodCount: 8, // Number of method calls if stacktrace is provided | ||
lineLength: 120, // Width of the output | ||
colors: true, // Colorful log messages | ||
printEmojis: true, // Print an emoji for each log message | ||
printTime: false // Should each log print contain a timestamp | ||
), | ||
); | ||
|
||
void logWarningOnly() { | ||
Logger.level = Level.warning; | ||
} | ||
|
||
/* | ||
Samples | ||
logger.t("Trace log"); | ||
logger.d("Debug log"); | ||
logger.i("Info log"); | ||
logger.w("Warning log"); | ||
logger.e("Error log", error: 'Test Error'); | ||
logger.f("What a fatal log", error: error, stackTrace: stackTrace); | ||
*/ |
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,12 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
// TODO: can be improved or just use a package for snackbars | ||
|
||
SnackBar createSnackbar(String message) { | ||
return SnackBar( | ||
content: Text( | ||
message, | ||
textAlign: TextAlign.center, | ||
), | ||
); | ||
} |
38 changes: 38 additions & 0 deletions
38
lib/features/authentication/data/datasources/user_remote_datasource.dart
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,38 @@ | ||
import 'package:firebase_auth/firebase_auth.dart'; | ||
|
||
import 'package:u_do_note/features/authentication/data/models/user_model.dart'; | ||
import 'package:u_do_note/core/logger/logger.dart'; | ||
|
||
class UserRemoteDataSource { | ||
final FirebaseAuth _auth; | ||
|
||
UserRemoteDataSource(this._auth); | ||
|
||
Future<UserModel> signInWithEmailAndPassword( | ||
String email, String password) async { | ||
final userCredential = await _auth.signInWithEmailAndPassword( | ||
email: email, | ||
password: password, | ||
); | ||
|
||
logger.i( | ||
"Signing in with email and password: \n email: $email \n password: $password"); | ||
|
||
return UserModel.fromFirebaseUser(userCredential.user); | ||
} | ||
|
||
Future<UserModel> signUpWithEmailAndPassword( | ||
String email, String password) async { | ||
final userCredential = await _auth.createUserWithEmailAndPassword( | ||
email: email, | ||
password: password, | ||
); | ||
|
||
// TODO: create user document with the created user's id. | ||
|
||
logger.i( | ||
"Signing up with email and password: \n email: $email \n password: $password"); | ||
|
||
return UserModel.fromFirebaseUser(userCredential.user); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,40 +1,40 @@ | ||
import 'package:firebase_auth/firebase_auth.dart'; | ||
import 'package:u_do_note/features/authentication/domain/entities/user.dart'; | ||
|
||
typedef FirebaseUser = User; | ||
|
||
class UserModel { | ||
final String id; | ||
final String email; | ||
final String name; | ||
final String photoUrl; | ||
final String uid; | ||
|
||
UserModel({ | ||
required this.id, | ||
required this.email, | ||
required this.name, | ||
required this.photoUrl, | ||
required this.uid, | ||
}); | ||
|
||
// entity to model | ||
factory UserModel.fromEntity(User user) => UserModel( | ||
id: user.id, | ||
email: user.email, | ||
name: user.name, | ||
photoUrl: user.photoUrl, | ||
uid: user.uid); | ||
factory UserModel.fromEntity(UserEntity user) => UserModel( | ||
id: user.id, | ||
email: user.email, | ||
name: user.name, | ||
photoUrl: user.photoUrl, | ||
); | ||
|
||
// model to entity | ||
User toEntity() => | ||
User(id: id, email: email, name: name, photoUrl: photoUrl, uid: uid); | ||
UserEntity toEntity() => | ||
UserEntity(id: id, email: email, name: name, photoUrl: photoUrl); | ||
|
||
// from firestore to model | ||
factory UserModel.fromSnapshot(Map<String, dynamic> snapshot) { | ||
factory UserModel.fromFirebaseUser(FirebaseUser? firebaseUser) { | ||
return UserModel( | ||
id: snapshot['id'], | ||
email: snapshot['email'], | ||
name: snapshot['name'], | ||
photoUrl: snapshot['photoUrl'], | ||
uid: snapshot['uid'], | ||
id: firebaseUser!.uid, | ||
email: firebaseUser.email!, | ||
name: firebaseUser.displayName ?? '', | ||
photoUrl: firebaseUser.photoURL ?? '', | ||
); | ||
} | ||
} |
52 changes: 24 additions & 28 deletions
52
lib/features/authentication/data/repositories/user_repository_impl.dart
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 |
---|---|---|
@@ -1,42 +1,38 @@ | ||
import 'package:dartz/dartz.dart'; | ||
import 'package:firebase_auth/firebase_auth.dart'; | ||
|
||
import 'package:u_do_note/core/error/failures.dart'; | ||
import 'package:u_do_note/features/authentication/domain/entities/user.dart'; | ||
import 'package:u_do_note/features/authentication/data/datasources/user_remote_datasource.dart'; | ||
import 'package:u_do_note/features/authentication/data/models/user_model.dart'; | ||
import 'package:u_do_note/features/authentication/domain/repositories/user_repository.dart'; | ||
|
||
class UserRepositoryImpl implements UserRepository { | ||
|
||
// Data sources | ||
final UserRemoteDataSource userRemoteDataSource; | ||
|
||
// 32:10 youtube | ||
@override | ||
Future<Either<Failure, User>> signInWithEmailAndPassword({required String email, required String password}) { | ||
// TODO: implement signInWithEmailAndPassword | ||
throw UnimplementedError(); | ||
} | ||
|
||
@override | ||
Future<Either<Failure, User>> signInWithFacebook() { | ||
// TODO: implement signInWithFacebook | ||
throw UnimplementedError(); | ||
} | ||
|
||
@override | ||
Future<Either<Failure, User>> signInWithGoogle() { | ||
// TODO: implement signInWithGoogle | ||
throw UnimplementedError(); | ||
} | ||
UserRepositoryImpl(this.userRemoteDataSource); | ||
|
||
@override | ||
Future<Either<Failure, User>> signOut() { | ||
// TODO: implement signOut | ||
throw UnimplementedError(); | ||
Future<Either<Failure, UserModel>> signUpWithEmailAndPassword( | ||
{required String email, required String password}) async { | ||
try { | ||
final userModel = await userRemoteDataSource.signUpWithEmailAndPassword( | ||
email, password); | ||
return Right(userModel); | ||
} on FirebaseAuthException catch (e) { | ||
return Left(AuthenticationException(code: e.code, message: e.message!)); | ||
} | ||
} | ||
|
||
@override | ||
Future<Either<Failure, User>> signUpWithEmailAndPassword({required String email, required String password}) { | ||
// TODO: implement signUpWithEmailAndPassword | ||
throw UnimplementedError(); | ||
Future<Either<Failure, UserModel>> signInWithEmailAndPassword( | ||
{required String email, required String password}) async { | ||
try { | ||
final userModel = await userRemoteDataSource.signInWithEmailAndPassword( | ||
email, password); | ||
return Right(userModel); | ||
} on FirebaseAuthException catch (e) { | ||
return Left(AuthenticationException(code: e.code, message: e.message!)); | ||
} | ||
} | ||
|
||
} | ||
} |
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 |
---|---|---|
@@ -1,15 +1,13 @@ | ||
class User { | ||
class UserEntity { | ||
final String id; | ||
final String email; | ||
final String name; | ||
final String photoUrl; | ||
final String uid; | ||
|
||
User({ | ||
UserEntity({ | ||
required this.id, | ||
required this.email, | ||
required this.name, | ||
required this.photoUrl, | ||
required this.uid, | ||
}); | ||
} |
10 changes: 3 additions & 7 deletions
10
lib/features/authentication/domain/repositories/user_repository.dart
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 |
---|---|---|
@@ -1,14 +1,10 @@ | ||
import 'package:dartz/dartz.dart'; | ||
import 'package:u_do_note/core/error/failures.dart'; | ||
import 'package:u_do_note/features/authentication/domain/entities/user.dart'; | ||
import 'package:u_do_note/features/authentication/data/models/user_model.dart'; | ||
|
||
abstract class UserRepository { | ||
Future<Either<Failure, User>> signInWithEmailAndPassword( | ||
Future<Either<Failure, UserModel>> signInWithEmailAndPassword( | ||
{required String email, required String password}); | ||
Future<Either<Failure, User>> signUpWithEmailAndPassword( | ||
Future<Either<Failure, UserModel>> signUpWithEmailAndPassword( | ||
{required String email, required String password}); | ||
Future<Either<Failure, User>> signInWithGoogle(); | ||
Future<Either<Failure, User>> signInWithFacebook(); | ||
Future<Either<Failure, User>> signOut(); | ||
// Future<Either<Failure, User>> getCurrentUser(); | ||
} |
16 changes: 16 additions & 0 deletions
16
lib/features/authentication/domain/usecases/sign_in_with_email_and_password.dart
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,16 @@ | ||
import 'package:dartz/dartz.dart'; | ||
|
||
import 'package:u_do_note/core/error/failures.dart'; | ||
import 'package:u_do_note/features/authentication/data/models/user_model.dart'; | ||
import 'package:u_do_note/features/authentication/domain/repositories/user_repository.dart'; | ||
|
||
class SignInWithEmailAndPassword { | ||
final UserRepository _userRepository; | ||
|
||
SignInWithEmailAndPassword(this._userRepository); | ||
|
||
Future<Either<Failure, UserModel>> call(String email, String password) { | ||
return _userRepository.signInWithEmailAndPassword( | ||
email: email, password: password); | ||
} | ||
} |
Oops, something went wrong.