210 lines
6.6 KiB
Dart
210 lines
6.6 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:manager_app/Components/loading.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/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:managerapi/api.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
class LoginScreen extends StatefulWidget {
|
|
LoginScreen({Key key}) : super(key: key);
|
|
|
|
@override
|
|
_LoginScreenState createState() => _LoginScreenState();
|
|
}
|
|
|
|
class _LoginScreenState extends State<LoginScreen> {
|
|
String email = "test@email.be"; // DEV
|
|
String password = "kljqsdkljqsd"; // DEV
|
|
String host = "http://192.168.31.96"; // DEV
|
|
Client clientAPI;
|
|
bool isLoading = false;
|
|
|
|
void authenticateTRY(dynamic appContext) async {
|
|
print("try auth.. ");
|
|
print(this.email);
|
|
print(this.password);
|
|
|
|
clientAPI = Client(this.host);
|
|
|
|
var isError = true;
|
|
if (this.email != null && this.password != null && this.host != null) {
|
|
|
|
// if () {} // Add if token exist and not null + not expired
|
|
try {
|
|
setState(() {
|
|
isLoading = true;
|
|
});
|
|
LoginDTO loginDTO = new LoginDTO(email: email, password: password);
|
|
TokenDTO token = await clientAPI.authenticationApi.authenticationAuthenticateWithJson(loginDTO);
|
|
print("Token ??");
|
|
print(token);
|
|
print(token.accessToken);
|
|
setAccessToken(token.accessToken);
|
|
|
|
showNotification(Colors.lightGreen, kWhite, 'Connexion réussie', context);
|
|
|
|
isError = false;
|
|
// Set the appContext
|
|
if (appContext.getContext() == null) {
|
|
ManagerAppContext managerAppContext = new ManagerAppContext();
|
|
// store user info locally
|
|
managerAppContext.email = email;
|
|
managerAppContext.token = token;
|
|
managerAppContext.clientAPI = clientAPI;
|
|
print(managerAppContext);
|
|
|
|
appContext.setContext(managerAppContext);
|
|
}
|
|
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
|
|
Navigator.pushAndRemoveUntil(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) {
|
|
return MainScreen();
|
|
},
|
|
),
|
|
(Route<dynamic> route) => false // For pushAndRemoveUntil
|
|
);
|
|
}
|
|
catch (e) {
|
|
print("error auth");
|
|
print(e);
|
|
showNotification(Colors.orange, kWhite, 'Un problème est survenu lors de la connexion', context);
|
|
|
|
setState(() {
|
|
isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
if (!isError) {
|
|
/*UserDetailDTO user = await clientAPI.userApi.userGetDetail("6071f74aaf542f03116f2b9b");
|
|
print("user values ??");
|
|
print(user.email);
|
|
print(user.id);
|
|
|
|
List<ConfigurationDTO> configurations = await clientAPI.configurationApi.configurationGet();
|
|
print("number of configurations " + configurations.length.toString());
|
|
configurations.forEach((element) {
|
|
print(element);
|
|
});
|
|
|
|
/*List<SectionDTO> sections = await clientAPI.sectionApi.sectionGetFromConfiguration(id);
|
|
print("number of sections " + sections.length.toString());
|
|
sections.forEach((element) {
|
|
print(element);
|
|
});*/
|
|
|
|
SectionDTO section = await clientAPI.sectionApi.sectionGetDetail("60916249494b9eaf283b44f7");
|
|
print(section);
|
|
|
|
List<ResourceDTO> resources = await clientAPI.resourceApi.resourceGet();
|
|
print("number of resources " + resources.length.toString());
|
|
resources.forEach((element) {
|
|
print(element);
|
|
});
|
|
|
|
List<DeviceDTO> devices = await clientAPI.deviceApi.deviceGet();
|
|
print("number of devices " + devices.length.toString());
|
|
devices.forEach((element) {
|
|
print(element);
|
|
});*/
|
|
}
|
|
}
|
|
|
|
void setAccessToken(String accessToken) {
|
|
clientAPI.apiApi.authentications.forEach((key, auth) {
|
|
if (auth is OAuth) {
|
|
auth.accessToken = accessToken;
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final appContext = Provider.of<AppContext>(context);
|
|
Size size = MediaQuery.of(context).size;
|
|
|
|
return Scaffold(
|
|
body: Center(
|
|
child: SingleChildScrollView(
|
|
child: Container(
|
|
height: size.height *0.5,
|
|
width: size.width *0.5,
|
|
decoration: BoxDecoration(
|
|
color: kWhite,
|
|
borderRadius: BorderRadius.circular(8.0),
|
|
boxShadow: [
|
|
BoxShadow(
|
|
color: kWhite.withOpacity(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>[
|
|
RoundedInputField(
|
|
hintText: "Host",
|
|
onChanged: (value) {
|
|
host = value;
|
|
},
|
|
icon: Icons.home
|
|
),
|
|
RoundedInputField(
|
|
hintText: "Email",
|
|
onChanged: (value) {
|
|
email = value;
|
|
},
|
|
icon: Icons.person
|
|
),
|
|
RoundedPasswordField(
|
|
onChanged: (value) {
|
|
password = value;
|
|
},
|
|
),
|
|
SizedBox(height: size.height * 0.02),
|
|
!isLoading ? RoundedButton(
|
|
text: "LOGIN",
|
|
fontSize: 25,
|
|
press: () {
|
|
authenticateTRY(appContext);
|
|
},
|
|
): Container(
|
|
height: size.height * 0.1,
|
|
child: Loading()
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
isValidApi(Client client) async {
|
|
print("TEST URL");
|
|
try {
|
|
var configs = await client.configurationApi.configurationGet();
|
|
return configs != null;
|
|
} catch (ex) {
|
|
return false;
|
|
}
|
|
}
|
|
} |