399 lines
15 KiB
Dart
399 lines
15 KiB
Dart
import 'dart:html';
|
|
|
|
import 'package:auto_size_text/auto_size_text.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:manager_app/Components/common_loader.dart';
|
|
import 'package:manager_app/Components/message_notification.dart';
|
|
import 'package:manager_app/Components/rounded_button.dart';
|
|
import 'package:manager_app/Components/rounded_input_field.dart';
|
|
import 'package:manager_app/Components/rounded_password_field.dart';
|
|
import 'package:manager_app/Models/managerContext.dart';
|
|
import 'package:manager_app/Models/session.dart';
|
|
import 'package:manager_app/Screens/Main/main_screen.dart';
|
|
import 'package:manager_app/app_context.dart';
|
|
import 'package:manager_app/client.dart';
|
|
import 'package:manager_app/constants.dart';
|
|
import 'package:manager_api_new/api.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:flutter/foundation.dart' show kIsWeb;
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
|
|
|
class LoginScreen extends StatefulWidget {
|
|
final Session? session;
|
|
LoginScreen({Key? key, this.session}) : super(key: key);
|
|
|
|
@override
|
|
_LoginScreenState createState() => _LoginScreenState();
|
|
}
|
|
|
|
class _LoginScreenState extends State<LoginScreen> {
|
|
String email = ""; // DEV "test@email.be"
|
|
String password = ""; // DEV = "kljqsdkljqsd"
|
|
String? host = "http://localhost:5000"; // "https://api.myinfomate.be" // "https://api.mymuseum.be" // DEV = "http://192.168.31.96" // http://localhost:5000 // https://api.mymuseum.be // myCore http://192.168.31.140:8089
|
|
Client? clientAPI;
|
|
bool isLoading = false;
|
|
bool isRememberMe = false;
|
|
String pageTitle = "MyInfoMate";
|
|
String? token;
|
|
String? instanceId;
|
|
String? pinCode;
|
|
Storage localStorage = window.localStorage;
|
|
|
|
void authenticateTRY(AppContext appContext, bool fromClick) async {
|
|
//print("try auth.. ");
|
|
|
|
/*this.host = "http://localhost:5000";
|
|
this.email = "test@email.be";
|
|
this.password = "kljqsdkljqsd";*/
|
|
|
|
clientAPI = Client(this.host!);
|
|
|
|
if (this.password != null || this.token != null) {
|
|
|
|
// if () {} // Add if token exist and not null + not expired
|
|
try {
|
|
if(fromClick) {
|
|
setState(() {
|
|
isLoading = true;
|
|
});
|
|
}
|
|
/*print(email);
|
|
print(password);*/
|
|
|
|
var accessToken = this.token;
|
|
var instanceId = this.instanceId;
|
|
var pinCode = this.pinCode;
|
|
/*print("accessToken");
|
|
print(accessToken);*/
|
|
if(accessToken == null) {
|
|
LoginDTO loginDTO = new LoginDTO(email: email, password: password);
|
|
/*print(email);
|
|
print(password);*/
|
|
|
|
TokenDTO? token = await clientAPI!.authenticationApi!
|
|
.authenticationAuthenticateWithJson(loginDTO);
|
|
|
|
/*print("TOKENNN");
|
|
print(token);*/
|
|
|
|
if(token != null) {
|
|
accessToken = token.accessToken!;
|
|
instanceId = token.instanceId!;
|
|
pinCode = token.pinCode;
|
|
|
|
showNotification(
|
|
kSuccess, kWhite, 'Connexion réussie', context, null);
|
|
|
|
if (isRememberMe) {
|
|
if (!localStorage.containsKey("remember")) {
|
|
localStorage.addEntries({"remember": "true"}.entries);
|
|
}
|
|
|
|
if (!localStorage.containsKey("email") &&
|
|
!localStorage.containsKey("token")) {
|
|
localStorage.addEntries({"email": email}.entries);
|
|
localStorage.addEntries({"token": token.accessToken!}.entries);
|
|
localStorage.addEntries({"instanceId": token.instanceId!}.entries);
|
|
if(token.pinCode != null) {
|
|
localStorage.addEntries({"pinCode": token.pinCode!.toString()}.entries);
|
|
}
|
|
}
|
|
} else {
|
|
localStorage.clear();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
if(accessToken != null) {
|
|
ManagerAppContext? managerAppContext = appContext.getContext();
|
|
// Set the appContext
|
|
if (managerAppContext == null) {
|
|
managerAppContext = new ManagerAppContext();
|
|
}
|
|
|
|
// store user info locally
|
|
managerAppContext.email = email;
|
|
managerAppContext.host = host;
|
|
managerAppContext.instanceId = instanceId;
|
|
managerAppContext.pinCode = pinCode;
|
|
managerAppContext.accessToken = accessToken;
|
|
managerAppContext.clientAPI = clientAPI;
|
|
setAccessToken(accessToken);
|
|
appContext.setContext(managerAppContext);
|
|
|
|
if(fromClick) {
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
}
|
|
Navigator.pushNamedAndRemoveUntil(
|
|
context,
|
|
'/main', // <- correspond à ta route définie
|
|
(Route<dynamic> route) => false,
|
|
);
|
|
/*Navigator.pushAndRemoveUntil(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) {
|
|
return MainScreen();
|
|
},
|
|
),
|
|
(Route<dynamic> route) => false // For pushAndRemoveUntil
|
|
);*/
|
|
} else {
|
|
showNotification(Colors.orange, kWhite, 'Un problème est survenu lors de la connexion', context, null);
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
catch (e) {
|
|
print("error auth");
|
|
print(e);
|
|
if(fromClick) {
|
|
showNotification(Colors.orange, kWhite, 'Un problème est survenu lors de la connexion', context, null);
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void setAccessToken(String accessToken) {
|
|
//clientAPI.resourceApi.apiClient.addDefaultHeader('authorization', 'Bearer '+accessToken);
|
|
clientAPI!.apiApi!.addDefaultHeader('authorization', 'Bearer '+accessToken);
|
|
//clientAPI.resourceApi.addDefaultHeader('Bearer', accessToken);
|
|
//clientAPI.apiApi.authentication.applyToParams([], Map.from({'Bearer': accessToken}));
|
|
|
|
/*clientAPI.apiApi.authentications.forEach((key, auth) {
|
|
if (auth is OAuth) {
|
|
auth.accessToken = accessToken;
|
|
}
|
|
});*/
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
//this.isRememberMe = widget.session.rememberMe;
|
|
this.host = "http://localhost:5000"; // "https://api.myinfomate.be"// "https://api.mymuseum.be" // "http://localhost:5000" //widget.session.host; // MDLF "http://192.168.1.19:8089" // "https://api.mymuseum.be"
|
|
//this.email = "test@email.be"; //widget.session.email;
|
|
//this.password = "kljqsdkljqsd"; //widget.session.password;
|
|
|
|
if(localStorage.containsKey("remember")) {
|
|
this.isRememberMe = true;
|
|
if(localStorage.containsKey("email")) {
|
|
this.email = localStorage.entries.where((element) => element.key == "email").first.value;
|
|
}
|
|
if(localStorage.containsKey("token")) {
|
|
this.token = localStorage.entries.where((element) => element.key == "token").first.value;
|
|
this.password = "AnythingAsWeAlreadyHaveAccessToken";
|
|
}
|
|
if(localStorage.containsKey("instanceId")) {
|
|
this.instanceId = localStorage.entries.where((element) => element.key == "instanceId").first.value;
|
|
}
|
|
if(localStorage.containsKey("pinCode")) {
|
|
this.pinCode = localStorage.entries.where((element) => element.key == "pinCode").first.value;
|
|
}
|
|
}
|
|
super.initState();
|
|
}
|
|
|
|
ManagerAppContext initInstance(ManagerAppContext managerAppContext) {
|
|
var url = window.location.href;
|
|
if(!url.contains("localhost")) {
|
|
var urlParts = url.split('.');
|
|
if(urlParts.length > 0) {
|
|
var subdomain = urlParts[0].replaceAll('https://', '');
|
|
switch(subdomain) {
|
|
case "manager":
|
|
this.pageTitle = "MyInfoMate";
|
|
break;
|
|
case "fortsaintheribert":
|
|
this.pageTitle = "Fort de Saint Héribert";
|
|
break;
|
|
}
|
|
} else {
|
|
print("subdomain not found");
|
|
}
|
|
} else {
|
|
this.email = "test@email.be"; //widget.session.email;
|
|
this.password = "kljqsdkljqsd"; //widget.session.password;
|
|
print("localhost.. set mymuseum instance by default");
|
|
}
|
|
|
|
return managerAppContext;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final appContext = Provider.of<AppContext>(context);
|
|
Size size = MediaQuery.of(context).size;
|
|
|
|
final GlobalKey<_LoginScreenState> loginKey = GlobalKey();
|
|
|
|
initInstance(appContext.getContext());
|
|
|
|
// ==> We need to work with route or something else like pop-up (pop up is nice) to make it works !
|
|
/*if(mounted && appContext != null && this.token != null)
|
|
{
|
|
this.authenticateTRY(appContext, false);
|
|
}*/
|
|
|
|
return Scaffold(
|
|
key: loginKey,
|
|
body: Center(
|
|
child: SingleChildScrollView(
|
|
child: Container(
|
|
height: size.height *0.7,
|
|
width: size.width *0.4,
|
|
decoration: BoxDecoration(
|
|
color: kWhite,
|
|
borderRadius: BorderRadius.circular(8.0),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: kWhite.withValues(alpha: 0.3),
|
|
spreadRadius: 0.5,
|
|
blurRadius: 0.5,
|
|
offset: Offset(0, 1.5), // changes position of shadow
|
|
),
|
|
],
|
|
),
|
|
child: Padding(
|
|
padding: const EdgeInsets.only(left: 60.0, right: 60.0),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
Padding(
|
|
padding: const EdgeInsets.only(bottom: 20),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Icon(Icons.museum_outlined, color: kPrimaryColor, size: size.height*0.08),
|
|
),
|
|
Center(
|
|
child: AutoSizeText(
|
|
pageTitle,
|
|
style: new TextStyle(color: kPrimaryColor, fontSize: 27, fontFamily: "Helvetica"),
|
|
maxLines: 2,
|
|
textAlign: TextAlign.center,
|
|
),
|
|
),
|
|
FutureBuilder(
|
|
future: getAppVersion(),
|
|
builder: (context, AsyncSnapshot<String> snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.done) {
|
|
if (snapshot.data != null) {
|
|
return Center(child: Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Text("${snapshot.data}", style: TextStyle(fontSize: 10)),
|
|
));
|
|
} else {
|
|
return Text("No version found");
|
|
}
|
|
|
|
} else if (snapshot.connectionState == ConnectionState.none) {
|
|
return Text("No data");
|
|
} else {
|
|
return SizedBox();
|
|
}
|
|
}
|
|
)
|
|
],
|
|
),
|
|
),
|
|
/*RoundedInputField(
|
|
hintText: "Host",
|
|
onChanged: (value) {
|
|
host = value;
|
|
},
|
|
initialValue: host,
|
|
icon: Icons.home
|
|
),*/
|
|
Form(
|
|
key: widget.key,
|
|
child: AutofillGroup(
|
|
child: Column(
|
|
children: [
|
|
SizedBox(
|
|
width: size.width*0.2,
|
|
child: RoundedInputField(
|
|
hintText: "E-mail",
|
|
autofill: AutofillHints.email,
|
|
onChanged: (value) {
|
|
email = value;
|
|
},
|
|
icon: Icons.person,
|
|
initialValue: email,
|
|
isEmail: true,
|
|
),
|
|
),
|
|
SizedBox(
|
|
width: size.width*0.2,
|
|
child: RoundedPasswordField(
|
|
initialValue: password,
|
|
onChanged: (value) {
|
|
password = value;
|
|
},
|
|
),
|
|
),
|
|
if(kIsWeb) Padding(
|
|
padding: const EdgeInsets.all(1.0),
|
|
child: Row(
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Container(
|
|
child: Checkbox(
|
|
checkColor: kTextLightColor,
|
|
activeColor: kPrimaryColor,
|
|
value: this.isRememberMe,
|
|
onChanged: (bool? value) {
|
|
if(value != null) {
|
|
setState(() {
|
|
this.isRememberMe = value;
|
|
});
|
|
}
|
|
},
|
|
),
|
|
),
|
|
Text("Se souvenir de moi", style: TextStyle(fontSize: 15, fontWeight: FontWeight.w500),),
|
|
],
|
|
),
|
|
),
|
|
SizedBox(height: size.height * 0.015),
|
|
!isLoading ? RoundedButton(
|
|
text: "SE CONNECTER",
|
|
fontSize: 16,
|
|
vertical: 15,
|
|
horizontal: 30,
|
|
press: () {
|
|
TextInput.finishAutofillContext();
|
|
authenticateTRY(appContext, true);
|
|
},
|
|
): Container(
|
|
height: size.height * 0.1,
|
|
child: CommonLoader(iconSize: 40)
|
|
),
|
|
],
|
|
)
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
)
|
|
);
|
|
}
|
|
|
|
Future<String> getAppVersion() async {
|
|
PackageInfo packageInfo = await PackageInfo.fromPlatform();
|
|
return "${packageInfo.version}"; //-${packageInfo.buildNumber}
|
|
}
|
|
} |