Przeglądaj źródła

files of David. I changed only AuthService file

iwa-42 3 lat temu
rodzic
commit
eb98ff161b

BIN
app/assets/communicate.png


BIN
app/assets/hello.png


BIN
app/assets/id-card.png


BIN
app/assets/teamlogo.png


BIN
app/assets/user.png


+ 57 - 0
app/lib/Services/AuthServiec.dart

@@ -0,0 +1,57 @@
+import 'package:cloud_firestore/cloud_firestore.dart';
+import 'package:firebase_auth/firebase_auth.dart';
+import 'package:physigo/Services/DatabaseManager.dart';
+
+class AuthenticationServices {
+  final FirebaseAuth _auth = FirebaseAuth.instance;
+  late User user;
+
+  //Register a user
+  Future createNewUser(String address, bool anonymous, String birth,
+      String name, String email, String password, String phone, String sharedID,
+      String surname) async {
+    try {
+      UserCredential result = await _auth.createUserWithEmailAndPassword(
+          email: email, password: password);
+      User? user = result.user;
+      await DatabaseManager("total_points").createUserData(
+          address, anonymous, birth, DateTime.now(), DateTime.now(), email,
+          name, phone, sharedID, surname, 0, DateTime.now(), 'null', user!.uid);
+
+      return user;
+    } catch (e) {
+      print(e.toString());
+    }
+  }
+
+  //LogIn with user
+  Future loginUser(String email, String password) async {
+    try {
+      UserCredential result = await _auth.signInWithEmailAndPassword(
+          email: email, password: password);
+      return result.user;
+    } catch(e) {
+      print(e.toString());
+    }
+  }
+
+
+  Future <dynamic> getCurrentUser() async{
+    User? user = _auth.currentUser;
+    var querySnapshot = await FirebaseFirestore.instance.collection('profileInfo')
+        .where('mail', isEqualTo: user?.email)
+        .get();
+    return querySnapshot;
+  }
+
+
+  Stream<int> getPlace(dynamic user) async*{
+    var users = await FirebaseFirestore.instance.collection('Users').
+    where("total_points", isGreaterThan: user["total_points"])
+        .get();
+    yield users.docs.length + 1;
+  }
+
+
+
+}

+ 42 - 0
app/lib/Services/DatabaseManager.dart

@@ -0,0 +1,42 @@
+import 'package:cloud_firestore/cloud_firestore.dart';
+
+class DatabaseManager {
+  late final String points;
+  final CollectionReference profileList =
+  FirebaseFirestore.instance.collection('profileInfo');
+  CollectionReference usersList =
+  FirebaseFirestore.instance.collection("Users");
+
+  DatabaseManager(this.points);
+
+  Future<void> createUserData(String address,
+      bool anonymous,
+      String birth_date,
+      DateTime create_date,
+      DateTime lastChallengeDate,
+      String mail,
+      String name,
+      String phoneNumber,
+      String shared_id,
+      String surname,
+      int totalpoints,
+      DateTime updated_date,
+      String weeklyplace,
+      String uid) async {
+    return await profileList.doc(uid).set({
+      'address': address,
+      'anonymous': anonymous,
+      'birth_date': birth_date,
+      'created_date': create_date,
+      'last_challenge_date': lastChallengeDate,
+      'mail': mail,
+      'name': name,
+      'phone_number': phoneNumber,
+      'shared_id': shared_id,
+      'surname': surname,
+      'total_points': totalpoints,
+      'updated_date': updated_date,
+      'weekly_place': weeklyplace,
+    });
+  }
+}

+ 341 - 0
app/lib/SignupPage.dart

