156 lines
4.9 KiB
Dart
156 lines
4.9 KiB
Dart
import 'package:flutter/material.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";
|
|
String password = "kljqsdkljqsd";
|
|
final clientAPI = Client();
|
|
|
|
void authenticateTRY(dynamic appContext) async {
|
|
print("try auth.. ");
|
|
print(this.email);
|
|
print(this.password);
|
|
|
|
// if () {} // Add if token exist and not null + not expired
|
|
var isError = true;
|
|
try {
|
|
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);
|
|
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);
|
|
}
|
|
|
|
Navigator.pushAndRemoveUntil(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) {
|
|
return MainScreen();
|
|
},
|
|
),
|
|
(Route<dynamic> route) => false // For pushAndRemoveUntil
|
|
);
|
|
}
|
|
catch (e) {
|
|
print("error auth");
|
|
}
|
|
|
|
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);
|
|
});
|
|
|
|
List<RessourceDTO> ressources = await clientAPI.ressourceApi.ressourceGet();
|
|
print("number of ressources " + ressources.length.toString());
|
|
ressources.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: "Your Email",
|
|
onChanged: (value) {
|
|
email = value;
|
|
},
|
|
icon: Icons.person
|
|
),
|
|
RoundedPasswordField(
|
|
onChanged: (value) {
|
|
password = value;
|
|
},
|
|
),
|
|
SizedBox(height: size.height * 0.02),
|
|
RoundedButton(
|
|
text: "LOGIN",
|
|
fontSize: 25,
|
|
press: () {
|
|
authenticateTRY(appContext);
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
} |