From 0b603816d6b7c1c218c57701c6dedd96b7e60a98 Mon Sep 17 00:00:00 2001 From: Dylan Aird Date: Sat, 19 Sep 2020 22:47:20 +1000 Subject: [PATCH] fixes --- lib/models/http_exception.dart | 6 +- lib/providers/auth.dart | 99 ++++++++++--------- lib/providers/events.dart | 0 lib/screens/auth_screen.dart | 16 +-- lib/screens/home_tabs_screen.dart | 8 +- ...kouts_screen.dart => schedule_screen.dart} | 2 +- pubspec.lock | 7 ++ pubspec.yaml | 1 + 8 files changed, 72 insertions(+), 67 deletions(-) create mode 100644 lib/providers/events.dart rename lib/screens/{workouts_screen.dart => schedule_screen.dart} (88%) diff --git a/lib/models/http_exception.dart b/lib/models/http_exception.dart index d11795d70..24a5db74f 100644 --- a/lib/models/http_exception.dart +++ b/lib/models/http_exception.dart @@ -1,10 +1,10 @@ class HttpException implements Exception { - final String message; + final Map errors; - HttpException(this.message); + HttpException(this.errors); @override String toString() { - return message; + return ''; } } diff --git a/lib/providers/auth.dart b/lib/providers/auth.dart index ff4556fe3..f1216d5e5 100644 --- a/lib/providers/auth.dart +++ b/lib/providers/auth.dart @@ -8,66 +8,69 @@ import 'dart:async'; class Auth with ChangeNotifier { String _token; - DateTime _expiryDate; - String _userId; - Timer _authTimer; + // DateTime _expiryDate; + // String _userId; + // Timer _authTimer; bool get isAuth { - return true; - // return token != null; + // return true; + return token != null; } String get token { - if (_expiryDate != null && - _expiryDate.isAfter(DateTime.now()) && - _token != null) { - return _token; - } - return null; + // if (_expiryDate != null && + // _expiryDate.isAfter(DateTime.now()) && + // _token != null) { + return _token; + // } + // return null; } - String get userId { - return _userId; - } + // String get userId { + // return _userId; + // } Future _authenticate( String email, String password, String urlSegment) async { - var url = 'https://wger.de/api/v2/login'; + var url = 'https://wger.de/api/v2/login/'; print(email); print(password); try { final response = await http.post( url, + headers: { + 'Content-Type': 'application/json; charset=UTF-8', + }, body: json.encode({'email': email, 'password': password}), ); final responseData = json.decode(response.body); - + print(response.statusCode); print(responseData); - if (responseData['error'] != null) { - throw HttpException(responseData['error']['message']); + if (response.statusCode >= 400) { + throw HttpException(json.decode(responseData) as Map); } // Log user in - _token = responseData['idToken']; - _userId = responseData['localId']; - _expiryDate = DateTime.now().add( - Duration( - seconds: int.parse(responseData['expiresIn']), - ), - ); + _token = responseData['token']; + // _userId = responseData['localId']; + // _expiryDate = DateTime.now().add( + // Duration( + // seconds: int.parse(responseData['expiresIn']), + // ), + // ); - _autoLogout(); + // _autoLogout(); notifyListeners(); // store login data in shared preferences final prefs = await SharedPreferences.getInstance(); final userData = json.encode({ 'token': _token, - 'userId': _userId, - 'expiryDate': _expiryDate.toIso8601String(), + // 'userId': _userId, + // 'expiryDate': _expiryDate.toIso8601String(), }); prefs.setString('userData', userData); } catch (error) { @@ -92,42 +95,42 @@ class Auth with ChangeNotifier { final extractedUserData = json.decode(prefs.getString('userData')) as Map; - final expiryDate = DateTime.parse(extractedUserData['expiryDate']); + // final expiryDate = DateTime.parse(extractedUserData['expiryDate']); - if (expiryDate.isBefore(DateTime.now())) { - return false; - } + // if (expiryDate.isBefore(DateTime.now())) { + // return false; + // } _token = extractedUserData['token']; - _userId = extractedUserData['userId']; - _expiryDate = expiryDate; + // _userId = extractedUserData['userId']; + // _expiryDate = expiryDate; notifyListeners(); - _autoLogout(); + // _autoLogout(); return true; } Future logout() async { _token = null; - _userId = null; - _expiryDate = null; - if (_authTimer != null) { - _authTimer.cancel(); - _authTimer = null; - } + // _userId = null; + // _expiryDate = null; + // if (_authTimer != null) { + // _authTimer.cancel(); + // _authTimer = null; + // } notifyListeners(); final prefs = await SharedPreferences.getInstance(); prefs.clear(); } - void _autoLogout() { - if (_authTimer != null) { - _authTimer.cancel(); - } - final timeToExpiry = _expiryDate.difference(DateTime.now()).inSeconds; - _authTimer = Timer(Duration(seconds: timeToExpiry), logout); - } + // void _autoLogout() { + // if (_authTimer != null) { + // _authTimer.cancel(); + // } + // final timeToExpiry = _expiryDate.difference(DateTime.now()).inSeconds; + // _authTimer = Timer(Duration(seconds: timeToExpiry), logout); + // } void setLoginServer() {} } diff --git a/lib/providers/events.dart b/lib/providers/events.dart new file mode 100644 index 000000000..e69de29bb diff --git a/lib/screens/auth_screen.dart b/lib/screens/auth_screen.dart index 33d6ef187..aefa756bd 100644 --- a/lib/screens/auth_screen.dart +++ b/lib/screens/auth_screen.dart @@ -128,19 +128,13 @@ class _AuthCardState extends State { } on HttpException catch (error) { var errorMessage = 'Authentication Failed'; - if (error.toString().contains('EMAIL_EXISTS')) { - errorMessage = 'This email address is already in use'; - } else if (error.toString().contains('INVALID_EMAIL')) { - errorMessage = 'This is not a valid email address'; - } else if (error.toString().contains('WEAK_PASSWORD')) { - errorMessage = 'This password is too weak'; - } else if (error.toString().contains('EMAIL_NOT_FOUND')) { - errorMessage = 'Could not find a user with that email'; - } else if (error.toString().contains('INVALID_PASSWORD')) { - errorMessage = 'Your password is incorrect'; + if (error.errors.containsKey('username')) { + errorMessage = error.errors['username']; + } else if (error.errors.containsKey('password')) { + errorMessage = error.errors['password']; } _showErrorDialog(errorMessage); - } catch (error) { + } finally { var errorMessage = 'Could not authenticate you. Please try again later'; _showErrorDialog(errorMessage); } diff --git a/lib/screens/home_tabs_screen.dart b/lib/screens/home_tabs_screen.dart index 75322e7ba..096e3bd51 100644 --- a/lib/screens/home_tabs_screen.dart +++ b/lib/screens/home_tabs_screen.dart @@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:wger/screens/nutrition_screen.dart'; import 'package:wger/screens/weight_screen.dart'; -import 'package:wger/screens/workouts_screen.dart'; +import 'package:wger/screens/schedule_screen.dart'; class HomeTabsScreen extends StatefulWidget { @override @@ -15,7 +15,7 @@ class _HomeTabsScreenState extends State { @override initState() { _pages = [ - {'page': WorkoutsScreen(), 'title': 'Workouts'}, + {'page': ScheduleScreen(), 'title': 'Schedule'}, {'page': NutritionScreen(), 'title': 'Nutrition'}, {'page': WeightScreen(), 'title': 'Weight'}, ]; @@ -42,8 +42,8 @@ class _HomeTabsScreenState extends State { items: [ BottomNavigationBarItem( backgroundColor: Theme.of(context).primaryColor, - icon: Icon(Icons.directions_run), - title: Text('Workouts'), + icon: Icon(Icons.calendar_today), + title: Text('Schedule'), ), BottomNavigationBarItem( backgroundColor: Theme.of(context).primaryColor, diff --git a/lib/screens/workouts_screen.dart b/lib/screens/schedule_screen.dart similarity index 88% rename from lib/screens/workouts_screen.dart rename to lib/screens/schedule_screen.dart index e032cb5bb..361ae3d3a 100644 --- a/lib/screens/workouts_screen.dart +++ b/lib/screens/schedule_screen.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; -class WorkoutsScreen extends StatelessWidget { +class ScheduleScreen extends StatelessWidget { Widget getAppBar() { return AppBar( title: Text('Workouts'), diff --git a/pubspec.lock b/pubspec.lock index e7afb833d..c0927ba79 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -69,6 +69,13 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_calendar_carousel: + dependency: "direct main" + description: + name: flutter_calendar_carousel + url: "https://pub.dartlang.org" + source: hosted + version: "1.4.12" flutter_test: dependency: "direct dev" description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index 5e62ced68..9c0330aa9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -27,6 +27,7 @@ dependencies: intl: ^0.16.1 http: ^0.12.2 shared_preferences: ^0.5.10 + flutter_calendar_carousel: ^1.4.12 # The following adds the Cupertino Icons font to your application.