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/material.dart';
import 'package:flutter/services.dart';
@ -10,6 +9,7 @@ import 'package:justmusic/main.dart';
import 'package:justmusic/values/constants.dart';
import '../components/login_button.dart';
import '../exceptions/user_exception.dart';
class LoginScreen extends StatefulWidget {
const LoginScreen({Key? key}) : super(key: key);
@ -33,7 +33,7 @@ class _LoginScreenState extends State<LoginScreen> {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
e.toString() ?? "",
e.toString(),
style: GoogleFonts.plusJakartaSans(color: Colors.white, fontWeight: FontWeight.w400, fontSize: 20.h),
),
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
Widget build(BuildContext context) {
return GestureDetector(
@ -269,7 +281,7 @@ class _LoginScreenState extends State<LoginScreen> {
SignInButton(
Buttons.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/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
@ -21,8 +20,7 @@ class _ProfileScreenState extends State<ProfileScreen> {
@override
Widget build(BuildContext context) {
Future<void> logout() async {
print("cc");
await FirebaseAuth.instance.signOut();
await MyApp.userViewModel.logout();
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_view.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:justmusic/exceptions/user_exception.dart';
import 'package:justmusic/values/constants.dart';
import '../components/login_button.dart';
@ -37,7 +38,7 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
e.toString() ?? "",
e.toString(),
style: GoogleFonts.plusJakartaSans(
color: Colors.white,
fontWeight: FontWeight.w400,
@ -51,8 +52,15 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
}
signInWithGoogle() async {
try {
await MyApp.userViewModel.signInWithGoogle();
//Navigator.pushNamed(context, '/explanation');
} 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

@ -3,21 +3,17 @@ import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:justmusic/exceptions/user_exception.dart';
import '../main.dart';
class AuthService {
register(String pseudo, String email, String password) async {
try {
Future<void> addUserToFirestore(
UserCredential userCredential, String pseudo, String email) async {
var token;
final data = await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: email,
password: password,
);
if (kIsWeb) {
token = "empty";
} else {
if (!kIsWeb) {
token = await FirebaseMessaging.instance.getToken();
} else {
token = "empty";
}
String uniqueId = await generateUniqueId(pseudo);
@ -35,12 +31,24 @@ class AuthService {
"https://firebasestorage.googleapis.com/v0/b/justmusic-435d5.appspot.com/o/justMusicDefaultImage.png?alt=media&token=020d0fcb-b7df-4d4d-b380-e99597293fcc"
};
MyApp.db
try {
await MyApp.db
.collection("users")
.doc(data.user?.uid)
.set(user)
.then((value) => print("User Added"))
.catchError((error) => print("Failed to add user: $error"));
.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 {
final data = await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: email,
password: password,
);
await addUserToFirestore(data, pseudo, email);
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
throw ('Mot de passe trop court');
@ -55,15 +63,26 @@ class AuthService {
signInWithGoogle() async {
final GoogleSignInAccount? gUser = await GoogleSignIn().signIn();
final GoogleSignInAuthentication gAuth = await gUser!.authentication;
final credential = GoogleAuthProvider.credential(
accessToken: gAuth.accessToken,
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 {
@ -100,8 +119,12 @@ class AuthService {
}
}
void signOut() async {
signOut() async {
final GoogleSignIn googleSignIn = GoogleSignIn();
await FirebaseAuth.instance.signOut();
if (await googleSignIn.isSignedIn()) {
await googleSignIn.signOut();
}
}
Future<void> delete() async {

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

Loading…
Cancel
Save