workouttest_app/lib/model/auth.dart
2020-08-17 12:38:47 +02:00

140 lines
4.3 KiB
Dart

import 'package:aitrainer_app/model/customer.dart';
import 'package:aitrainer_app/model/exercise_tree.dart';
import 'package:aitrainer_app/service/exercise_tree_service.dart';
import 'package:aitrainer_app/service/exercisetype_service.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:aitrainer_app/model/exercise_type.dart';
enum SharePrefsChange {
login,
registration,
logout,
}
/*
Auth flow of the app
1. During the login screen the authentication will be executed
- if not successful: message: Network error, try again later
- if successful
- get the stored shared preferences and customer id
- if customer_id not present -> registration page
- if present, check if the expiration_date > 10 days -> login page
- else get the API customer by the stored customer_id
- After registration / login store the preferences:
- AuthToken
- customer_id
- last_store_date
- is_registered
- is_logged_in
*/
class Auth {
static final Auth _singleton = Auth._internal();
// Keys to store and fetch data from SharedPreferences
static final String authTokenKey = 'auth_token';
static final String customerIdKey = 'customer_id';
static final String lastStoreDateKey = 'last_date';
static final String isRegisteredKey = 'is_registered';
static final String isLoggedInKey = 'is_logged_in';
static final String baseUrl = 'http://aitrainer.info:8888/api/';
static final String mediaUrl = 'https://aitrainer.info:4343/media/';
static final String username = 'bosi';
static final String password = 'andio2009';
String authToken = "";
Customer userLoggedIn;
bool firstLoad = true;
List<ExerciseType> _exerciseTypes;
List<ExerciseTree> _exerciseTree;
List deviceLanguages;
String startPage;
factory Auth() {
return _singleton;
}
Auth._internal();
String getAuthToken() {
return this.authToken;
}
static String getToken(SharedPreferences prefs) {
return prefs.getString(authTokenKey);
}
static String getBaseUrl() {
return baseUrl;
}
static String getMediaUrl() {
return mediaUrl;
}
afterRegistration(Customer customer) {
Future<SharedPreferences> prefs = SharedPreferences.getInstance();
userLoggedIn = customer;
setPreferences(prefs, SharePrefsChange.registration, customer.customerId);
}
afterLogin(Customer customer) async {
Future<SharedPreferences> prefs = SharedPreferences.getInstance();
userLoggedIn = customer;
await setPreferences(prefs, SharePrefsChange.login, customer.customerId);
}
logout() async {
userLoggedIn = null;
authToken = "";
//firstLoad = true;
Future<SharedPreferences> prefs = SharedPreferences.getInstance();
await setPreferences(prefs, SharePrefsChange.logout, 0);
}
setPreferences(Future<SharedPreferences> prefs,
SharePrefsChange type,
int customerId) async {
SharedPreferences sharedPreferences;
sharedPreferences = await prefs;
DateTime now = DateTime.now();
sharedPreferences.setString(Auth.lastStoreDateKey, now.toString());
if ( type == SharePrefsChange.registration ) {
Auth().startPage = "home";
sharedPreferences.setInt(Auth.customerIdKey, customerId);
sharedPreferences.setBool(Auth.isRegisteredKey, true);
sharedPreferences.setBool(Auth.isLoggedInKey, true);
await ExerciseTypeApi().getExerciseTypes();
await ExerciseTreeApi().getExerciseTree();
} else if ( type == SharePrefsChange.login ) {
Auth().startPage = "home";
sharedPreferences.setInt(Auth.customerIdKey, customerId);
sharedPreferences.setBool(Auth.isLoggedInKey, true);
await ExerciseTypeApi().getExerciseTypes();
await ExerciseTreeApi().getExerciseTree();
} else if ( type == SharePrefsChange.logout ) {
sharedPreferences.setBool(Auth.isLoggedInKey, false);
sharedPreferences.setInt(Auth.customerIdKey, 0);
sharedPreferences.setString(authTokenKey, "");
}
}
void setExerciseTypes( List<ExerciseType> exerciseTypes) {
this._exerciseTypes = exerciseTypes;
}
void setExerciseTree( List<ExerciseTree> exerciseTree) {
this._exerciseTree = exerciseTree;
}
List<ExerciseType> getExerciseTypes() {
return this._exerciseTypes;
}
List<ExerciseTree> getExerciseTree() {
return this._exerciseTree;
}
}