-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.dart
64 lines (45 loc) · 1.69 KB
/
auth.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
import 'package:firebase_auth/firebase_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'dart:async';
import 'package:systemreader9/shared/logprocess.dart';
class AuthService {
final GoogleSignIn _googleSignIn = GoogleSignIn();
final FirebaseAuth _auth = FirebaseAuth.instance;
final Firestore _db = Firestore.instance;
Future<FirebaseUser> get getUser => _auth.currentUser();
Stream<FirebaseUser> get user => _auth.onAuthStateChanged;
Future<FirebaseUser> googleSignIn() async {
try {
GoogleSignInAccount googleSignInAccount = await _googleSignIn.signIn();
GoogleSignInAuthentication googleAuth =
await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
FirebaseUser user = await _auth.signInWithCredential(credential);
updateUserData(user);
return user;
} catch (error, stackTrace) {
LogProcess.getState().reportError(error,stackTrace);
return null;
}
}
Future<FirebaseUser> anonLogin() async {
FirebaseUser user = await _auth.signInAnonymously();
updateUserData(user);
return user;
}
Future<void> updateUserData(FirebaseUser user) {
DocumentReference reportRef = _db.collection('reports').document(user.uid);
return reportRef.setData({
'uid': user.uid,
'lastActivity': DateTime.now()
}, merge: true);
}
Future<void> signOut() {
return _auth.signOut();
}
}
// keytool -list -v -alias androiddebugkey -keystore %USERPROFILE%\.android\debug.keystore