@@ -0,0 +1,341 @@
+import 'package:flutter/material.dart';
+import 'package:physigo/Services/AuthServiec.dart';
+
+
+//Fields..
+TextEditingController _name = TextEditingController();
+TextEditingController _surname = TextEditingController();
+TextEditingController _mail = TextEditingController();
+TextEditingController _phoneNumber = TextEditingController();
+TextEditingController _address = TextEditingController();
+TextEditingController _username = TextEditingController();
+TextEditingController _passwd = TextEditingController();
+TextEditingController _passwd2 = TextEditingController();
+
+bool anonymous = false;
+String? _dateString;
+
+final AuthenticationServices _auth = AuthenticationServices();
+
+
+class SignupPage extends StatefulWidget {
+  const SignupPage({Key? key}) : super(key: key);
+
+  @override
+  State<SignupPage> createState() => _SignupPageState();
+}
+
+class _SignupPageState extends State<SignupPage> {
+  int _pageIndex = 0;
+
+  List<Widget> forms = [
+    SignupForm1(
+      title: '',
+    ),
+    SignupForm2(
+      title: '',
+    ),
+    SignupForm3(
+      title: '',
+    )
+  ];
+
+  void _updatePageIndex(int newPageIndex) {
+    if (newPageIndex == -1) {
+      Navigator.pushNamed(context, '/');
+    } else if (newPageIndex == 3) {
+      //autentification
+      //Navigator.pop(context);
+      newPageIndex == 2;
+    } else {
+      setState(() {
+        _pageIndex = newPageIndex;
+      });
+    }
+  }
+
+  @override
+  Widget build(BuildContext context) {
+    return Scaffold(
+      floatingActionButton: Row(
+        mainAxisAlignment: MainAxisAlignment.end,
+        children: [
+          FloatingActionButton(
+              heroTag: "btn1",
+              child: Icon(Icons.navigate_before_outlined),
+              onPressed: () => _updatePageIndex(_pageIndex - 1)),
+          SizedBox(
+            width: 25,
+          ),
+          FloatingActionButton(
+              heroTag: "btn2",
+              child: Icon(Icons.navigate_next_outlined),
+              onPressed: () => _updatePageIndex(_pageIndex + 1))
+        ],
+      ),
+      body: Center(child: forms[_pageIndex]),
+    );
+  }
+}
+
+class SignupForm1 extends StatefulWidget {
+  const SignupForm1({Key? key, required this.title}) : super(key: key);
+  final String title;
+
+  @override
+  //_RegisterScreen createState() => _RegisterScreen();
+  State<SignupForm1> createState() => _SignupForm1();
+}
+
+class _SignupForm1 extends State<SignupForm1> {
+  DateTime? _dateTime;
+
+  @override
+  Widget build(BuildContext context) {
+    return Scaffold(
+      appBar: AppBar(
+        title: const Text('Register Screen'),
+      ),
+      body: Center(
+          child: SizedBox(
+        width: 300,
+        child: Column(children: <Widget>[
+          const SizedBox(height: 30),
+          Text(
+            'Personal Information',
+            style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
+          ),
+          const SizedBox(height: 30),
+          Image.asset(
+            'assets/user.png',
+            width: 150,
+          ),
+          const SizedBox(height: 30),
+          TextFormField(
+            controller: _name,
+            decoration: InputDecoration(
+              hintText: 'Name',
+            ),
+          ),
+          const SizedBox(height: 10),
+          TextFormField(
+            controller: _surname,
+            decoration: InputDecoration(
+              hintText: 'Surname',
+            ),
+          ),
+          const SizedBox(height: 10),
+          Row(children: <Widget>[
+            Text(_dateString == null
+                ? 'Select birthday date :'
+                : _dateString.toString()),
+            const SizedBox(width: 49),
+            ElevatedButton(
+              onPressed: () {
+                showDatePicker(
+                        context: context,
+                        initialDate: DateTime.now(),
+                        firstDate: DateTime(1900),
+                        lastDate: DateTime.now())
+                    .then((date) {
+                  setState(() {
+                    _dateTime = date!;
+                    _dateString = "${date.day}-${date.month}-${date.year}";
+                  });
+                });
+              },
+              child: const Text('Select date'),
+            ),
+          ]),
+          const SizedBox(height: 10),
+        ]),
+      )),
+    );
+  }
+}
+
+class SignupForm2 extends StatefulWidget {
+  const SignupForm2({Key? key, required this.title}) : super(key: key);
+  final String title;
+
+  @override
+  //_RegisterScreen createState() => _RegisterScreen();
+  State<SignupForm2> createState() => _SignupForm2();
+}
+
+class _SignupForm2 extends State<SignupForm2> {
+  @override
+  Widget build(BuildContext context) {
+    return Scaffold(
+      appBar: AppBar(
+        title: const Text('Register Screen'),
+      ),
+      body: Center(
+          child: SizedBox(
+        width: 300,
+        child: Column(children: <Widget>[
+          const SizedBox(height: 30),
+          Text(
+            'Contact Details',
+            style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
+          ),
+          const SizedBox(height: 30),
+          Image.asset(
+            'assets/communicate.png',
+            width: 150,
+          ),
+          const SizedBox(height: 30),
+          TextFormField(
+            controller: _mail,
+            decoration: InputDecoration(
+              hintText: 'Mail',
+            ),
+          ),
+          const SizedBox(height: 10),
+          TextFormField(
+            controller: _phoneNumber,
+            decoration: InputDecoration(
+              hintText: 'Phone Number',
+            ),
+          ),
+          const SizedBox(height: 10),
+          TextFormField(
+            controller: _address,
+            decoration: InputDecoration(
+              hintText: 'Home Adress..',
+            ),
+          ),
+          const SizedBox(height: 10),
+          Row(children: <Widget>[
+            Text('Anonymous'),
+            Checkbox(
+              checkColor: Colors.white,
+              value: anonymous,
+              onChanged: (bool? value) {
+                setState(() {
+                  anonymous = value!;
+                });
+              },
+            ),
+          ]),
+          const SizedBox(height: 10),
+        ]),
+      )),
+    );
+  }
+}
+
+class SignupForm3 extends StatefulWidget {
+  const SignupForm3({Key? key, required this.title}) : super(key: key);
+  final String title;
+
+  @override
+  //_RegisterScreen createState() => _RegisterScreen();
+  State<SignupForm3> createState() => _SignupForm3();
+}
+
+class _SignupForm3 extends State<SignupForm3> {
+  @override
+  Widget build(BuildContext context) {
+    return Scaffold(
+      appBar: AppBar(
+        title: const Text('Register Screen'),
+      ),
+      body: Center(
+          child: SizedBox(
+        width: 300,
+        child: Column(children: <Widget>[
+          const SizedBox(height: 30),
+          Text(
+            'User details',
+            style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
+          ),
+          const SizedBox(height: 30),
+          Image.asset(
+            'assets/id-card.png',
+            width: 150,
+          ),
+          const SizedBox(height: 30),
+          TextFormField(
+            controller: _username,
+            decoration: InputDecoration(
+              hintText: 'Username',
+            ),
+          ),
+          const SizedBox(height: 10),
+          TextFormField(
+            controller: _passwd,
+            obscureText: true,
+            enableSuggestions: false,
+            autocorrect: false,
+            decoration: InputDecoration(
+              hintText: 'Password',
+            ),
+          ),
+          const SizedBox(height: 10),
+          TextFormField(
+            controller: _passwd2,
+            obscureText: true,
+            enableSuggestions: false,
+            autocorrect: false,
+            decoration: InputDecoration(
+              hintText: 'Repeat password',
+            ),
+          ),
+          const SizedBox(height: 15),
+          ElevatedButton(
+              onPressed: () {
+                try {
+                  if (_passwd.text == _passwd2.text) {
+                    createUser();
+                    Navigator.pop(context);
+                  } else {
+                    print("passwd is not the same");
+                  }
+                } catch (e) {
+                  print(e.toString());
+                }
+              },
+              child: const Text('Finish Register')),
+
+        ]),
+      )),
+    );
+  }
+
+  void createUser() async {
+    dynamic result =
+        await _auth.createNewUser(_address.text, anonymous, _dateString.toString(), _name.text, _mail.text, _passwd.text,
+            _phoneNumber.text, _username.text, _surname.text);
+    if (result == null) {
+      print('mail not valid');
+    } else {
+      //print(result.toString());
+      //[51.787378° N, 19.449455° E]
+
+      print('*****  address: ' + _address.text +
+          ' anonymous: ' + anonymous.toString() +
+          ' birthdate: ' + _dateString.toString() +
+          ' createDate: ' + DateTime.now().toString()+
+          ' last_challenge_data: ' + 'null'+
+          ' mail: ' + _mail.text +
+          ' name: ' + _name.text +
+          ' passwd: ' + '??? ' +
+          ' phone_number: ' + _phoneNumber.text+
+          ' shared_id: ' + _username.text +
+          ' surname: ' + _surname.text +
+          ' totalpoints: 0 ' +
+          ' update_date: ' + DateTime.now().toString()+
+          ' weeklyplace: ' + 'null');
+
+      _name.clear();
+      _surname.clear();
+      _mail.clear();
+      _phoneNumber.clear();
+      _address.clear();
+      _username.clear();
+      _passwd.clear();
+      _passwd2.clear();
+    }
+  }
+}

