|
|
@ -3,44 +3,52 @@ import 'package:firebase_auth/firebase_auth.dart';
|
|
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:google_sign_in/google_sign_in.dart';
|
|
|
|
import 'package:google_sign_in/google_sign_in.dart';
|
|
|
|
|
|
|
|
import 'package:justmusic/exceptions/user_exception.dart';
|
|
|
|
import '../main.dart';
|
|
|
|
import '../main.dart';
|
|
|
|
|
|
|
|
|
|
|
|
class AuthService {
|
|
|
|
class AuthService {
|
|
|
|
register(String pseudo, String email, String password) async {
|
|
|
|
Future<void> addUserToFirestore(
|
|
|
|
|
|
|
|
UserCredential userCredential, String pseudo, String email) async {
|
|
|
|
|
|
|
|
var token;
|
|
|
|
|
|
|
|
if (!kIsWeb) {
|
|
|
|
|
|
|
|
token = await FirebaseMessaging.instance.getToken();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
token = "empty";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String uniqueId = await generateUniqueId(pseudo);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final user = <String, dynamic>{
|
|
|
|
|
|
|
|
"mail": email,
|
|
|
|
|
|
|
|
"pseudo": pseudo,
|
|
|
|
|
|
|
|
"unique_id": uniqueId,
|
|
|
|
|
|
|
|
"followed": [],
|
|
|
|
|
|
|
|
"nbCapsules": 0,
|
|
|
|
|
|
|
|
"followers": [],
|
|
|
|
|
|
|
|
"token_notify": token,
|
|
|
|
|
|
|
|
"musics_likes": [],
|
|
|
|
|
|
|
|
"picture":
|
|
|
|
|
|
|
|
"https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/justMusicDefaultImage.png?alt=media&token=020d0fcb-b7df-4d4d-b380-e99597293fcc"
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
await MyApp.db
|
|
|
|
|
|
|
|
.collection("users")
|
|
|
|
|
|
|
|
.doc(userCredential.user?.uid)
|
|
|
|
|
|
|
|
.set(user);
|
|
|
|
|
|
|
|
print("User Added");
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
print("Failed to add user: $error");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> register(String pseudo, String email, String password) async {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
var token;
|
|
|
|
|
|
|
|
final data = await FirebaseAuth.instance.createUserWithEmailAndPassword(
|
|
|
|
final data = await FirebaseAuth.instance.createUserWithEmailAndPassword(
|
|
|
|
email: email,
|
|
|
|
email: email,
|
|
|
|
password: password,
|
|
|
|
password: password,
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
await addUserToFirestore(data, pseudo, email);
|
|
|
|
if (kIsWeb) {
|
|
|
|
|
|
|
|
token = "empty";
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
token = await FirebaseMessaging.instance.getToken();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String uniqueId = await generateUniqueId(pseudo);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final user = <String, dynamic>{
|
|
|
|
|
|
|
|
"mail": email,
|
|
|
|
|
|
|
|
"pseudo": pseudo,
|
|
|
|
|
|
|
|
"unique_id": uniqueId,
|
|
|
|
|
|
|
|
"followed": [],
|
|
|
|
|
|
|
|
"nbCapsules": 0,
|
|
|
|
|
|
|
|
"followers": [],
|
|
|
|
|
|
|
|
"token_notify": token,
|
|
|
|
|
|
|
|
"musics_likes": [],
|
|
|
|
|
|
|
|
"picture":
|
|
|
|
|
|
|
|
"https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/justMusicDefaultImage.png?alt=media&token=020d0fcb-b7df-4d4d-b380-e99597293fcc"
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MyApp.db
|
|
|
|
|
|
|
|
.collection("users")
|
|
|
|
|
|
|
|
.doc(data.user?.uid)
|
|
|
|
|
|
|
|
.set(user)
|
|
|
|
|
|
|
|
.then((value) => print("User Added"))
|
|
|
|
|
|
|
|
.catchError((error) => print("Failed to add user: $error"));
|
|
|
|
|
|
|
|
} on FirebaseAuthException catch (e) {
|
|
|
|
} on FirebaseAuthException catch (e) {
|
|
|
|
if (e.code == 'weak-password') {
|
|
|
|
if (e.code == 'weak-password') {
|
|
|
|
throw ('Mot de passe trop court');
|
|
|
|
throw ('Mot de passe trop court');
|
|
|
@ -55,15 +63,26 @@ class AuthService {
|
|
|
|
|
|
|
|
|
|
|
|
signInWithGoogle() async {
|
|
|
|
signInWithGoogle() async {
|
|
|
|
final GoogleSignInAccount? gUser = await GoogleSignIn().signIn();
|
|
|
|
final GoogleSignInAccount? gUser = await GoogleSignIn().signIn();
|
|
|
|
|
|
|
|
|
|
|
|
final GoogleSignInAuthentication gAuth = await gUser!.authentication;
|
|
|
|
final GoogleSignInAuthentication gAuth = await gUser!.authentication;
|
|
|
|
|
|
|
|
|
|
|
|
final credential = GoogleAuthProvider.credential(
|
|
|
|
final credential = GoogleAuthProvider.credential(
|
|
|
|
accessToken: gAuth.accessToken,
|
|
|
|
accessToken: gAuth.accessToken,
|
|
|
|
idToken: gAuth.idToken,
|
|
|
|
idToken: gAuth.idToken,
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
return await FirebaseAuth.instance.signInWithCredential(credential);
|
|
|
|
final userCredential =
|
|
|
|
|
|
|
|
await FirebaseAuth.instance.signInWithCredential(credential);
|
|
|
|
|
|
|
|
final user =
|
|
|
|
|
|
|
|
await MyApp.db.collection("users").doc(userCredential.user?.uid).get();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!user.exists) {
|
|
|
|
|
|
|
|
await addUserToFirestore(
|
|
|
|
|
|
|
|
userCredential,
|
|
|
|
|
|
|
|
userCredential.user?.displayName ?? "user",
|
|
|
|
|
|
|
|
userCredential.user?.email ?? "");
|
|
|
|
|
|
|
|
throw UserException("user-created", "L'utilisateur vien d'être créé");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
throw UserException("user-already-exist", "L'utilisateur existe déjà");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<String> generateUniqueId(String pseudo) async {
|
|
|
|
Future<String> generateUniqueId(String pseudo) async {
|
|
|
@ -100,8 +119,12 @@ class AuthService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void signOut() async {
|
|
|
|
signOut() async {
|
|
|
|
|
|
|
|
final GoogleSignIn googleSignIn = GoogleSignIn();
|
|
|
|
await FirebaseAuth.instance.signOut();
|
|
|
|
await FirebaseAuth.instance.signOut();
|
|
|
|
|
|
|
|
if (await googleSignIn.isSignedIn()) {
|
|
|
|
|
|
|
|
await googleSignIn.signOut();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> delete() async {
|
|
|
|
Future<void> delete() async {
|
|
|
|