Skip to content

Commit

Permalink
add user on fireStore database
Browse files Browse the repository at this point in the history
  • Loading branch information
usamaaelgendy committed Dec 17, 2020
1 parent d29caad commit 1d42b14
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 4 deletions.
13 changes: 13 additions & 0 deletions lib/core/service/firestore_user.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:ecommerce_app/model/user_model.dart';

class FireStoreUser {
final CollectionReference _userCollectionRef =
FirebaseFirestore.instance.collection('Users');

Future<void> addUserToFireStore(UserModel userModel) async {
return await _userCollectionRef
.doc(userModel.userId)
.set(userModel.toJson());
}
}
27 changes: 23 additions & 4 deletions lib/core/view_model/auth_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:ecommerce_app/core/service/firestore_user.dart';
import 'package:ecommerce_app/model/user_model.dart';
import 'package:ecommerce_app/view/home_view.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -46,7 +48,10 @@ class AuthViewModel extends GetxController {
accessToken: googleSignInAuthentication.accessToken,
);

await _auth.signInWithCredential(credential);
await _auth.signInWithCredential(credential).then((user) {
saveUser(user);
Get.offAll(HomeView());
});
}

void facebookSignInMethod() async {
Expand All @@ -57,7 +62,9 @@ class AuthViewModel extends GetxController {
if (result.status == FacebookLoginStatus.loggedIn) {
final faceCredential = FacebookAuthProvider.credential(accessToken);

await _auth.signInWithCredential(faceCredential);
await _auth.signInWithCredential(faceCredential).then((user) async {
saveUser(user);
});
}
}

Expand All @@ -78,8 +85,11 @@ class AuthViewModel extends GetxController {

void createAccountWithEmailAndPassword() async {
try {
await _auth.createUserWithEmailAndPassword(
email: email, password: password);
await _auth
.createUserWithEmailAndPassword(email: email, password: password)
.then((user) async {
saveUser(user);
});

Get.offAll(HomeView());
} catch (e) {
Expand All @@ -92,4 +102,13 @@ class AuthViewModel extends GetxController {
);
}
}

void saveUser(UserCredential user) async {
await FireStoreUser().addUserToFireStore(UserModel(
userId: user.user.uid,
email: user.user.email,
name: name == null ? user.user.displayName : name,
pic: '',
));
}
}
25 changes: 25 additions & 0 deletions lib/model/user_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class UserModel {
String userId, email, name, pic;

UserModel({this.userId, this.email, this.name, this.pic});

UserModel.fromJson(Map<dynamic, dynamic> map) {
if (map == null) {
return;
}

userId = map['userId'];
email = map['email'];
name = map['name'];
pic = map['pic'];
}

toJson() {
return {
'userId': userId,
'email': email,
'name': name,
'pic': pic,
};
}
}
21 changes: 21 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0-nullsafety.3"
cloud_firestore:
dependency: "direct main"
description:
name: cloud_firestore
url: "https://pub.dartlang.org"
source: hosted
version: "0.14.4"
cloud_firestore_platform_interface:
dependency: transitive
description:
name: cloud_firestore_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.1"
cloud_firestore_web:
dependency: transitive
description:
name: cloud_firestore_web
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.1+2"
collection:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies:
firebase_core: "0.5.3"
firebase_auth: "^0.18.4"
flutter_facebook_login: ^3.0.0
cloud_firestore: "^0.14.4"

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit 1d42b14

Please sign in to comment.