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
87
88
89
90
91
92
93
94
95
|
import 'dart:io';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/foundation.dart';
import 'package:linkchat/firebase/database.dart';
import '../models/user.dart';
import 'storage.dart';
class Auth {
final FirebaseAuth _auth = FirebaseAuth.instance;
final GithubAuthProvider _githubProvider = GithubAuthProvider();
final GoogleAuthProvider _googleAuthProvider = GoogleAuthProvider();
final Database _db = Database();
User? get currentUser => _auth.currentUser;
Future<bool> createUserWithEmailAndPassword({
required String email,
required String password,
required String displayName,
required File avatar,
}) async {
try {
UserCredential cred = await _auth.createUserWithEmailAndPassword(
email: email,
password: password,
);
User? user = cred.user;
if (user != null) {
String photoUrl = await Storage().uploadAvatar(user.uid, avatar);
await user.updateDisplayName(displayName);
await user.updatePhotoURL(photoUrl);
// Store user in database
_db.saveUser(FsUser(
uid: user.uid,
displayName: displayName,
photoUrl: photoUrl,
email: user.email!,
));
}
return true;
} catch (e) {
if (kDebugMode) print(e);
return false;
}
}
Future<bool> signInWithEmailAndPassword({
required String email,
required String password,
}) async {
try {
UserCredential cred = await _auth.signInWithEmailAndPassword(
email: email,
password: password,
);
return cred.user?.emailVerified == true;
} catch (e) {
if (kDebugMode) print(e);
return false;
}
}
Future<bool> signInWithGoogle() async {
try {
await _auth.signInWithProvider(_googleAuthProvider);
return true;
} catch (e) {
if (kDebugMode) print(e);
return false;
}
}
Future<bool> signInWithGithub() async {
try {
await _auth.signInWithProvider(_githubProvider);
return true;
} catch (e) {
if (kDebugMode) print(e);
return false;
}
}
Future<bool> signOut() async {
try {
await _auth.signOut();
return true;
} catch (e) {
if (kDebugMode) print(e);
return false;
}
}
}
|