adding packages
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
export 'unsupported.dart'
|
||||
if (dart.library.html) 'web.dart'
|
||||
if (dart.library.io) 'io.dart';
|
||||
@@ -0,0 +1,52 @@
|
||||
import '../../classes/index.dart';
|
||||
|
||||
abstract class FBAuthImpl {
|
||||
final FbApp app;
|
||||
|
||||
FBAuthImpl(this.app);
|
||||
|
||||
Future<AuthUser> login(String username, String password) async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
Future<AuthUser> createAccount(String username, String password,
|
||||
{String displayName, String photoUrl}) async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
Future logout() async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
Future<AuthUser> currentUser() async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
Future<AuthUser> startAsGuest() async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
Stream<AuthUser> onAuthChanged() {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
Future editInfo({String displayName, String photoUrl}) async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
Future forgotPassword(String email) async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
Future sendEmailVerification() async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
Future loginCustomToken(String token) async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
Future loginGoogle({String idToken, String accessToken}) async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import '../../classes/index.dart';
|
||||
import '../../utils/directory.dart';
|
||||
import '../mobile/sdk.dart';
|
||||
import '../rest_api/client.dart';
|
||||
import 'impl.dart';
|
||||
|
||||
class FBAuth implements FBAuthImpl {
|
||||
final FbApp app;
|
||||
final bool useRestClient;
|
||||
File _saveFile;
|
||||
|
||||
FBAuth(
|
||||
this.app, {
|
||||
this.useRestClient = false,
|
||||
}) {
|
||||
if (useClient) {
|
||||
_client = FbClient(
|
||||
app,
|
||||
onLoad: () async {
|
||||
await _loadFile();
|
||||
Map<String, dynamic> storage = Map<String, dynamic>();
|
||||
if (await _saveFile.exists()) {
|
||||
try {
|
||||
storage = json.decode(await _saveFile.readAsString())
|
||||
as Map<String, dynamic>;
|
||||
return storage;
|
||||
} catch (_) {
|
||||
await _saveFile.delete();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
},
|
||||
onSave: (data) async {
|
||||
try {
|
||||
if (data == null) {
|
||||
await _saveFile.delete();
|
||||
} else {
|
||||
await _saveFile.writeAsString(json.encode(data));
|
||||
}
|
||||
} catch (e) {}
|
||||
},
|
||||
);
|
||||
}
|
||||
if (isMobile) {
|
||||
_sdk = FbSdk();
|
||||
}
|
||||
}
|
||||
|
||||
Future _loadFile() async {
|
||||
if (_saveFile == null) {
|
||||
final dir = await PathUtils.getDocumentDir();
|
||||
_saveFile = File('${dir.path}/fb_auth.json');
|
||||
}
|
||||
}
|
||||
|
||||
bool get useClient => isDesktop || useRestClient;
|
||||
static bool get isDesktop =>
|
||||
Platform.isWindows || Platform.isWindows || Platform.isMacOS;
|
||||
static bool get isMobile => Platform.isIOS || Platform.isAndroid;
|
||||
FbSdk _sdk;
|
||||
FbClient _client;
|
||||
@override
|
||||
Future<AuthUser> login(String username, String password) async {
|
||||
if (useClient) {
|
||||
await _loadFile();
|
||||
return _client.login(username, password);
|
||||
} else {
|
||||
return _sdk.login(username, password);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<AuthUser> onAuthChanged() {
|
||||
if (useClient) {
|
||||
return _client.onAuthChanged();
|
||||
} else {
|
||||
return _sdk.onAuthChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<AuthUser> startAsGuest() async {
|
||||
try {
|
||||
if (useClient) {
|
||||
await _loadFile();
|
||||
return _client.startAsGuest();
|
||||
} else {
|
||||
return _sdk.startAsGuest();
|
||||
}
|
||||
} catch (e) {}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Future logout() async {
|
||||
if (useClient) {
|
||||
await _loadFile();
|
||||
await _client.logout();
|
||||
} else {
|
||||
await _sdk.logout();
|
||||
}
|
||||
try {
|
||||
await _saveFile.delete();
|
||||
} catch (e) {}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<AuthUser> currentUser() async {
|
||||
if (useClient) {
|
||||
await _loadFile();
|
||||
return _client.currentUser();
|
||||
} else {
|
||||
return _sdk.currentUser();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future editInfo({String displayName, String photoUrl}) async {
|
||||
if (useClient) {
|
||||
await _loadFile();
|
||||
return _client.editInfo(
|
||||
displayName: displayName,
|
||||
photoUrl: photoUrl,
|
||||
);
|
||||
} else {
|
||||
return _sdk.editInfo(
|
||||
displayName: displayName,
|
||||
photoUrl: photoUrl,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future forgotPassword(String email) async {
|
||||
if (useClient) {
|
||||
await _loadFile();
|
||||
return _client.forgotPassword(email);
|
||||
} else {
|
||||
return _sdk.forgotPassword(email);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future sendEmailVerification() async {
|
||||
if (useClient) {
|
||||
await _loadFile();
|
||||
return _client.sendEmailVerification();
|
||||
} else {
|
||||
return _sdk.sendEmailVerification();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<AuthUser> createAccount(String username, String password,
|
||||
{String displayName, String photoUrl}) async {
|
||||
if (useClient) {
|
||||
await _loadFile();
|
||||
return _client.createAccount(
|
||||
username,
|
||||
password,
|
||||
photoUrl: photoUrl,
|
||||
displayName: displayName,
|
||||
);
|
||||
} else {
|
||||
return _sdk.createAccount(
|
||||
username,
|
||||
password,
|
||||
photoUrl: photoUrl,
|
||||
displayName: displayName,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future loginCustomToken(String token) async {
|
||||
if (useClient) {
|
||||
return _client.loginCustomToken(token);
|
||||
} else {
|
||||
return _sdk.loginCustomToken(token);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future loginGoogle({String idToken, String accessToken}) async {
|
||||
if (useClient) {
|
||||
return _client.loginGoogle(
|
||||
idToken: idToken,
|
||||
accessToken: accessToken,
|
||||
);
|
||||
} else {
|
||||
return _sdk.loginGoogle(
|
||||
idToken: idToken,
|
||||
accessToken: accessToken,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import '../../classes/index.dart';
|
||||
import 'impl.dart';
|
||||
|
||||
class FBAuth implements FBAuthImpl {
|
||||
final FbApp app;
|
||||
|
||||
FBAuth(this.app);
|
||||
|
||||
@override
|
||||
Future<AuthUser> login(String username, String password) async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
@override
|
||||
Future<AuthUser> createAccount(String username, String password,
|
||||
{String displayName, String photoUrl}) async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
@override
|
||||
Future logout() async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
@override
|
||||
Future<AuthUser> currentUser() async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
@override
|
||||
Future<AuthUser> startAsGuest() async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<AuthUser> onAuthChanged() {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
@override
|
||||
Future editInfo({String displayName, String photoUrl}) async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
@override
|
||||
Future forgotPassword(String email) async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
@override
|
||||
Future sendEmailVerification() async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
@override
|
||||
Future loginCustomToken(String token) async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
|
||||
@override
|
||||
Future loginGoogle({String idToken, String accessToken}) async {
|
||||
throw 'Platform Not Supported';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
import 'package:firebase/firebase.dart';
|
||||
|
||||
import '../../classes/index.dart';
|
||||
import 'impl.dart';
|
||||
|
||||
class FBAuth implements FBAuthImpl {
|
||||
final FbApp app;
|
||||
final _auth = auth();
|
||||
|
||||
FBAuth(this.app);
|
||||
|
||||
Future _setPersistenceWeb(Auth _auth) async {
|
||||
// try {
|
||||
// var selectedPersistence = 'local';
|
||||
// await _auth.setPersistence(selectedPersistence);
|
||||
// } catch (e) {
|
||||
// print('_auth.setPersistence -> $e');
|
||||
// }
|
||||
}
|
||||
|
||||
@override
|
||||
Future<AuthUser> login(String username, String password) async {
|
||||
await _setPersistenceWeb(_auth);
|
||||
try {
|
||||
final _result =
|
||||
await _auth.signInWithEmailAndPassword(username, password);
|
||||
if (_result != null && _result?.user != null) {
|
||||
final _user = AuthUser(
|
||||
uid: _result.user.uid,
|
||||
displayName: _result.user.displayName,
|
||||
email: _result.user?.email,
|
||||
isAnonymous: _result.user.isAnonymous,
|
||||
isEmailVerified: _result.user.emailVerified,
|
||||
photoUrl: _result.user.photoURL,
|
||||
);
|
||||
return _user;
|
||||
}
|
||||
} catch (e) {
|
||||
print('FBAuthUtils -> _loginWeb -> $e');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<AuthUser> startAsGuest() async {
|
||||
await _setPersistenceWeb(_auth);
|
||||
try {
|
||||
final _result = await _auth.signInAnonymously();
|
||||
if (_result != null && _result?.user != null) {
|
||||
final _user = AuthUser(
|
||||
uid: _result.user.uid,
|
||||
displayName: _result.user.displayName,
|
||||
email: _result.user?.email,
|
||||
isAnonymous: _result.user.isAnonymous,
|
||||
isEmailVerified: _result.user.emailVerified,
|
||||
photoUrl: _result.user.photoURL,
|
||||
);
|
||||
return _user;
|
||||
}
|
||||
} catch (e) {
|
||||
print('FBAuthUtils -> startAsGuest -> $e');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Future logout() async {
|
||||
try {
|
||||
await _auth.signOut();
|
||||
} catch (e) {
|
||||
print('FBAuthUtils -> logout -> $e');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<AuthUser> onAuthChanged() {
|
||||
return _auth.onAuthStateChanged.map((user) {
|
||||
if (user == null) return null;
|
||||
final _user = AuthUser(
|
||||
uid: user.uid,
|
||||
displayName: user.displayName,
|
||||
email: user?.email,
|
||||
isAnonymous: user.isAnonymous,
|
||||
isEmailVerified: user.emailVerified,
|
||||
photoUrl: user.photoURL,
|
||||
);
|
||||
return _user;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Future<AuthUser> currentUser() async {
|
||||
await _setPersistenceWeb(_auth);
|
||||
try {
|
||||
final _result = _auth.currentUser;
|
||||
if (_result != null) {
|
||||
final _user = AuthUser(
|
||||
uid: _result.uid,
|
||||
displayName: _result.displayName,
|
||||
email: _result?.email,
|
||||
isAnonymous: _result.isAnonymous,
|
||||
isEmailVerified: _result.emailVerified,
|
||||
photoUrl: _result.photoURL,
|
||||
);
|
||||
return _user;
|
||||
}
|
||||
} catch (e) {
|
||||
print('FBAuthUtils -> currentUser -> $e');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Future editInfo({String displayName, String photoUrl}) async {
|
||||
final _user = _auth.currentUser;
|
||||
final _info = UserProfile();
|
||||
if (displayName != null) _info.displayName = displayName;
|
||||
if (photoUrl != null) _info.photoURL = photoUrl;
|
||||
try {
|
||||
await _user.updateProfile(_info);
|
||||
} catch (e) {
|
||||
throw 'Error editInfo -> $e';
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future forgotPassword(String email) async {
|
||||
try {
|
||||
await _auth.sendPasswordResetEmail(email);
|
||||
} catch (e) {
|
||||
throw 'Error forgotPassword -> $e';
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future sendEmailVerification() async {
|
||||
try {
|
||||
final _user = _auth.currentUser;
|
||||
await _user.sendEmailVerification();
|
||||
} catch (e) {
|
||||
throw 'Error sendEmailVerification -> $e';
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<AuthUser> createAccount(String username, String password,
|
||||
{String displayName, String photoUrl}) async {
|
||||
UserCredential _user;
|
||||
try {
|
||||
_user = await _auth.createUserWithEmailAndPassword(username, password);
|
||||
if (_user != null) {
|
||||
await editInfo(displayName: displayName, photoUrl: photoUrl);
|
||||
}
|
||||
} catch (e) {}
|
||||
if (_user == null) {
|
||||
try {
|
||||
_user = await _auth.signInWithEmailAndPassword(username, password);
|
||||
} catch (err) {
|
||||
throw Exception(err);
|
||||
}
|
||||
}
|
||||
return await currentUser();
|
||||
}
|
||||
|
||||
@override
|
||||
Future loginCustomToken(String token) async {
|
||||
await _setPersistenceWeb(_auth);
|
||||
try {
|
||||
final _result = await _auth.signInAndRetrieveDataWithCustomToken(token);
|
||||
if (_result != null && _result?.user != null) {
|
||||
final _user = AuthUser(
|
||||
uid: _result.user.uid,
|
||||
displayName: _result.user.displayName,
|
||||
email: _result.user?.email,
|
||||
isAnonymous: _result.user.isAnonymous,
|
||||
isEmailVerified: _result.user.emailVerified,
|
||||
photoUrl: _result.user.photoURL,
|
||||
);
|
||||
return _user;
|
||||
}
|
||||
} catch (e) {
|
||||
print('FBAuthUtils -> loginCustomToken -> $e');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Future loginGoogle({String idToken, String accessToken}) async {
|
||||
final _cred = GoogleAuthProvider.credential(idToken, accessToken);
|
||||
await _setPersistenceWeb(_auth);
|
||||
try {
|
||||
final _result = await _auth.signInWithCredential(_cred);
|
||||
if (_result != null && _result?.user != null) {
|
||||
final _user = AuthUser(
|
||||
uid: _result.user.uid,
|
||||
displayName: _result.user.displayName,
|
||||
email: _result.user?.email,
|
||||
isAnonymous: _result.user.isAnonymous,
|
||||
isEmailVerified: _result.user.emailVerified,
|
||||
photoUrl: _result.user.photoURL,
|
||||
);
|
||||
return _user;
|
||||
}
|
||||
} catch (e) {
|
||||
print('FBAuthUtils -> loginCustomToken -> $e');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user