forked from fayaz07/flutter_firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.dart
105 lines (89 loc) · 3.44 KB
/
code.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
import 'dart:async';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
enum PhoneAuthState { Started, CodeSent, CodeResent, Verified, Failed, Error, AutoRetrievalTimeOut }
class FirebasePhoneAuth {
static var firebaseAuth;
static var _authCredential, actualCode, phone, status;
static StreamController<String> statusStream = StreamController();
static StreamController<PhoneAuthState> phoneAuthState = StreamController(sync: true);
static Stream stateStream = phoneAuthState.stream;
static instantiate({String phoneNumber}) async {
firebaseAuth = await FirebaseAuth.instance;
phone = phoneNumber;
startAuth();
}
static startAuth() {
statusStream.stream
.listen((String status) => debugPrint("PhoneAuth: " + status));
firebaseAuth.verifyPhoneNumber(
phoneNumber: phone,
timeout: Duration(seconds: 60),
verificationCompleted: verificationCompleted,
verificationFailed: verificationFailed,
codeSent: codeSent,
codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
}
static final PhoneCodeSent codeSent =
(String verificationId, [int forceResendingToken]) async {
actualCode = verificationId;
addStatus("\nEnter the code sent to " + phone);
addState(PhoneAuthState.CodeSent);
};
static final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
(String verificationId) {
actualCode = verificationId;
addStatus("\nAuto retrieval time out");
addState(PhoneAuthState.AutoRetrievalTimeOut);
};
static final PhoneVerificationFailed verificationFailed =
(AuthException authException) {
addStatus('${authException.message}');
addState(PhoneAuthState.Error);
if (authException.message.contains('not authorized'))
addStatus('App not authroized');
else if (authException.message.contains('Network'))
addStatus('Please check your internet connection and try again');
else
addStatus('Something has gone wrong, please try later ' +
authException.message);
};
static final PhoneVerificationCompleted verificationCompleted =
(AuthCredential auth) {
addStatus('Auto retrieving verification code');
firebaseAuth.signInWithCredential(auth).then((AuthResult value) {
if (value.user != null) {
addStatus(status = 'Authentication successful');
addState(PhoneAuthState.Verified);
onAuthenticationSuccessful();
} else {
addState(PhoneAuthState.Failed);
addStatus('Invalid code/invalid authentication');
}
}).catchError((error) {
addState(PhoneAuthState.Error);
addStatus('Something has gone wrong, please try later $error');
});
};
static void signInWithPhoneNumber(String smsCode) async {
_authCredential = PhoneAuthProvider.getCredential(
verificationId: actualCode, smsCode: smsCode);
firebaseAuth
.signInWithCredential(_authCredential)
.then((FirebaseUser user) async {
addStatus('Authentication successful');
addState(PhoneAuthState.Verified);
onAuthenticationSuccessful();
}).catchError((error) {
addState(PhoneAuthState.Error);
addStatus('Something has gone wrong, please try later(signInWithPhoneNumber) $error');
});
}
static onAuthenticationSuccessful() {}
static addState(PhoneAuthState state){
phoneAuthState.sink.add(state);
}
static void addStatus(String s) {
statusStream.sink.add(s);
}
}