filename components and MIH_Package to mih_package

This commit is contained in:
2024-09-17 15:36:53 +02:00
parent fb86eba228
commit 46c6eba8a4
42 changed files with 65 additions and 65 deletions

View File

@@ -0,0 +1,27 @@
import 'package:flutter/material.dart';
import 'package:patient_manager/mih_packages/authentication/signInOrRegister.dart';
import 'package:patient_manager/mih_packages/MIH_Home/home.dart';
import 'package:supertokens_flutter/supertokens.dart';
class AuthCheck extends StatelessWidget {
const AuthCheck({super.key});
Future<bool> doesSessionExist() async {
return await SuperTokens.doesSessionExist();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder(
future: doesSessionExist(),
builder: (context, snapshot) {
if (snapshot.data == true) {
return const Home();
} else {
return const SignInOrRegister();
}
}),
);
}
}

View File

@@ -0,0 +1,337 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:patient_manager/mih_components/mih_pop_up_messages/mih_error_message.dart';
import 'package:patient_manager/mih_components/mih_inputs_and_buttons/mih_text_input.dart';
import 'package:patient_manager/mih_components/mih_inputs_and_buttons/mih_button.dart';
import 'package:patient_manager/mih_components/mih_pop_up_messages/mih_loading_circle.dart';
import 'package:patient_manager/mih_components/mih_pop_up_messages/mih_success_message.dart';
import 'package:patient_manager/env/env.dart';
import 'package:patient_manager/main.dart';
import 'package:supabase_auth_ui/supabase_auth_ui.dart';
import 'package:supertokens_flutter/http.dart' as http;
class ForgotPassword extends StatefulWidget {
const ForgotPassword({super.key});
@override
State<ForgotPassword> createState() => _ForgotPasswordState();
}
class _ForgotPasswordState extends State<ForgotPassword> {
final emailController = TextEditingController();
//bool _obscureText = true;
bool successfulForgotPassword = false;
bool acceptWarning = false;
// focus node to capture keyboard events
final FocusNode _focusNode = FocusNode();
final baseAPI = AppEnviroment.baseApiUrl;
Future<void> submitPasswodReset() async {
showDialog(
context: context,
builder: (context) {
return const Mihloadingcircle();
},
);
try {
var response = await http.post(
Uri.parse("$baseAPI/auth/user/password/reset/token"),
body:
'{"formFields": [{"id": "email","value": "${emailController.text}"}]}',
headers: {
'Content-type': 'application/json',
'Accept': 'application/json',
//"Authorization": "leatucczyixqwkqqdrhayiwzeofkltds"
},
);
//print(response.body[])
if (response.statusCode == 200) {
//print(response.body);
var userSignedin = jsonDecode(response.body);
if (userSignedin["status"] == "OK") {
//print("here");
setState(() {
successfulForgotPassword = true;
});
Navigator.of(context).pop();
} else {
Navigator.of(context).pop();
//loginError();
}
}
} on AuthException {
Navigator.of(context).pop();
//loginError();
}
}
Color getPrim() {
return MzanziInnovationHub.of(context)!.theme.secondaryColor();
}
Color getSec() {
return MzanziInnovationHub.of(context)!.theme.primaryColor();
}
void prePassResteWarning() {
showDialog(
context: context,
builder: (context) {
return Dialog(
child: Stack(
children: [
Container(
padding: const EdgeInsets.all(10.0),
width: 500.0,
height: 450,
decoration: BoxDecoration(
color: MzanziInnovationHub.of(context)!.theme.primaryColor(),
borderRadius: BorderRadius.circular(25.0),
border: Border.all(
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
width: 5.0),
),
child: SingleChildScrollView(
child: Column(
//mainAxisSize: MainAxisSize.max,
children: [
Icon(
Icons.warning_amber_rounded,
size: 100,
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
),
const SizedBox(height: 15),
Text(
" Password Reset Confirmation",
textAlign: TextAlign.center,
style: TextStyle(
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
fontSize: 25.0,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 15),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: Text(
"Before you reset your password, please be aware that you'll receive an email with a link to confirm your identity and set a new password. Make sure to check your inbox, including spam or junk folders. If you don't receive the email within a few minutes, please try resending the reset request.",
style: TextStyle(
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
),
const SizedBox(height: 25),
SizedBox(
width: 500.0,
height: 50.0,
child: MIHButton(
buttonText: "Continue",
buttonColor: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
textColor: MzanziInnovationHub.of(context)!
.theme
.primaryColor(),
onTap: () {
setState(() {
acceptWarning = true;
});
Navigator.of(context).pop();
validateInput();
},
),
),
],
),
),
),
Positioned(
top: 5,
right: 5,
width: 50,
height: 50,
child: IconButton(
onPressed: () {
Navigator.pop(context);
},
icon: Icon(
Icons.close,
color: MzanziInnovationHub.of(context)!.theme.errorColor(),
size: 35,
),
),
),
],
),
);
},
);
}
void loginError() {
showDialog(
context: context,
builder: (context) {
return const MIHErrorMessage(errorType: "Invalid Credentials");
},
);
}
void resetLinkSentSuccessfully() {
showDialog(
context: context,
builder: (context) {
return const MIHSuccessMessage(
successType: "Success",
successMessage:
"We've sent a password reset link to your email address. Please check your inbox, including spam or junk folders.\n\nOnce you find the email, click on the link to reset your password.\n\nIf you don't receive the email within a few minutes, please try resending the reset request.\n\nThe reset link will expire after 2 hours");
},
);
}
void validateInput() async {
if (emailController.text.isEmpty) {
showDialog(
context: context,
builder: (context) {
return const MIHErrorMessage(errorType: "Input Error");
},
);
} else {
await submitPasswodReset();
if (successfulForgotPassword) {
Navigator.of(context).pushNamedAndRemoveUntil('/', (route) => false);
resetLinkSentSuccessfully();
}
}
}
@override
void dispose() {
emailController.dispose();
_focusNode.dispose();
super.dispose();
}
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return KeyboardListener(
focusNode: _focusNode,
autofocus: true,
onKeyEvent: (event) async {
if (event is KeyDownEvent &&
event.logicalKey == LogicalKeyboardKey.enter) {
validateInput();
}
},
child: Scaffold(
//backgroundColor: Colors.white,
body: Stack(
children: [
SafeArea(
child: Center(
child: SingleChildScrollView(
physics: const BouncingScrollPhysics(),
child: Padding(
padding: const EdgeInsets.all(25.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
//logo
Icon(
Icons.lock,
size: 100,
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
),
//spacer
const SizedBox(height: 10),
//Heading
Text(
'Forgot Password',
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
),
),
//spacer
const SizedBox(height: 25),
//email input
SizedBox(
width: 500.0,
child: MIHTextField(
controller: emailController,
hintText: 'Email',
editable: true,
required: true,
),
),
//spacer
const SizedBox(height: 30),
// sign in button
SizedBox(
width: 500.0,
height: 50.0,
child: MIHButton(
buttonText: "Reset Password",
buttonColor: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
textColor: MzanziInnovationHub.of(context)!
.theme
.primaryColor(),
onTap: () {
prePassResteWarning();
},
),
),
],
),
),
),
),
),
Positioned(
top: 10,
left: 5,
width: 50,
height: 50,
child: IconButton(
onPressed: () {
Navigator.of(context).pop();
},
icon: const Icon(Icons.arrow_back),
),
),
],
),
),
);
}
}

