This commit is contained in:
Dylan Aird
2020-09-19 22:47:20 +10:00
parent 8c2e3901da
commit 0b603816d6
8 changed files with 72 additions and 67 deletions

View File

@@ -1,10 +1,10 @@
class HttpException implements Exception {
final String message;
final Map<String, dynamic> errors;
HttpException(this.message);
HttpException(this.errors);
@override
String toString() {
return message;
return '';
}
}

View File

@@ -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<void> _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: <String, String>{
'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<String, dynamic>);
}
// 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<String, Object>;
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<void> 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() {}
}

View File

View File

@@ -128,19 +128,13 @@ class _AuthCardState extends State<AuthCard> {
} 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);
}

View File

@@ -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<HomeTabsScreen> {
@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<HomeTabsScreen> {
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,

View File

@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
class WorkoutsScreen extends StatelessWidget {
class ScheduleScreen extends StatelessWidget {
Widget getAppBar() {
return AppBar(
title: Text('Workouts'),

View File

@@ -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

View File

@@ -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.