+ 80 - 0
app/lib/logIn.dart

@@ -0,0 +1,80 @@
+import 'package:flutter/material.dart';
+import 'Services/AuthServiec.dart';
+
+class LogIn extends StatefulWidget {
+  const LogIn({Key? key, required this.title}) : super(key: key);
+  final String title;
+
+  @override
+  //_RegisterScreen createState() => _RegisterScreen();
+  State<LogIn> createState() => _LogIn();
+}
+
+class _LogIn extends State<LogIn> {
+  TextEditingController _email = TextEditingController();
+  TextEditingController _passwd = TextEditingController();
+  final AuthenticationServices _auth = AuthenticationServices();
+
+  @override
+  Widget build(BuildContext context) {
+    return Scaffold(
+      appBar: AppBar(
+        title: const Text('Login Screen'),
+      ),
+      body: Center(
+          child: SizedBox(
+        width: 300,
+        child: Column(children: <Widget>[
+          const SizedBox(height: 30),
+          Text(
+            'Welcome back!',
+            style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
+          ),
+          const SizedBox(height: 30),
+          Image.asset(
+            'assets/hello.png',
+            width: 150,
+          ),
+          const SizedBox(height: 30),
+          TextFormField(
+            controller: _email,
+            decoration: InputDecoration(
+              hintText: 'Mail',
+            ),
+          ),
+          const SizedBox(height: 10),
+          TextFormField(
+            controller: _passwd,
+            obscureText: true,
+            enableSuggestions: false,
+            autocorrect: false,
+            decoration: InputDecoration(
+              hintText: 'Password',
+            ),
+          ),
+          const SizedBox(height: 10),
+          ElevatedButton(
+            onPressed: () {
+              signInUser();
+            },
+            child: const Text('Log In'),
+          ),
+        ]),
+      )),
+    );
+  }
+
+  void signInUser() async {
+    dynamic authResult = await _auth.loginUser(_email.text, _passwd.text);
+    if (authResult == null) {
+      print("log in error");
+    } else {
+      _email.clear();
+      _passwd.clear();
+      print("Succesful login ");
+      Navigator.pushNamed(context, '/mainPage',
+        arguments: {'uid': authResult.uid},
+      );
+    }
+  }
+}