View File

@@ -0,0 +1,375 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:patient_manager/mih_components/mih_pop_up_messages/mih_loading_circle.dart';
import 'package:patient_manager/mih_components/mih_pop_up_messages/mih_error_message.dart';
import 'package:patient_manager/mih_components/mih_inputs_and_buttons/mih_pass_input.dart';
import 'package:patient_manager/mih_components/mih_pop_up_messages/mih_success_message.dart';
import 'package:patient_manager/mih_components/mih_inputs_and_buttons/mih_text_input.dart';
import 'package:patient_manager/mih_components/mih_inputs_and_buttons/mih_button.dart';
import 'package:patient_manager/env/env.dart';
import 'package:patient_manager/main.dart';
//import 'package:patient_manager/objects/sessionST.dart';
import 'package:supabase_auth_ui/supabase_auth_ui.dart';
//import 'package:supertokens_flutter/supertokens.dart';
import 'package:supertokens_flutter/http.dart' as http;
import 'package:supertokens_flutter/supertokens.dart';
class Register extends StatefulWidget {
final Function()? onTap;
const Register({super.key, required this.onTap});
@override
State<Register> createState() => _RegisterState();
}
class _RegisterState extends State<Register> {
final emailController = TextEditingController();
final passwordController = TextEditingController();
final confirmPasswordController = TextEditingController();
final officeID = TextEditingController();
final baseAPI = AppEnviroment.baseApiUrl;
final FocusNode _focusNode = FocusNode();
bool _obscureText = true;
bool successfulSignUp = false;
Future<void> addUserAPICall(String email, String uid) async {
//await getOfficeIdByUser(docOfficeIdApiUrl + widget.userEmail);
//print(futureDocOfficeId.toString());
var response = await http.post(
Uri.parse("$baseAPI/user/insert/"),
headers: <String, String>{
"Content-Type": "application/json; charset=UTF-8"
},
body: jsonEncode(<String, dynamic>{
"email": email,
"app_id": uid,
}),
);
if (response.statusCode == 201) {
Navigator.of(context).pushNamedAndRemoveUntil('/', (route) => false);
signUpSuccess();
// setState(() {
// successfulSignUp = true;
// });
} else {
internetConnectionPopUp();
}
}
//sign user in
Future<void> signUserUp() async {
if (!validEmail()) {
emailError();
} else if (passwordController.text != confirmPasswordController.text) {
passwordError();
} else {
//var _backgroundColor = Colors.transparent;
showDialog(
context: context,
builder: (context) {
return const Mihloadingcircle();
},
);
try {
Uri uri = Uri.parse(
"$baseAPI/auth/emailpassword/email/exists?email=${emailController.text}");
//print("Here");
var response = await http.get(uri);
//print(response.body);
//print("response 1: ${response.statusCode}");
if (response.statusCode == 200) {
var userExists = jsonDecode(response.body);
if (userExists["exists"]) {
signUpError();
} else {
var response2 = await http.post(
Uri.parse("$baseAPI/auth/signup"),
body:
'{"formFields": [{"id": "email","value": "${emailController.text}"}, {"id": "password","value": "${passwordController.text}"}]}',
headers: {
'Content-type': 'application/json',
'Accept': 'application/json',
"Authorization": "leatucczyixqwkqqdrhayiwzeofkltds"
},
);
//print("response 2: ${response2.statusCode}");
if (response2.statusCode == 200) {
//print("response 2: ${response2.body}");
var userCreated = jsonDecode(response2.body);
//print("Created user $userCreated");
if (userCreated["status"] == "OK") {
//print("Here1");
//Creat user in db
String uid = await SuperTokens.getUserId();
//print("uid: $uid");
addUserAPICall(emailController.text, uid);
Navigator.of(context).pop();
//print("Here1");
} else if (userCreated["status"] == "FIELD_ERROR") {
Navigator.of(context).pop();
passwordReqError();
} else {
Navigator.of(context).pop();
internetConnectionPopUp();
}
}
}
}
} on AuthException catch (error) {
Navigator.of(context).pop();
loginError(error.message);
emailController.clear();
passwordController.clear();
confirmPasswordController.clear();
}
}
}
bool validEmail() {
String text = emailController.text;
var regex = RegExp(r'^[a-zA-Z0-9]+@[a-zA-Z.-]+\.[a-zA-Z]{2,}$');
return regex.hasMatch(text);
}
void internetConnectionPopUp() {
showDialog(
context: context,
builder: (context) {
return const MIHErrorMessage(errorType: "Internet Connection");
},
);
}
void signUpSuccess() {
showDialog(
context: context,
builder: (context) {
return const MIHSuccessMessage(
successType: "Success",
successMessage:
"Congratulations! Your account has been created successfully. You are logged in and can start exploring.\n\nPlease note: more apps will appear once you update your profile.");
},
);
}
void signUpError() {
showDialog(
context: context,
builder: (context) {
return const MIHErrorMessage(errorType: "User Exists");
},
);
}
void passwordError() {
showDialog(
context: context,
builder: (context) {
return const MIHErrorMessage(errorType: "Password Match");
},
);
}
void emailError() {
showDialog(
context: context,
builder: (context) {
return const MIHErrorMessage(errorType: "Invalid Email");
},
);
}
void passwordReqError() {
showDialog(
context: context,
builder: (context) {
return const MIHErrorMessage(errorType: "Password Requirements");
},
);
}
void loginError(error) {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text(error),
);
},
);
}
void validateInput() async {
if (emailController.text.isEmpty ||
passwordController.text.isEmpty ||
confirmPasswordController.text.isEmpty) {
showDialog(
context: context,
builder: (context) {
return const MIHErrorMessage(errorType: "Input Error");
},
);
} else {
await signUserUp();
}
}
void toggle() {
setState(() {
_obscureText = !_obscureText;
});
}
@override
void dispose() {
emailController.dispose();
passwordController.dispose();
officeID.dispose();
_focusNode.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return KeyboardListener(
focusNode: _focusNode,
autofocus: true,
onKeyEvent: (event) async {
if (event is KeyDownEvent &&
event.logicalKey == LogicalKeyboardKey.enter) {
validateInput();
}
},
child: Scaffold(
//backgroundColor: Colors.white,
body: SafeArea(
child: Center(
child: SingleChildScrollView(
physics: const BouncingScrollPhysics(),
child: Padding(
padding: const EdgeInsets.all(25.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
//logo
Icon(
Icons.lock,
size: 100,
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
),
//spacer
const SizedBox(height: 10),
//Heading
Text(
'Register',
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
),
),
//spacer
const SizedBox(height: 25),
//email input
SizedBox(
width: 500.0,
child: MIHTextField(
controller: emailController,
hintText: 'Email',
editable: true,
required: true,
),
),
//spacer
const SizedBox(height: 10),
//password input
SizedBox(
width: 500.0,
child: MIHPassField(
controller: passwordController,
hintText: 'Password',
required: true,
signIn: false,
),
),
//spacer
const SizedBox(height: 10),
//password input
SizedBox(
width: 500.0,
child: MIHPassField(
controller: confirmPasswordController,
hintText: 'Confirm Password',
required: true,
signIn: false,
),
),
//spacer
const SizedBox(height: 30),
// sign up button
SizedBox(
width: 500.0,
height: 50.0,
child: MIHButton(
buttonText: "Sign Up",
buttonColor: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
textColor: MzanziInnovationHub.of(context)!
.theme
.primaryColor(),
onTap: () async {
validateInput();
},
),
),
const SizedBox(height: 10),
//register text
SizedBox(
width: 500.0,
//height: 100.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
const Text(
'Already a User?',
style: TextStyle(fontSize: 18, color: Colors.grey),
),
const SizedBox(
width: 6,
),
GestureDetector(
onTap: widget.onTap,
child: Text(
'Sign In',
style: TextStyle(
fontSize: 18,
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
fontWeight: FontWeight.bold,
),
),
)
],
),
)
],
),
),
),
),
),
),
);
}
}

