Connection with google done
continuous-integration/drone/push Build is passing Details

pull/57/head
Emre KARTAL 2 years ago
parent 75ca7ba086
commit cf0accfc47

@ -0,0 +1,6 @@
class UserException implements Exception {
String code;
String description;
UserException(this.code,this.description);
}

@ -1,4 +1,3 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
@ -10,6 +9,7 @@ import 'package:justmusic/main.dart';
import 'package:justmusic/values/constants.dart'; import 'package:justmusic/values/constants.dart';
import '../components/login_button.dart'; import '../components/login_button.dart';
import '../exceptions/user_exception.dart';
class LoginScreen extends StatefulWidget { class LoginScreen extends StatefulWidget {
const LoginScreen({Key? key}) : super(key: key); const LoginScreen({Key? key}) : super(key: key);
@ -33,7 +33,7 @@ class _LoginScreenState extends State<LoginScreen> {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
content: Text( content: Text(
e.toString() ?? "", e.toString(),
style: GoogleFonts.plusJakartaSans(color: Colors.white, fontWeight: FontWeight.w400, fontSize: 20.h), style: GoogleFonts.plusJakartaSans(color: Colors.white, fontWeight: FontWeight.w400, fontSize: 20.h),
), ),
backgroundColor: Colors.red, backgroundColor: Colors.red,
@ -43,6 +43,18 @@ class _LoginScreenState extends State<LoginScreen> {
} }
} }
signInWithGoogle() async {
try {
await MyApp.userViewModel.signInWithGoogle();
} on UserException catch (e) {
if (e.code == 'user-created') {
Navigator.pushNamed(context, '/explanation');
} else if (e.code == 'user-already-exist') {
Navigator.pushNamed(context, '/feed');
}
}
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return GestureDetector( return GestureDetector(
@ -269,7 +281,7 @@ class _LoginScreenState extends State<LoginScreen> {
SignInButton( SignInButton(
Buttons.Google, Buttons.Google,
text: "Login with Google", text: "Login with Google",
onPressed: () {}, onPressed: signInWithGoogle,
), ),
], ],
), ),

@ -1,4 +1,3 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart';
@ -21,8 +20,7 @@ class _ProfileScreenState extends State<ProfileScreen> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
Future<void> logout() async { Future<void> logout() async {
print("cc"); await MyApp.userViewModel.logout();
await FirebaseAuth.instance.signOut();
Navigator.pushNamed(context, '/welcome'); Navigator.pushNamed(context, '/welcome');
} }

@ -7,6 +7,7 @@ import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_signin_button/button_list.dart'; import 'package:flutter_signin_button/button_list.dart';
import 'package:flutter_signin_button/button_view.dart'; import 'package:flutter_signin_button/button_view.dart';
import 'package:google_fonts/google_fonts.dart'; import 'package:google_fonts/google_fonts.dart';
import 'package:justmusic/exceptions/user_exception.dart';
import 'package:justmusic/values/constants.dart'; import 'package:justmusic/values/constants.dart';
import '../components/login_button.dart'; import '../components/login_button.dart';
@ -37,7 +38,7 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
content: Text( content: Text(
e.toString() ?? "", e.toString(),
style: GoogleFonts.plusJakartaSans( style: GoogleFonts.plusJakartaSans(
color: Colors.white, color: Colors.white,
fontWeight: FontWeight.w400, fontWeight: FontWeight.w400,
@ -51,8 +52,15 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
} }
signInWithGoogle() async { signInWithGoogle() async {
await MyApp.userViewModel.signInWithGoogle(); try {
//Navigator.pushNamed(context, '/explanation'); await MyApp.userViewModel.signInWithGoogle();
} on UserException catch (e) {
if (e.code == 'user-created') {
Navigator.pushNamed(context, '/explanation');
} else if (e.code == 'user-already-exist') {
Navigator.pushNamed(context, '/feed');
}
}
} }
@override @override

@ -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 {

@ -80,7 +80,13 @@ class UserViewModel {
} }
signInWithGoogle() async { signInWithGoogle() async {
await _authService.signInWithGoogle(); try {
await _authService.signInWithGoogle();
await updateUserCurrent();
} catch (e) {
print(e);
rethrow;
}
} }
Future<List<User>> getUsersByUniqueId(String uniqueId) async { Future<List<User>> getUsersByUniqueId(String uniqueId) async {
@ -105,12 +111,12 @@ class UserViewModel {
} }
} }
logout() { logout() async {
_authService.signOut(); await _authService.signOut();
} }
delete() { delete() async {
_authService.delete(); await _authService.delete();
} }
bool isFriend(String id) { bool isFriend(String id) {

Loading…
Cancel
Save