+ 40 - 0
app/lib/mainPage.dart

@@ -0,0 +1,40 @@
+import 'package:flutter/material.dart';
+
+class MainPage extends StatefulWidget {
+  const MainPage({Key? key}) : super(key: key);
+  @override
+  State<MainPage> createState() => _MainPage();
+}
+class _MainPage extends State<MainPage> {
+
+
+  @override
+  Widget build(BuildContext context) {
+    final arguments = (ModalRoute.of(context)?.settings.arguments ?? <String, dynamic>{}) as Map;
+    final String id = arguments['uid'];
+    print(id);
+
+    return Scaffold(
+      appBar: AppBar(
+
+        title: const Text('Welcome Back!'),
+        leading: GestureDetector(
+          onTap: () {
+            print('going to profile page');
+            Navigator.pushNamed(context, '/profilePage',
+              arguments: {'id': id},
+            );
+            },
+          child: Icon(
+            Icons.account_circle_rounded,  // add custom icons also
+          ),
+        ),
+      ),
+      body: Center(
+        child: Column(
+        ),
+
+      ),
+    );
+  }
+}

+ 101 - 0
app/lib/profilePage.dart

@@ -0,0 +1,101 @@
+import 'package:cloud_firestore/cloud_firestore.dart';
+import 'package:flutter/material.dart';
+
+class ProfilePage extends StatefulWidget {
+  const ProfilePage({Key? key, required this.title}) : super(key: key);
+  final String title;
+
+  @override
+  //_RegisterScreen createState() => _RegisterScreen();
+  State<ProfilePage> createState() => _ProfilePage();
+}
+
+class _ProfilePage extends State<ProfilePage> {
+  String name = '', surname = '', birth_date = '', mail = '', shared_id = '',
+      phone_number = '', anonymous = '', address = '';
+
+  @override
+  Widget build(BuildContext context) {
+    final arguments = (ModalRoute.of(context)?.settings.arguments ??
+        <String, dynamic>{}) as Map;
+    print('******' + arguments['id']);
+
+    //getting info of the user one by one
+    FirebaseFirestore.instance.collection('profileInfo').doc(arguments['id'])
+        .get().then((DocumentSnapshot) {
+      name = DocumentSnapshot.get('name').toString();
+    });
+    FirebaseFirestore.instance.collection('profileInfo').doc(arguments['id'])
+        .get().then((DocumentSnapshot) {
+      surname = DocumentSnapshot.get('surname').toString();
+    });
+    FirebaseFirestore.instance.collection('profileInfo').doc(arguments['id'])
+        .get().then((DocumentSnapshot) {
+      birth_date = DocumentSnapshot.get('birth_date').toString();
+    });
+    FirebaseFirestore.instance.collection('profileInfo').doc(arguments['id'])
+        .get().then((DocumentSnapshot) {
+      mail = DocumentSnapshot.get('mail').toString();
+    });
+    FirebaseFirestore.instance.collection('profileInfo').doc(arguments['id'])
+        .get().then((DocumentSnapshot) {
+      shared_id = DocumentSnapshot.get('shared_id').toString();
+    });
+    FirebaseFirestore.instance.collection('profileInfo').doc(arguments['id'])
+        .get().then((DocumentSnapshot) {
+      phone_number = DocumentSnapshot.get('phone_number').toString();
+    });
+    FirebaseFirestore.instance.collection('profileInfo').doc(arguments['id'])
+        .get().then((DocumentSnapshot) {
+      anonymous = DocumentSnapshot.get('anonymous').toString();
+    });
+    FirebaseFirestore.instance.collection('profileInfo').doc(arguments['id'])
+        .get().then((DocumentSnapshot) {
+      address = DocumentSnapshot.get('address').toString();
+    });
+
+    return Scaffold(
+      appBar: AppBar(title: const Text('Profile Page')),
+      body: Center(
+        child: SizedBox(
+          width: 300,
+          child: Column(
+            children: [
+              const SizedBox(height: 30),
+              Text(
+                'Personal Information',
+                style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
+              ),
+              const SizedBox(height: 30),
+              Image.asset(
+                'assets/user.png',
+                width: 150,
+              ),
+              const SizedBox(height: 30),
+              Text('Name: ' + name + ' ' + surname),
+              const SizedBox(height: 10),
+              Text('Birth Date: ' + birth_date),
+              const SizedBox(height: 10),
+              Text('Username: ' + shared_id),
+              const SizedBox(height: 10),
+              Text('Mail: ' + mail),
+              const SizedBox(height: 10),
+              Text('Phone: ' + phone_number),
+              const SizedBox(height: 10),
+              Text('Address: ' + address),
+              const SizedBox(height: 10),
+              Text('Anonymous: ' + anonymous),
+              const SizedBox(height: 30),
+              ElevatedButton(
+                onPressed: () {
+                  setState(() {});
+                },
+                child: const Text('Refresh'),
+              ),
+            ],
+          ),
+        ),
+      ),
+    );
+  }
+}