View File

@@ -0,0 +1,393 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:patient_manager/mih_components/mih_inputs_and_buttons/mih_pass_input.dart';
import 'package:patient_manager/mih_components/mih_pop_up_messages/mih_error_message.dart';
import 'package:patient_manager/mih_components/mih_inputs_and_buttons/mih_button.dart';
import 'package:patient_manager/mih_components/mih_pop_up_messages/mih_loading_circle.dart';
import 'package:patient_manager/mih_components/mih_pop_up_messages/mih_success_message.dart';
import 'package:patient_manager/env/env.dart';
import 'package:patient_manager/main.dart';
import 'package:supabase_auth_ui/supabase_auth_ui.dart';
import 'package:supertokens_flutter/http.dart' as http;
class ResetPassword extends StatefulWidget {
final String? token;
const ResetPassword({
super.key,
required this.token,
});
@override
State<ResetPassword> createState() => _ResetPasswordState();
}
class _ResetPasswordState extends State<ResetPassword> {
final passwordController = TextEditingController();
final confirmPasswordController = TextEditingController();
//bool _obscureText = true;
bool successfulResetPassword = false;
bool acceptWarning = false;
// focus node to capture keyboard events
final FocusNode _focusNode = FocusNode();
final baseAPI = AppEnviroment.baseApiUrl;
Future<void> submitPasswodReset() async {
showDialog(
context: context,
builder: (context) {
return const Mihloadingcircle();
},
);
try {
var callBody =
'{"method": "token","formFields": [{"id": "password","value": "${passwordController.text}"}],"token": "${widget.token}"}';
//print(callBody);
var response = await http.post(
Uri.parse("$baseAPI/auth/user/password/reset"),
body: callBody,
headers: {
'Content-type': 'application/json; charset=utf-8',
// 'Accept': 'application/json',
//"Authorization": "leatucczyixqwkqqdrhayiwzeofkltds"
},
);
//print(response.body[])
if (response.statusCode == 200) {
//print(response.body);
var userPassReset = jsonDecode(response.body);
if (userPassReset["status"] == "OK") {
//print("here");
setState(() {
successfulResetPassword = true;
});
Navigator.of(context).pop();
Navigator.of(context).pushNamed('/');
} else if (userPassReset["status"] == "FIELD_ERROR") {
Navigator.of(context).pop();
passwordReqError();
} else {
Navigator.of(context).pop();
loginError();
}
}
} on AuthException {
Navigator.of(context).pop();
//loginError();
}
}
void passwordReqError() {
showDialog(
context: context,
builder: (context) {
return const MIHErrorMessage(errorType: "Password Requirements");
},
);
}
Color getPrim() {
return MzanziInnovationHub.of(context)!.theme.secondaryColor();
}
Color getSec() {
return MzanziInnovationHub.of(context)!.theme.primaryColor();
}
void prePassResteWarning() {
showDialog(
context: context,
builder: (context) {
return Dialog(
child: Stack(
children: [
Container(
padding: const EdgeInsets.all(10.0),
width: 500.0,
height: 450,
decoration: BoxDecoration(
color: MzanziInnovationHub.of(context)!.theme.primaryColor(),
borderRadius: BorderRadius.circular(25.0),
border: Border.all(
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
width: 5.0),
),
child: SingleChildScrollView(
child: Column(
//mainAxisSize: MainAxisSize.max,
children: [
Icon(
Icons.warning_amber_rounded,
size: 100,
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
),
const SizedBox(height: 15),
Text(
"Password Reset Confirmation",
textAlign: TextAlign.center,
style: TextStyle(
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
fontSize: 25.0,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 15),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 25.0),
child: Text(
"Before you reset your password, please be aware that you'll receive an email with a link to confirm your identity and set a new password. Make sure to check your inbox, including spam or junk folders. If you don't receive the email within a few minutes, please try resending the reset request.",
style: TextStyle(
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
),
const SizedBox(height: 25),
SizedBox(
width: 500.0,
height: 50.0,
child: MIHButton(
buttonText: "Continue",
buttonColor: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
textColor: MzanziInnovationHub.of(context)!
.theme
.primaryColor(),
onTap: () {
setState(() {
acceptWarning = true;
});
Navigator.of(context).pop();
validateInput();
},
),
),
],
),
),
),
Positioned(
top: 5,
right: 5,
width: 50,
height: 50,
child: IconButton(
onPressed: () {
Navigator.pop(context);
},
icon: Icon(
Icons.close,
color: MzanziInnovationHub.of(context)!.theme.errorColor(),
size: 35,
),
),
),
],
),
);
},
);
}
void loginError() {
showDialog(
context: context,
builder: (context) {
return const MIHErrorMessage(errorType: "Invalid Credentials");
},
);
}
void resetSuccessfully() {
showDialog(
context: context,
builder: (context) {
return const MIHSuccessMessage(
successType: "Success",
successMessage:
"Great news! Your password reset is complete. You can now log in to Mzansi Innovation Hub using your new password.");
},
);
}
void passwordError() {
showDialog(
context: context,
builder: (context) {
return const MIHErrorMessage(errorType: "Password Match");
},
);
}
void validateInput() async {
if (passwordController.text.isEmpty ||
confirmPasswordController.text.isEmpty) {
showDialog(
context: context,
builder: (context) {
return const MIHErrorMessage(errorType: "Input Error");
},
);
} else if (passwordController.text != confirmPasswordController.text) {
passwordError();
} else {
await submitPasswodReset();
if (successfulResetPassword) {
resetSuccessfully();
}
}
}
@override
void dispose() {
passwordController.dispose();
confirmPasswordController.dispose();
_focusNode.dispose();
super.dispose();
}
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return KeyboardListener(
focusNode: _focusNode,
autofocus: true,
onKeyEvent: (event) async {
if (event is KeyDownEvent &&
event.logicalKey == LogicalKeyboardKey.enter) {
validateInput();
}
},
child: Scaffold(
//backgroundColor: Colors.white,
body: Stack(
children: [
SafeArea(
child: Center(
child: SingleChildScrollView(
physics: const BouncingScrollPhysics(),
child: Padding(
padding: const EdgeInsets.all(25.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
//logo
Icon(
Icons.lock,
size: 100,
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
),
//spacer
const SizedBox(height: 10),
//Heading
Text(
'Reset Password',
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
),
),
//spacer
// const SizedBox(height: 15),
// Text(
// 'token: ${widget.token}',
// style: TextStyle(
// fontSize: 15,
// fontWeight: FontWeight.bold,
// color: MzanziInnovationHub.of(context)!
// .theme
// .secondaryColor(),
// ),
// ),
//spacer
const SizedBox(height: 25),
//email input
SizedBox(
width: 500.0,
child: MIHPassField(
controller: passwordController,
hintText: 'New Password',
required: true,
signIn: false,
),
),
//spacer
const SizedBox(height: 10),
//password input
SizedBox(
width: 500.0,
child: MIHPassField(
controller: confirmPasswordController,
hintText: 'Confirm New Password',
required: true,
signIn: false,
),
),
//spacer
const SizedBox(height: 30),
// sign in button
SizedBox(
width: 500.0,
height: 50.0,
child: MIHButton(
buttonText: "Reset Password",
buttonColor: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
textColor: MzanziInnovationHub.of(context)!
.theme
.primaryColor(),
onTap: () {
validateInput();
},
),
),
],
),
),
),
),
),
Positioned(
top: 10,
left: 5,
width: 50,
height: 50,
child: IconButton(
onPressed: () {
//Navigator.of(context).pop();
Navigator.of(context).pushReplacementNamed('/');
},
icon: const Icon(Icons.login),
),
),
],
),
),
);
}
}

