-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from Steelhacks-2023/userAuthUpdate
Part 1 of many towards chat application
- Loading branch information
Showing
6 changed files
with
56 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,17 +4,19 @@ import 'package:flutter/material.dart'; | |
import 'package:google_sign_in/google_sign_in.dart'; | ||
import 'package:lost_found_steelhacks/authentication/user.dart'; | ||
import 'package:rxdart/rxdart.dart'; | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
|
||
class AuthService extends ChangeNotifier { | ||
//final GoogleSignIn _googleSignIn = GoogleSignIn(); | ||
final FirebaseAuth _auth = FirebaseAuth.instance; | ||
final FirebaseFirestore _firestore = FirebaseFirestore.instance; | ||
|
||
// create user obj based on FirebaseUser | ||
MyUser? _userFromFirebaseUser(User? user) { | ||
return user != null ? MyUser(uid: user.uid) : null; | ||
} | ||
|
||
bool isSignedIn() { | ||
bool isSignedIn() { | ||
return _auth.currentUser != null; | ||
} | ||
|
||
|
@@ -31,9 +33,14 @@ class AuthService extends ChangeNotifier { | |
email: _email, password: _password); | ||
User? user = result.user; | ||
|
||
//create a new document for the user with the uid | ||
// await DatabaseService(uid: user!.uid) | ||
// .updateUserData('0', 'New Username', _email, user!.uid); | ||
//create entry if firestore of user info | ||
_firestore.collection('users').doc(result.user!.uid).set({ | ||
'uid': result.user!.uid, | ||
'email': result.user!.email, | ||
'firstName': _firstName, | ||
'lastName': _lastName, | ||
'location': GeoPoint(40.4440279, -79.9700647) | ||
}); | ||
|
||
return _userFromFirebaseUser(user); | ||
} catch (e) { | ||
|
@@ -46,6 +53,13 @@ class AuthService extends ChangeNotifier { | |
Future signInWithEmailAndPassword(String _email, String _password) async { | ||
UserCredential result = await _auth.signInWithEmailAndPassword( | ||
email: _email, password: _password); | ||
|
||
_firestore.collection('users').doc(result.user!.uid).set({ | ||
'uid': result.user!.uid, | ||
'email': result.user!.email, | ||
'location': GeoPoint(40.4440279, -79.9700647) | ||
}, SetOptions(merge: true)); | ||
|
||
return _userFromFirebaseUser(result.user); | ||
} | ||
|
||
|
@@ -55,7 +69,6 @@ class AuthService extends ChangeNotifier { | |
try { | ||
if (kIsWeb) { | ||
GoogleAuthProvider googleProvider = GoogleAuthProvider(); | ||
|
||
// googleProvider.addScope('https://www.googleapis.com/auth/contacts.readonly'); | ||
// googleProvider.setCustomParameters({ | ||
// 'login_hint': '[email protected]' | ||
|
@@ -74,6 +87,25 @@ class AuthService extends ChangeNotifier { | |
} | ||
|
||
User? user = result.user; | ||
|
||
String? firstName; | ||
String? lastName; | ||
|
||
//this needed to add user info and splits via first and last name | ||
//works for GoogleAuth if we extend to other methods need to check more | ||
for (final providerProfile in user!.providerData) { | ||
String? name = providerProfile.displayName; | ||
firstName = name!.split(' ')[0]; | ||
lastName = name.split(' ')[1]; | ||
} | ||
|
||
_firestore.collection('users').doc(result.user!.uid).set({ | ||
'uid': result.user!.uid, | ||
'email': result.user!.email, | ||
'firstName' : firstName ?? '', | ||
'lastName': lastName ?? '', | ||
'location': GeoPoint(40.4440279, -79.9700647) | ||
}, SetOptions(merge: true)); | ||
return _userFromFirebaseUser(user); | ||
} on FirebaseAuthException catch (e) { | ||
return null; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.