-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrentUser.dart
86 lines (68 loc) · 2.64 KB
/
currentUser.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import 'package:flutter/cupertino.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart' show Firebase;
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:digibook2/models/user.dart';
import 'package:digibook2/services/database.dart';
class CurrentUser extends ChangeNotifier{ //verifica provider
OurUser _currentUser=OurUser();
OurUser get getCurrentUser=>_currentUser;
Future<String> signUpUser(String email, String password, String fullName) async{
String retVal="error";
OurUser _user=OurUser();
try{
//AuthResult _authResult=await _auth.createUserWithEmailAndPassword(email: email, password: password);
final _authResult=(await FirebaseAuth.instance.createUserWithEmailAndPassword(email: email, password: password)).user;
_user.uid=_authResult.uid;
_user.email=_authResult.email;
_user.fullName=fullName;
String returnString= await OurDatabase().createUser(_user);
if(returnString=="success") {
retVal = "success";
}
}catch(e) {
retVal=e.message;
}
return retVal;
}
Future<String> logInUserWithEmail(String email, String password) async{
String retVal="error";
try{
final _authResult=(await FirebaseAuth.instance.signInWithEmailAndPassword(email: email, password: password)).user;
_currentUser=await OurDatabase().getUserInfo(_authResult.uid);
if(_currentUser!=null){
retVal="success";
}
}catch(e) {
retVal=e.message;
}
return retVal;
}
Future<String> logInUserWithGoogle() async{
String retVal="error";
OurUser _user=OurUser();
try{
final GoogleSignInAccount _googleUser=await GoogleSignIn().signIn();
final GoogleSignInAuthentication _googleAuth=await _googleUser.authentication;
final GoogleAuthCredential credential=GoogleAuthProvider.credential(
accessToken: _googleAuth.accessToken,
idToken: _googleAuth.idToken,
);
final _authResult=(await FirebaseAuth.instance.signInWithCredential(credential));
if(_authResult.additionalUserInfo.isNewUser){
_user.uid=_authResult.user.uid;
_user.email=_authResult.user.email;
_user.fullName=_authResult.user.displayName;
OurDatabase().createUser(_user);
}
_currentUser=await OurDatabase().getUserInfo(_authResult.user.uid);
if(_currentUser!=null){
retVal="success";
}
}catch(e) {
retVal=e.message;
}
return retVal;
}
}