179 lines
5.0 KiB
Dart
179 lines
5.0 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:appwrite/appwrite.dart';
|
|
import 'package:appwrite/models.dart' as models;
|
|
import 'package:flutter/material.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:messaging_app/components/constants.dart';
|
|
import 'package:messaging_app/service/navigation_service.dart';
|
|
import 'package:messaging_app/service/snackbar_service.dart';
|
|
|
|
enum AuthStatus {
|
|
notAuthenticated,
|
|
authenticated,
|
|
authenticating,
|
|
userNotFound,
|
|
error,
|
|
}
|
|
|
|
class MessagingProviders extends ChangeNotifier {
|
|
//
|
|
Client client = Client();
|
|
late final Account account;
|
|
late final Databases database;
|
|
late final Storage storage;
|
|
|
|
// Auth Status
|
|
AuthStatus? status;
|
|
|
|
models.User? user;
|
|
models.Session? session;
|
|
|
|
static MessagingProviders instance = MessagingProviders();
|
|
|
|
MessagingProviders() {
|
|
init();
|
|
loadUser();
|
|
}
|
|
|
|
init() {
|
|
client.setEndpoint(endPoint).setProject(projectId).setSelfSigned();
|
|
account = Account(client);
|
|
database = Databases(client);
|
|
storage = Storage(client);
|
|
}
|
|
|
|
loadUser() async {
|
|
try {
|
|
user = await account.get();
|
|
status = AuthStatus.authenticated;
|
|
} catch (e) {
|
|
status = AuthStatus.notAuthenticated;
|
|
} finally {
|
|
notifyListeners();
|
|
}
|
|
}
|
|
|
|
void loginWithEmailAndPassword(String _email, String _password) async {
|
|
//
|
|
notifyListeners();
|
|
try {
|
|
status != AuthStatus.error;
|
|
|
|
try {
|
|
status = AuthStatus.authenticating;
|
|
session = await account.createEmailPasswordSession(
|
|
email: _email, password: _password);
|
|
|
|
status = AuthStatus.authenticated;
|
|
var value = await database.getDocument(
|
|
databaseId: databaseId,
|
|
collectionId: userCollectionId,
|
|
documentId: session!.userId,
|
|
);
|
|
|
|
SnackBarService.instance
|
|
.showSnackBarSuccess("Welcome! ${value.data['name']}");
|
|
NavigationService.instance.navigateToReplacement("home");
|
|
account.deleteSession(sessionId: 'current');
|
|
} on AppwriteException catch (e) {
|
|
SnackBarService.instance.showSnackBarError(e.message!);
|
|
status = AuthStatus.error;
|
|
user = null;
|
|
//
|
|
} on Exception catch (e) {
|
|
SnackBarService.instance.showSnackBarError(e.toString());
|
|
//
|
|
}
|
|
notifyListeners();
|
|
} on Exception catch (e) {
|
|
SnackBarService.instance.showSnackBarError(e.toString());
|
|
}
|
|
}
|
|
|
|
Future<void> createUser(String _email, String _password, String _name,
|
|
Future<void> onSuccess(String _uid)) async {
|
|
notifyListeners();
|
|
try {
|
|
user = await account.create(
|
|
userId: ID.unique(), email: _email, password: _password, name: _name);
|
|
|
|
status = AuthStatus.authenticated;
|
|
await onSuccess(user!.$id);
|
|
SnackBarService.instance.showSnackBarSuccess("Welcome! ${user!.name}");
|
|
NavigationService.instance.goBack();
|
|
NavigationService.instance.navigateToReplacement("home");
|
|
} on AppwriteException catch (e) {
|
|
status = AuthStatus.error;
|
|
user = null;
|
|
SnackBarService.instance.showSnackBarError(e.message!);
|
|
} on Exception catch (e) {
|
|
SnackBarService.instance.showSnackBarError(e.toString());
|
|
//
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
Future<void> createUserInDB(
|
|
String _documentId, String _email, String _name, String _imageURL) async {
|
|
notifyListeners();
|
|
try {
|
|
await database.createDocument(
|
|
databaseId: databaseId,
|
|
collectionId: userCollectionId,
|
|
documentId: _documentId,
|
|
data: {
|
|
"name": _name,
|
|
"email": _email,
|
|
"lastSeen": DateTime.now().toUtc().toIso8601String(),
|
|
"image": _imageURL
|
|
},
|
|
permissions: [
|
|
Permission.read(Role.any()),
|
|
//Permission.update(Role.user(_documentId)),
|
|
],
|
|
);
|
|
|
|
status = AuthStatus.authenticated;
|
|
} on AppwriteException catch (e) {
|
|
SnackBarService.instance.showSnackBarError(e.message!);
|
|
status = AuthStatus.error;
|
|
user = null;
|
|
//
|
|
} on Exception catch (e) {
|
|
SnackBarService.instance.showSnackBarError(e.toString());
|
|
status = AuthStatus.error;
|
|
user = null;
|
|
//
|
|
}
|
|
notifyListeners();
|
|
}
|
|
|
|
uploadUserImage(XFile? file, String _uid) async {
|
|
notifyListeners();
|
|
try {
|
|
File _file = File(file!.path);
|
|
var _message = await storage.createFile(
|
|
bucketId: imageStorageBucket,
|
|
fileId: ID.unique(),
|
|
file: InputFile.fromPath(path: _file.path, filename: file.name),
|
|
permissions: [
|
|
Permission.read(Role.any()),
|
|
// Permission.update(Role.user(_uid))
|
|
],
|
|
);
|
|
|
|
return "$endPoint/storage/buckets/${_message.bucketId}/files/${_message.$id}/view?project=$projectId";
|
|
} on AppwriteException catch (e) {
|
|
SnackBarService.instance.showSnackBarError(e.message!);
|
|
//
|
|
} on Exception catch (e) {
|
|
status = AuthStatus.error;
|
|
user = null;
|
|
SnackBarService.instance.showSnackBarError(e.toString());
|
|
//
|
|
}
|
|
notifyListeners();
|
|
}
|
|
}
|