385 lines
14 KiB
Dart
385 lines
14 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:flutter_svg/svg.dart';
|
|
import 'package:go_router/go_router.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/l10n/app_localizations.dart';
|
|
import 'package:manager_app/Helpers/FileHelper.dart';
|
|
import 'package:manager_app/Models/managerContext.dart';
|
|
import 'package:manager_app/Models/session.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 {
|
|
clientAPI = Client(this.host!);
|
|
|
|
if (this.password.isNotEmpty || this.token != null) {
|
|
try {
|
|
if (fromClick) {
|
|
setState(() {
|
|
isLoading = true;
|
|
});
|
|
}
|
|
|
|
var accessToken = this.token;
|
|
var instanceId = this.instanceId;
|
|
var pinCode = this.pinCode;
|
|
UserRole? userRole;
|
|
|
|
if (accessToken == null) {
|
|
LoginDTO loginDTO = new LoginDTO(email: email, password: password);
|
|
|
|
TokenDTO? token = await clientAPI!.authenticationApi!
|
|
.authenticationAuthenticateWithJson(loginDTO);
|
|
|
|
if (token != null) {
|
|
accessToken = token.accessToken!;
|
|
instanceId = token.instanceId!;
|
|
pinCode = token.pinCode;
|
|
userRole = token.role;
|
|
|
|
showNotification(
|
|
kSuccess, kWhite, AppLocalizations.of(context)!.loginSuccess, 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();
|
|
if (managerAppContext == null) {
|
|
managerAppContext = new ManagerAppContext();
|
|
}
|
|
|
|
managerAppContext.email = email;
|
|
managerAppContext.host = host;
|
|
managerAppContext.instanceId = instanceId;
|
|
managerAppContext.pinCode = pinCode;
|
|
managerAppContext.accessToken = accessToken;
|
|
managerAppContext.role = userRole;
|
|
managerAppContext.clientAPI = clientAPI;
|
|
setAccessToken(accessToken);
|
|
|
|
InstanceDTO? instance = await managerAppContext.clientAPI!.instanceApi!
|
|
.instanceGetDetail(managerAppContext.instanceId!);
|
|
managerAppContext.instanceDTO = instance;
|
|
|
|
FileHelper().writeSession(new Session(
|
|
rememberMe: true,
|
|
instanceId: instanceId,
|
|
host: host,
|
|
token: accessToken,
|
|
password: password,
|
|
email: email,
|
|
role: userRole?.value));
|
|
appContext.setContext(managerAppContext);
|
|
|
|
if (instance!.isMobile!) {
|
|
context.go('/main/mobile');
|
|
} else {
|
|
if (instance.isTablet!) {
|
|
context.go('/main/tablet');
|
|
} else {
|
|
if (instance.isWeb!) {
|
|
context.go('/main/web');
|
|
} else {
|
|
if (instance.isVR!) {
|
|
context.go('/main/vr');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (fromClick) {
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
}
|
|
} else {
|
|
showNotification(Colors.orange, kWhite, AppLocalizations.of(context)!.loginError, context, null);
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
}
|
|
} catch (e) {
|
|
print("error auth");
|
|
print(e);
|
|
if (fromClick) {
|
|
showNotification(Colors.orange, kWhite, AppLocalizations.of(context)!.loginError, context, null);
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void setAccessToken(String accessToken) {
|
|
clientAPI!.apiApi!.addDefaultHeader('authorization', 'Bearer ' + accessToken);
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
this.host = "http://localhost:5000";
|
|
|
|
if (localStorage.containsKey("remember")) {
|
|
this.isRememberMe = true;
|
|
if (localStorage.containsKey("email")) {
|
|
this.email = localStorage.entries.where((e) => e.key == "email").first.value;
|
|
}
|
|
if (localStorage.containsKey("token")) {
|
|
this.token = localStorage.entries.where((e) => e.key == "token").first.value;
|
|
this.password = "AnythingAsWeAlreadyHaveAccessToken";
|
|
}
|
|
if (localStorage.containsKey("instanceId")) {
|
|
this.instanceId = localStorage.entries.where((e) => e.key == "instanceId").first.value;
|
|
}
|
|
if (localStorage.containsKey("pinCode")) {
|
|
this.pinCode = localStorage.entries.where((e) => e.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";
|
|
this.password = "kljqsdkljqsd";
|
|
print("localhost.. set mymuseum instance by default");
|
|
}
|
|
|
|
return managerAppContext;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final appContext = Provider.of<AppContext>(context);
|
|
|
|
initInstance(appContext.getContext());
|
|
|
|
return Scaffold(
|
|
body: LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
final isMobile = constraints.maxWidth < 550;
|
|
final isTablet = constraints.maxWidth < 800;
|
|
final cardWidth = isMobile
|
|
? constraints.maxWidth - 32.0
|
|
: isTablet
|
|
? 420.0
|
|
: 440.0;
|
|
final horizontalPadding = isMobile ? 24.0 : 40.0;
|
|
|
|
return Container(
|
|
width: double.infinity,
|
|
height: double.infinity,
|
|
decoration: BoxDecoration(
|
|
gradient: LinearGradient(
|
|
begin: Alignment.topLeft,
|
|
end: Alignment.bottomRight,
|
|
colors: [
|
|
kPrimaryColor.withValues(alpha: 0.12),
|
|
kBackgroundColor,
|
|
kBackgroundColor,
|
|
],
|
|
),
|
|
),
|
|
child: Center(
|
|
child: SingleChildScrollView(
|
|
padding: EdgeInsets.all(isMobile ? 16.0 : 24.0),
|
|
child: Container(
|
|
width: cardWidth,
|
|
decoration: BoxDecoration(
|
|
color: kWhite,
|
|
borderRadius: BorderRadius.circular(16.0),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: Colors.black.withValues(alpha: 0.08),
|
|
spreadRadius: 0,
|
|
blurRadius: 32,
|
|
offset: Offset(0, 8),
|
|
),
|
|
],
|
|
),
|
|
child: Padding(
|
|
padding: EdgeInsets.symmetric(
|
|
horizontal: horizontalPadding,
|
|
vertical: 40.0,
|
|
),
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
Container(
|
|
constraints: BoxConstraints(maxHeight: 80, minHeight: 40),
|
|
child: SvgPicture.asset('assets/images/MyInfoMate_logo_only.svg'),
|
|
),
|
|
SizedBox(height: 16),
|
|
AutoSizeText(
|
|
pageTitle,
|
|
style: TextStyle(
|
|
color: kPrimaryColor,
|
|
fontSize: 24,
|
|
fontFamily: "Helvetica",
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
maxLines: 2,
|
|
textAlign: TextAlign.center,
|
|
),
|
|
FutureBuilder(
|
|
future: getAppVersion(),
|
|
builder: (context, AsyncSnapshot<String> snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.done &&
|
|
snapshot.data != null) {
|
|
return Padding(
|
|
padding: const EdgeInsets.only(top: 4),
|
|
child: Text(
|
|
snapshot.data!,
|
|
style: TextStyle(fontSize: 11, color: Colors.grey),
|
|
),
|
|
);
|
|
}
|
|
return SizedBox.shrink();
|
|
},
|
|
),
|
|
SizedBox(height: 32),
|
|
Form(
|
|
key: widget.key,
|
|
child: AutofillGroup(
|
|
child: Column(
|
|
children: [
|
|
RoundedInputField(
|
|
hintText: "E-mail",
|
|
autofill: AutofillHints.email,
|
|
onChanged: (value) => email = value,
|
|
icon: Icons.person_outline,
|
|
initialValue: email,
|
|
isEmail: true,
|
|
),
|
|
RoundedPasswordField(
|
|
initialValue: password,
|
|
onChanged: (value) => password = value,
|
|
),
|
|
if (kIsWeb)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 4),
|
|
child: Row(
|
|
children: [
|
|
Checkbox(
|
|
checkColor: kWhite,
|
|
activeColor: kPrimaryColor,
|
|
value: isRememberMe,
|
|
onChanged: (value) {
|
|
if (value != null) {
|
|
setState(() => isRememberMe = value);
|
|
}
|
|
},
|
|
),
|
|
Text(
|
|
AppLocalizations.of(context)!.rememberMe,
|
|
style: TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
SizedBox(height: 24),
|
|
!isLoading
|
|
? SizedBox(
|
|
width: double.infinity,
|
|
child: RoundedButton(
|
|
text: AppLocalizations.of(context)!.connect,
|
|
fontSize: 16,
|
|
vertical: 15,
|
|
horizontal: 30,
|
|
press: () {
|
|
TextInput.finishAutofillContext();
|
|
authenticateTRY(appContext, true);
|
|
},
|
|
),
|
|
)
|
|
: CommonLoader(iconSize: 40),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<String> getAppVersion() async {
|
|
PackageInfo packageInfo = await PackageInfo.fromPlatform();
|
|
return packageInfo.version;
|
|
}
|
|
}
|