View File

@@ -0,0 +1,35 @@
import 'package:flutter/material.dart';
import 'package:patient_manager/mih_packages/authentication/register.dart';
import 'package:patient_manager/mih_packages/authentication/signin.dart';
class SignInOrRegister extends StatefulWidget {
const SignInOrRegister({super.key});
@override
State<SignInOrRegister> createState() => _SignInOrRegisterState();
}
class _SignInOrRegisterState extends State<SignInOrRegister> {
bool showSignInPage = true;
void togglePages() {
setState(() {
showSignInPage = !showSignInPage;
});
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
}
@override
Widget build(BuildContext context) {
if (showSignInPage) {
return SignIn(onTap: togglePages);
} else {
return Register(onTap: togglePages);
}
}
}

View File

@@ -0,0 +1,456 @@
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:patient_manager/mih_components/mih_layout/mih_tile.dart';
import 'package:patient_manager/mih_components/mih_pop_up_messages/mih_loading_circle.dart';
import 'package:patient_manager/mih_components/mih_pop_up_messages/mih_error_message.dart';
import 'package:patient_manager/mih_components/mih_inputs_and_buttons/mih_pass_input.dart';
import 'package:patient_manager/mih_components/mih_inputs_and_buttons/mih_text_input.dart';
import 'package:patient_manager/mih_components/mih_inputs_and_buttons/mih_button.dart';
import 'package:patient_manager/env/env.dart';
import 'package:patient_manager/main.dart';
import 'package:supabase_auth_ui/supabase_auth_ui.dart';
import 'package:supertokens_flutter/http.dart' as http;
class SignIn extends StatefulWidget {
final Function()? onTap;
const SignIn({super.key, required this.onTap});
@override
State<SignIn> createState() => _SignInState();
}
class _SignInState extends State<SignIn> {
final emailController = TextEditingController();
final passwordController = TextEditingController();
//bool _obscureText = true;
bool successfulSignIn = false;
// focus node to capture keyboard events
final FocusNode _focusNode = FocusNode();
final baseAPI = AppEnviroment.baseApiUrl;
late List<MIHTile> sandboxProfileList = [];
//sign user in
Future<void> signUserIn() async {
//var _backgroundColor = Colors.transparent;
showDialog(
context: context,
builder: (context) {
return const Mihloadingcircle();
},
);
try {
var response = await http.post(
Uri.parse("$baseAPI/auth/signin"),
body:
'{"formFields": [{"id": "email","value": "${emailController.text}"}, {"id": "password","value": "${passwordController.text}"}]}',
headers: {
'Content-type': 'application/json',
'Accept': 'application/json',
"Authorization": "leatucczyixqwkqqdrhayiwzeofkltds"
},
);
//print(response.body[])
if (response.statusCode == 200) {
//print(response.body);
var userSignedin = jsonDecode(response.body);
if (userSignedin["status"] == "OK") {
//print("here");
setState(() {
successfulSignIn = true;
});
Navigator.of(context).pop();
} else {
Navigator.of(context).pop();
loginError();
passwordController.clear();
}
}
} on AuthException {
Navigator.of(context).pop();
loginError();
passwordController.clear();
}
}
Color getPrim() {
return MzanziInnovationHub.of(context)!.theme.secondaryColor();
}
Color getSec() {
return MzanziInnovationHub.of(context)!.theme.primaryColor();
}
void setSandboxProfiles(List<MIHTile> tileList) {
tileList.add(MIHTile(
onTap: () {
setState(() {
emailController.text = "testpatient@mzansi-innovation-hub.co.za";
passwordController.text = "Testprofile@1234";
});
validateInput();
},
tileName: "Patient",
tileIcon: Icon(
Icons.perm_identity_rounded,
color: getSec(),
size: 200,
),
p: getPrim(),
s: getSec(),
));
tileList.add(MIHTile(
onTap: () {
setState(() {
emailController.text = "testdoctor@mzansi-innovation-hub.co.za";
passwordController.text = "Testprofile@1234";
});
validateInput();
},
tileName: "Doctor",
tileIcon: Icon(
Icons.medical_services,
color: getSec(),
size: 200,
),
p: getPrim(),
s: getSec(),
));
//if (AppEnviroment.getEnv() == "Dev") {
tileList.add(MIHTile(
onTap: () {
setState(() {
emailController.text = "test@mzansi-innovation-hub.co.za";
passwordController.text = "Testprofile@1234";
});
validateInput();
},
tileName: "Test",
tileIcon: Icon(
Icons.warning_amber_rounded,
color: getSec(),
size: 200,
),
p: getPrim(),
s: getSec(),
));
//}
}
void loginError() {
showDialog(
context: context,
builder: (context) {
return const MIHErrorMessage(errorType: "Invalid Credentials");
},
);
}
void validateInput() async {
if (emailController.text.isEmpty || passwordController.text.isEmpty) {
showDialog(
context: context,
builder: (context) {
return const MIHErrorMessage(errorType: "Input Error");
},
);
} else {
await signUserIn();
if (successfulSignIn) {
Navigator.of(context).pushNamedAndRemoveUntil('/', (route) => false);
}
}
}
void showSandboxProfiles() {
showDialog(
context: context,
builder: (context) {
return Dialog(
//backgroundColor: MzanziInnovationHub.of(context)!.theme.primaryColor(),
child: Stack(
children: [
Container(
padding: const EdgeInsets.all(10.0),
width: 500.0,
height: 500,
decoration: BoxDecoration(
color: MzanziInnovationHub.of(context)!.theme.primaryColor(),
borderRadius: BorderRadius.circular(25.0),
border: Border.all(
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
width: 5.0),
),
child: Column(
//mainAxisSize: MainAxisSize.max,
children: [
Text(
"Sandbox Profiles",
textAlign: TextAlign.center,
style: TextStyle(
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
fontSize: 25.0,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 10),
Text(
"NB: These accounts are used for test purposes. Please do not store personal information on these profiles.",
textAlign: TextAlign.center,
style: TextStyle(
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
fontSize: 15.0,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 15),
Expanded(
child: GridView.builder(
// physics: ,
// shrinkWrap: true,
itemCount: sandboxProfileList.length,
gridDelegate:
const SliverGridDelegateWithMaxCrossAxisExtent(
mainAxisSpacing: 10, maxCrossAxisExtent: 100),
itemBuilder: (context, index) {
return sandboxProfileList[index];
},
),
),
],
),
),
Positioned(
top: 5,
right: 5,
width: 50,
height: 50,
child: IconButton(
onPressed: () {
Navigator.pop(context);
},
icon: Icon(
Icons.close,
color: MzanziInnovationHub.of(context)!.theme.errorColor(),
size: 35,
),
),
),
],
),
);
},
);
}
@override
void dispose() {
emailController.dispose();
passwordController.dispose();
_focusNode.dispose();
super.dispose();
}
@override
void initState() {
setState(() {
setSandboxProfiles(sandboxProfileList);
});
super.initState();
}
@override
Widget build(BuildContext context) {
return KeyboardListener(
focusNode: _focusNode,
autofocus: true,
onKeyEvent: (event) async {
if (event is KeyDownEvent &&
event.logicalKey == LogicalKeyboardKey.enter) {
validateInput();
}
},
child: Scaffold(
//backgroundColor: Colors.white,
body: SafeArea(
child: Center(
child: SingleChildScrollView(
physics: const BouncingScrollPhysics(),
child: Padding(
padding: const EdgeInsets.all(25.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
//logo
Icon(
Icons.lock,
size: 100,
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
),
//spacer
const SizedBox(height: 10),
//Heading
Text(
'Sign In',
style: TextStyle(
fontSize: 25,
fontWeight: FontWeight.bold,
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
),
),
//spacer
const SizedBox(height: 25),
SizedBox(
width: 500.0,
//height: 100.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
GestureDetector(
onTap: () {
showSandboxProfiles();
},
child: Text(
'Sandbox Profile',
style: TextStyle(
fontSize: 18,
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
fontWeight: FontWeight.bold,
),
),
),
],
),
),
const SizedBox(height: 10),
//email input
SizedBox(
width: 500.0,
child: MIHTextField(
controller: emailController,
hintText: 'Email',
editable: true,
required: true,
),
),
//spacer
const SizedBox(height: 10),
//password input
SizedBox(
width: 500.0,
child: MIHPassField(
controller: passwordController,
hintText: 'Password',
required: true,
signIn: true,
),
),
SizedBox(
width: 500.0,
//height: 100.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
GestureDetector(
onTap: () {
Navigator.of(context).pushNamed(
'/forgot-password',
);
},
child: Text(
'Forgot Password?',
style: TextStyle(
fontSize: 15,
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
fontWeight: FontWeight.bold,
),
),
),
],
),
),
//spacer
const SizedBox(height: 30),
// sign in button
SizedBox(
width: 500.0,
height: 50.0,
child: MIHButton(
buttonText: "Sign In",
buttonColor: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
textColor: MzanziInnovationHub.of(context)!
.theme
.primaryColor(),
onTap: () async {
validateInput();
},
),
),
//spacer
const SizedBox(height: 10),
//register text
SizedBox(
width: 500.0,
//height: 100.0,
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
const Text(
'New User?',
style: TextStyle(
fontSize: 18,
color: Color.fromARGB(255, 201, 200, 200)),
),
const SizedBox(
width: 6,
),
GestureDetector(
onTap: widget.onTap,
child: Text(
'Register Now',
style: TextStyle(
fontSize: 18,
color: MzanziInnovationHub.of(context)!
.theme
.secondaryColor(),
fontWeight: FontWeight.bold,
),
),
),
],
),
)
],
),
),
),
),
),
),
);
}
}