+ 48 - 0
app/lib/welcomeScreen.dart

@@ -0,0 +1,48 @@
+
+import 'package:flutter/material.dart';
+import 'package:flutter/widgets.dart';
+
+class MyHomePage extends StatefulWidget {
+  const MyHomePage({Key? key, required this.title}) : super(key: key);
+  final String title;
+
+  @override
+  State<MyHomePage> createState() => _MyHomePageState();
+}
+
+class _MyHomePageState extends State<MyHomePage> {
+  @override
+  Widget build(BuildContext context) {
+    return Scaffold(
+      appBar: AppBar(
+        title: const Text('Welcome to PhysiGo'),
+      ),
+      body: Center(
+        child: Column(
+          children: <Widget>[
+            const SizedBox(height: 30),
+            Image.asset(
+              'assets/teamlogo.png',
+              width: 260,
+            ),
+            const SizedBox(height: 30),
+            ElevatedButton(
+              // Within the `FirstScreen` widget
+              onPressed: () {
+                // Navigate to the second screen using a named route.
+                Navigator.pushNamed(context, '/register');
+              },
+              child: Text('Register'),
+            ),
+            ElevatedButton(
+              onPressed: () {
+                Navigator.pushNamed(context, '/login');
+              },
+              child: const Text('Log In'),
+            )
+          ],
+        ),
+      ),
+    );
+  }
+}