bloc
This commit is contained in:
@@ -0,0 +1 @@
|
||||
export 'my_user_entity.dart';
|
||||
@@ -0,0 +1,46 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class MyUserEntity extends Equatable {
|
||||
final String id;
|
||||
final String email;
|
||||
final String name;
|
||||
final String? picture;
|
||||
|
||||
const MyUserEntity({
|
||||
required this.id,
|
||||
required this.email,
|
||||
required this.name,
|
||||
this.picture,
|
||||
});
|
||||
|
||||
Map<String, Object?> toDocument() {
|
||||
return {
|
||||
'id': id,
|
||||
'email': email,
|
||||
'name': name,
|
||||
'picture': picture,
|
||||
};
|
||||
}
|
||||
|
||||
static MyUserEntity fromDocument(Map<String, dynamic> doc) {
|
||||
return MyUserEntity(
|
||||
id: doc['id'] as String,
|
||||
email: doc['email'] as String,
|
||||
name: doc['name'] as String,
|
||||
picture: doc['picture'] as String?,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [id, email, name, picture];
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return '''UserEntity {
|
||||
id: $id
|
||||
email: $email
|
||||
name: $name
|
||||
picture: $picture
|
||||
}''';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
import 'dart:developer';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:firebase_storage/firebase_storage.dart';
|
||||
import 'package:user_repository/src/models/my_user.dart';
|
||||
import 'entities/entities.dart';
|
||||
import 'user_repo.dart';
|
||||
|
||||
class FirebaseUserRepository implements UserRepository {
|
||||
FirebaseUserRepository({
|
||||
FirebaseAuth? firebaseAuth,
|
||||
}) : _firebaseAuth = firebaseAuth ?? FirebaseAuth.instance;
|
||||
|
||||
final FirebaseAuth _firebaseAuth;
|
||||
final usersCollection = FirebaseFirestore.instance.collection('users');
|
||||
|
||||
@override
|
||||
Stream<User?> get user {
|
||||
return _firebaseAuth.authStateChanges().map((firebaseUser) {
|
||||
final user = firebaseUser;
|
||||
return user;
|
||||
});
|
||||
}
|
||||
|
||||
// Sign up
|
||||
@override
|
||||
Future<MyUser> signUp(MyUser myUser, String password) async {
|
||||
try {
|
||||
UserCredential user = await _firebaseAuth.createUserWithEmailAndPassword(
|
||||
email: myUser.email,
|
||||
password: password,
|
||||
);
|
||||
|
||||
myUser = myUser.copyWith(
|
||||
id: user.user!.uid,
|
||||
);
|
||||
|
||||
return myUser;
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
// Sign in
|
||||
@override
|
||||
Future<void> signIn(String email, String password) async {
|
||||
try {
|
||||
await _firebaseAuth.signInWithEmailAndPassword(
|
||||
email: email,
|
||||
password: password,
|
||||
);
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
// Sign out
|
||||
@override
|
||||
Future<void> logOut() async {
|
||||
try {
|
||||
await _firebaseAuth.signOut();
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
// Reset password
|
||||
@override
|
||||
Future<void> resetPassword(String email) async {
|
||||
try {
|
||||
await _firebaseAuth.sendPasswordResetEmail(email: email);
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
// Set user data
|
||||
@override
|
||||
Future<void> setUserData(MyUser user) async {
|
||||
try {
|
||||
await usersCollection.doc(user.id).set(user.toEntity().toDocument());
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
// Get user data
|
||||
@override
|
||||
Future<MyUser> getMyUser(String myUserId) async {
|
||||
try {
|
||||
return usersCollection.doc(myUserId).get().then((value) =>
|
||||
MyUser.fromEntity(MyUserEntity.fromDocument(value.data()!)));
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateUserInfo(String userId, Map<String, dynamic> data) async {
|
||||
try {
|
||||
await usersCollection.doc(userId).update(data);
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> uploadPicture(String file, String userId) async {
|
||||
try {
|
||||
File imageFile = File(file);
|
||||
Reference firebaseStoreRef =
|
||||
FirebaseStorage.instance.ref().child('$userId/PP/${userId}_lead');
|
||||
await firebaseStoreRef.putFile(
|
||||
imageFile,
|
||||
);
|
||||
String url = await firebaseStoreRef.getDownloadURL();
|
||||
await usersCollection.doc(userId).update({'picture': url});
|
||||
return url;
|
||||
} catch (e) {
|
||||
log(e.toString());
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export 'my_user.dart';
|
||||
@@ -0,0 +1,70 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
import '../entities/entities.dart';
|
||||
|
||||
class MyUser extends Equatable {
|
||||
final String id;
|
||||
final String email;
|
||||
final String name;
|
||||
String? picture;
|
||||
|
||||
MyUser({
|
||||
required this.id,
|
||||
required this.email,
|
||||
required this.name,
|
||||
this.picture,
|
||||
});
|
||||
|
||||
/// Empty user which represents an unauthenticated user.
|
||||
static final empty = MyUser(id: '', email: '', name: '', picture: '');
|
||||
|
||||
/// Modify MyUser parameters
|
||||
MyUser copyWith({
|
||||
String? id,
|
||||
String? email,
|
||||
String? name,
|
||||
String? picture,
|
||||
}) {
|
||||
return MyUser(
|
||||
id: id ?? this.id,
|
||||
email: email ?? this.email,
|
||||
name: name ?? this.name,
|
||||
picture: picture ?? this.picture,
|
||||
);
|
||||
}
|
||||
|
||||
/// Convenience getter to determine whether the current user is empty.
|
||||
bool get isEmpty => this == MyUser.empty;
|
||||
|
||||
/// Convenience getter to determine whether the current user is not empty.
|
||||
bool get isNotEmpty => this != MyUser.empty;
|
||||
|
||||
MyUserEntity toEntity() {
|
||||
return MyUserEntity(
|
||||
id: id,
|
||||
email: email,
|
||||
name: name,
|
||||
picture: picture,
|
||||
);
|
||||
}
|
||||
|
||||
static MyUser fromEntity(MyUserEntity entity) {
|
||||
return MyUser(
|
||||
id: entity.id,
|
||||
email: entity.email,
|
||||
name: entity.name,
|
||||
picture: entity.picture,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [id, email, name, picture];
|
||||
}
|
||||
// final String name;
|
||||
// final String city;
|
||||
// final String? profession;
|
||||
// final String? state;
|
||||
// final Reference? photo;
|
||||
// final int? tarifa;
|
||||
// final String? phoneNumber;
|
||||
// final String? token;
|
||||
@@ -0,0 +1,23 @@
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
|
||||
import '../user_repository.dart';
|
||||
|
||||
abstract class UserRepository {
|
||||
Stream<User?> get user;
|
||||
|
||||
Future<void> signIn(String email, String password);
|
||||
|
||||
Future<void> logOut();
|
||||
|
||||
Future<MyUser> signUp(MyUser myUser, String password);
|
||||
|
||||
Future<void> resetPassword(String email);
|
||||
|
||||
Future<void> setUserData(MyUser user);
|
||||
|
||||
Future<MyUser> getMyUser(String myUserId);
|
||||
|
||||
Future<void> updateUserInfo(String userId, Map<String, dynamic> data);
|
||||
|
||||
Future<String> uploadPicture(String file, String userId);
|
||||
}
|
||||
Reference in New Issue
Block a user