aboutsummaryrefslogtreecommitdiff
path: root/lib/firebase
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2023-05-20 16:43:21 -0600
committerIván Ávalos <avalos@disroot.org>2023-05-20 16:43:21 -0600
commitcd8f22959e62c0b7b191e64f4ecfec53cba9f364 (patch)
tree9e40717d4e7b5c18694ab65558acda65875f817b /lib/firebase
parent3a3af285a7460a7e2f6850b9a8dd7465492163f6 (diff)
downloadlinkchat-cd8f22959e62c0b7b191e64f4ecfec53cba9f364.tar.gz
linkchat-cd8f22959e62c0b7b191e64f4ecfec53cba9f364.tar.bz2
linkchat-cd8f22959e62c0b7b191e64f4ecfec53cba9f364.zip
Se añade autenticación y onboarding
Diffstat (limited to 'lib/firebase')
-rw-r--r--lib/firebase/auth.dart83
-rw-r--r--lib/firebase/storage.dart16
2 files changed, 99 insertions, 0 deletions
diff --git a/lib/firebase/auth.dart b/lib/firebase/auth.dart
new file mode 100644
index 0000000..a876d5b
--- /dev/null
+++ b/lib/firebase/auth.dart
@@ -0,0 +1,83 @@
+import 'dart:io';
+
+import 'package:firebase_auth/firebase_auth.dart';
+import 'package:flutter/foundation.dart';
+
+import 'storage.dart';
+
+class Auth {
+ final FirebaseAuth _auth = FirebaseAuth.instance;
+ final GithubAuthProvider _githubProvider = GithubAuthProvider();
+ final GoogleAuthProvider _googleAuthProvider = GoogleAuthProvider();
+
+ User? get currentUser => _auth.currentUser;
+
+ Future<bool> createUserWithEmailAndPassword({
+ required String email,
+ required String password,
+ required String displayName,
+ required File avatar,
+ }) async {
+ try {
+ UserCredential cred = await _auth.createUserWithEmailAndPassword(
+ email: email,
+ password: password,
+ );
+ User? user = cred.user;
+ if (user != null) {
+ user.updateDisplayName(displayName);
+ user.updatePhotoURL(await Storage().uploadAvatar(user.uid, avatar));
+ }
+ return true;
+ } catch (e) {
+ if (kDebugMode) print(e);
+ return false;
+ }
+ }
+
+ Future<bool> signInWithEmailAndPassword({
+ required String email,
+ required String password,
+ }) async {
+ try {
+ UserCredential cred = await _auth.signInWithEmailAndPassword(
+ email: email,
+ password: password,
+ );
+ return cred.user?.emailVerified == true;
+ } catch (e) {
+ if (kDebugMode) print(e);
+ return false;
+ }
+ }
+
+ Future<bool> signInWithGoogle() async {
+ try {
+ await _auth.signInWithProvider(_googleAuthProvider);
+ return true;
+ } catch (e) {
+ if (kDebugMode) print(e);
+ return false;
+ }
+ }
+
+ Future<bool> signInWithGithub() async {
+ try {
+ await _auth.signInWithProvider(_githubProvider);
+ return true;
+ } catch (e) {
+ if (kDebugMode) print(e);
+ return false;
+ }
+ }
+
+ Future<bool> signOut() async {
+ try {
+ await _auth.signOut();
+ return true;
+ } catch (e) {
+ if (kDebugMode) print(e);
+ return false;
+ }
+ }
+}
diff --git a/lib/firebase/storage.dart b/lib/firebase/storage.dart
new file mode 100644
index 0000000..3e0c629
--- /dev/null
+++ b/lib/firebase/storage.dart
@@ -0,0 +1,16 @@
+import 'dart:io';
+
+import 'package:firebase_storage/firebase_storage.dart';
+import 'package:path/path.dart';
+
+class Storage {
+ final FirebaseStorage _storage = FirebaseStorage.instance;
+
+ Future<String> uploadAvatar(String userId, File file) async {
+ String filename = basename(file.path);
+ Reference ref = _storage.ref().child(userId).child(filename);
+ UploadTask task = ref.putFile(file);
+ await task.whenComplete(() => {});
+ return await ref.getDownloadURL();
+ }
+}