Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix - Login creates a user #260

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions lib/components/login/login.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
Expand Down Expand Up @@ -71,9 +72,8 @@ class _LoginPageState extends State<LoginPage> {
setState((){
_isLoading = true;
});
final GoogleSignIn googleSignIn = GoogleSignIn();

try {
final GoogleSignIn googleSignIn = GoogleSignIn();
final GoogleSignInAccount? googleSignInAccount = await googleSignIn.signIn();

if (googleSignInAccount != null) {
Expand All @@ -88,19 +88,39 @@ class _LoginPageState extends State<LoginPage> {
final User? user = authResult.user;

if (user != null) {
final firestore = FirebaseFirestore.instance;
final String? username = googleSignInAccount.displayName;
final String email = googleSignInAccount.email;

SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setBool('isLoggedIn', true);
await prefs.setInt('loginTimestamp', DateTime.now().millisecondsSinceEpoch);
final querySnapshot = await firestore
.collection('users')
.where('email', isEqualTo: email)
.get();

if(mounted) { // Check if the widget is still in the tree
showToast(message: 'Google sign in successful ');
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const MainPage(),
),
if (querySnapshot.docs.isEmpty) {
await firestore.collection('users').doc(user.uid).set(
{
'username': username,
'email': email,
'surname': null,
'balance': 0,
'profileImageUrl': null,
'notificationsEnabled': false,
}
);
}
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setBool('isLoggedIn', true);
await prefs.setInt('loginTimestamp', DateTime.now().millisecondsSinceEpoch);

if(mounted) { // Check if the widget is still in the tree
showToast(message: 'Google sign in successful');
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const MainPage(),
),
);
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions lib/components/login/signup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:smart_parking_system/components/common/common_functions.dart';
import 'package:smart_parking_system/components/login/card_registration.dart';
import 'package:smart_parking_system/components/common/toast.dart';
Expand Down Expand Up @@ -80,6 +81,10 @@ class _SignupPageState extends State<SignupPage> {
}
);

SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setBool('isLoggedIn', true);
await prefs.setInt('loginTimestamp', DateTime.now().millisecondsSinceEpoch);

if(mounted) { // Check if the widget is still in the tree
showToast(message: 'Successfully signed up');
Navigator.of(context).push(
Expand Down
Loading