Messaging App Initial Commit
Pages Already Implemented Login and Registeration Page
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
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;
|
||||
|
||||
//User Details
|
||||
models.User? user;
|
||||
models.Session? session;
|
||||
|
||||
// var _email;
|
||||
// var _name;
|
||||
// var _imageURL;
|
||||
// var _timeStamp;
|
||||
|
||||
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 {
|
||||
//
|
||||
status = AuthStatus.authenticating;
|
||||
notifyListeners();
|
||||
try {
|
||||
session = await account.createEmailPasswordSession(
|
||||
email: _email, password: _password);
|
||||
|
||||
// user = await account.get();
|
||||
status = AuthStatus.authenticated;
|
||||
SnackBarService.instance.showSnackBarSuccess("Welcome! ${user!.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();
|
||||
}
|
||||
|
||||
Future<void> createUser(String _email, String _password,
|
||||
Future<void> onSuccess(String _uid)) async {
|
||||
try {
|
||||
models.User _user = await account.create(
|
||||
userId: ID.unique(), email: _email, password: _password);
|
||||
|
||||
status = AuthStatus.authenticated;
|
||||
await onSuccess(_user.$id);
|
||||
SnackBarService.instance.showSnackBarSuccess("Welcome! ${_user.email}");
|
||||
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 {
|
||||
try {
|
||||
await database.createDocument(
|
||||
databaseId: databaseId,
|
||||
collectionId: userCollectionId,
|
||||
documentId: _documentId,
|
||||
data: {
|
||||
"name": _name,
|
||||
"email": _email,
|
||||
"timeStamp": DateTime.now().toUtc().toIso8601String(),
|
||||
"image": _imageURL
|
||||
});
|
||||
|
||||
status = AuthStatus.authenticated;
|
||||
} on AppwriteException catch (e) {
|
||||
SnackBarService.instance.showSnackBarError(e.message!);
|
||||
//
|
||||
} on Exception catch (e) {
|
||||
SnackBarService.instance.showSnackBarError(e.toString());
|
||||
//
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
uploadUserImage(XFile? file, String _uid) async {
|
||||
try {
|
||||
File _file = File(file!.path);
|
||||
var _test = await storage.createFile(
|
||||
bucketId: imageStorageBucket,
|
||||
fileId: ID.unique(),
|
||||
file: InputFile.fromPath(path: _file.path, filename: file.name),
|
||||
permissions: [
|
||||
Permission.read(Role.any()),
|
||||
],
|
||||
);
|
||||
|
||||
return "$endPoint/storage/buckets/${_test.bucketId}/files/${_test.$id}/view?project=$projectId";
|
||||
} on AppwriteException catch (e) {
|
||||
SnackBarService.instance.showSnackBarError(e.message!);
|
||||
print(e.message);
|
||||
//
|
||||
} on Exception catch (e) {
|
||||
SnackBarService.instance.showSnackBarError(e.toString());
|
||||
//
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user