Skip to content

Commit

Permalink
Merge pull request #41 from Steelhacks-2023/userAuthUpdate
Browse files Browse the repository at this point in the history
Part 1 of many towards chat application
  • Loading branch information
tbeidlershenk authored Dec 22, 2023
2 parents 12717af + a51d770 commit 77da54b
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 28 deletions.
42 changes: 37 additions & 5 deletions lib/authentication/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) {
Expand All @@ -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);
}

Expand All @@ -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]'
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions lib/data/item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Item {
final String? itemName;
final String? description;
final num? phone;
final int? picture;
final String? picture;
final GeoPoint? location;

Item(
Expand All @@ -20,12 +20,13 @@ class Item {
SnapshotOptions? options,
) {
Map<String, dynamic> data = snapshot.data() as Map<String, dynamic>;

return Item(
itemName: data['ItemName'],
description: data['Description'],
location: data['Location'],
phone: data['Phone'],
picture: data['Picture']);
picture: data['Picture'] is int ? '' : data['Picture']); //because some pictures entries are of numbers
}

Map<String, dynamic> toFirestore() {
Expand Down
1 change: 1 addition & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class _MyAppState extends State<MyApp> {
ChangeNotifierProvider<AuthService>(create: (_) => AuthService()),
],
child: MaterialApp(
title: 'Lost@Pitt | For Students By Students',
debugShowCheckedModeBanner: false,
routes: {
'loginPage': (context) => const LoginPage(),
Expand Down
6 changes: 4 additions & 2 deletions lib/pages/item_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ String _imgFromDeviceError = '';
Uint8List? imgBytesToFirebase;
String _imgName = '';

const List<String> list = <String>[
List<String> list = <String>[
'Water Bottle',
'ID',
'Wallet',
Expand Down Expand Up @@ -119,6 +119,7 @@ class _ItemRequestState extends State<ItemRequest> {
//adding to firebase collection
.add({
//Data added in the form of a dictionary into the document.
'Date Created': Timestamp.now(),
'Description': description,
'ItemName': category,
'Location': GeoPoint(lat, long),
Expand All @@ -133,6 +134,7 @@ class _ItemRequestState extends State<ItemRequest> {
//adding to firebase collection
.add({
//Data added in the form of a dictionary into the document.
'Date Created': Timestamp.now(),
'Description': description,
'ItemName': category,
'Location': GeoPoint(lat, long),
Expand All @@ -146,7 +148,7 @@ class _ItemRequestState extends State<ItemRequest> {
final AppTheme theme = Theme.of(context).extension<AppTheme>()!;
lat = widget.itemLoc.latitude;
long = widget.itemLoc.longitude;

list.sort((a, b) => a.compareTo(b));
return Padding(
padding: const EdgeInsets.all(30.0),
child: SizedBox(
Expand Down
30 changes: 11 additions & 19 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ packages:
dependency: transitive
description:
name: collection
sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687
sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c"
url: "https://pub.dev"
source: hosted
version: "1.17.2"
version: "1.17.1"
convert:
dependency: transitive
description:
Expand Down Expand Up @@ -580,18 +580,18 @@ packages:
dependency: transitive
description:
name: matcher
sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e"
sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb"
url: "https://pub.dev"
source: hosted
version: "0.12.16"
version: "0.12.15"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724
url: "https://pub.dev"
source: hosted
version: "0.5.0"
version: "0.2.0"
meta:
dependency: transitive
description:
Expand Down Expand Up @@ -849,10 +849,10 @@ packages:
dependency: transitive
description:
name: source_span
sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c"
sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250
url: "https://pub.dev"
source: hosted
version: "1.10.0"
version: "1.9.1"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -897,10 +897,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8"
sha256: eb6ac1540b26de412b3403a163d919ba86f6a973fe6cc50ae3541b80092fdcfb
url: "https://pub.dev"
source: hosted
version: "0.6.0"
version: "0.5.1"
typed_data:
dependency: transitive
description:
Expand Down Expand Up @@ -997,14 +997,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.0"
web:
dependency: transitive
description:
name: web
sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10
url: "https://pub.dev"
source: hosted
version: "0.1.4-beta"
win32:
dependency: transitive
description:
Expand Down Expand Up @@ -1038,5 +1030,5 @@ packages:
source: hosted
version: "3.1.2"
sdks:
dart: ">=3.1.0-185.0.dev <4.0.0"
dart: ">=3.0.0 <4.0.0"
flutter: ">=3.10.0"
Binary file modified web/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 77da54b

Please sign in to comment.