forked from terrence-au/aimimi_flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_service.dart
186 lines (158 loc) · 5.58 KB
/
auth_service.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import 'package:aimimi/services/user_document.dart';
import 'package:apple_sign_in/apple_sign_in.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_sign_in/google_sign_in.dart';
import "package:aimimi/models/user.dart";
class AuthService extends ChangeNotifier {
final googleSignIn = GoogleSignIn();
final FirebaseAuth auth = FirebaseAuth.instance;
bool _isSigningIn;
AuthService() {
_isSigningIn = false;
}
bool get isSigningIn => _isSigningIn;
set isSigningIn(bool isSigningIn) {
_isSigningIn = isSigningIn;
notifyListeners();
}
OurUser _createUser(User user) {
return user != null
? OurUser(uid: user.uid, username: user.displayName)
: null;
}
Stream<OurUser> get user {
return FirebaseAuth.instance.userChanges().map(_createUser);
}
Future signUp(
String email, String password, String name, BuildContext context) async {
isSigningIn = true;
try {
UserCredential result = await auth.createUserWithEmailAndPassword(
email: email, password: password);
await auth.currentUser.updateProfile(displayName: name);
await UserDocument(uid: auth.currentUser.uid).createUserDocuments(
FieldValue.serverTimestamp(), name, auth.currentUser.photoURL);
isSigningIn = false;
return _createUser(result.user);
} catch (e) {
showError(e.message, context);
print(e);
isSigningIn = false;
}
}
Future emailLogin(String email, String password, BuildContext context) async {
isSigningIn = true;
try {
UserCredential result = await auth.signInWithEmailAndPassword(
email: email, password: password);
isSigningIn = false;
return _createUser(result.user);
} catch (e) {
showError(e.message, context);
print(e);
isSigningIn = false;
}
}
showError(String errormessage, BuildContext context) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('ERROR'),
content: Text(errormessage),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('OK'))
],
);
});
}
Future googleLogin() async {
isSigningIn = true;
if (googleSignIn.currentUser != null) {
await googleSignIn.signOut();
}
final user = await googleSignIn.signIn();
if (user == null) {
isSigningIn = false;
return;
} else {
final googleAuth = await user.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
UserCredential result =
await FirebaseAuth.instance.signInWithCredential(credential);
// Update Firebase auth info from Google
await auth.currentUser.updateProfile(
displayName: user.displayName, photoURL: user.photoUrl);
await UserDocument(uid: auth.currentUser.uid).createUserDocuments(
FieldValue.serverTimestamp(),
result.user.displayName,
result.user.photoURL);
isSigningIn = false;
return _createUser(result.user);
}
}
Future appleSignIn(BuildContext context) async {
if (!await AppleSignIn.isAvailable()) {
showError("Apple sign in is not available on this device", context);
return;
}
print("do what?");
final result = await AppleSignIn.performRequests([
AppleIdRequest(requestedScopes: [Scope.email, Scope.fullName])
]);
print("result" + result.toString());
switch (result.status) {
case AuthorizationStatus.authorized:
try {
final AppleIdCredential appleIdCredential = result.credential;
final OAuthProvider oAuthProvider = OAuthProvider("apple.com");
final credential = oAuthProvider.credential(
idToken: String.fromCharCodes(appleIdCredential.identityToken),
accessToken:
String.fromCharCodes(appleIdCredential.authorizationCode));
UserCredential res =
await FirebaseAuth.instance.signInWithCredential(credential);
print("Apple" + appleIdCredential.toString());
// Update Firebase auth info from Google
await auth.currentUser.updateProfile(
displayName:
"${appleIdCredential.fullName.givenName} ${appleIdCredential.fullName.familyName}",
photoURL: null);
await UserDocument(uid: auth.currentUser.uid).createUserDocuments(
FieldValue.serverTimestamp(),
"${appleIdCredential.fullName.givenName} ${appleIdCredential.fullName.familyName}",
null);
return _createUser(res.user);
} on PlatformException catch (error) {
showError(error.message, context);
} on FirebaseAuthException catch (error) {
showError(error.message, context);
}
break;
case AuthorizationStatus.error:
showError("Apple authorization failed", context);
break;
case AuthorizationStatus.cancelled:
break;
}
}
Future logout() async {
if (auth.currentUser.providerData[0].providerId == 'google.com') {
return await Future.wait([
FirebaseAuth.instance.signOut(),
googleSignIn.disconnect(),
]);
}
return FirebaseAuth.instance.signOut();
}
}