Commit aebac3f8 authored by Ilham Maulana's avatar Ilham Maulana 💻

fix: sifting change password with confirm email to without confirm email

parent fc31fd6f
......@@ -51,6 +51,10 @@ class _LibraryApp extends State<LibraryApp> {
path: '/confirm-reset-password',
builder: (context, state) => const ConfirmResetPasswordScreen(),
),
GoRoute(
path: "/change-password",
builder: (context, state) => const ChangePasswordScreen(),
)
],
);
......
......@@ -23,6 +23,7 @@ class AuthProvider with ChangeNotifier {
bool isLoading = false;
bool resetPasswordTokenSended = false;
bool resetPasswordSucced = false;
bool changePasswordSucced = false;
bool loanBookSuccess = false;
List<dynamic>? loans;
......@@ -231,6 +232,45 @@ class AuthProvider with ChangeNotifier {
}
}
Future<void> changePassword(
BuildContext context,
int accountId,
String oldPassword,
String newPassword,
) async {
final token = await getAccessToken();
try {
setLoading(true);
final response = await http.post(
Uri.parse('$baseUrl/members/$accountId/change-password'),
body: jsonEncode({
"old_password": oldPassword,
"new_password": newPassword,
}),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $token'
},
);
if (response.statusCode == 200) {
if (context.mounted) {
context.go("/");
}
changePasswordSucced = true;
} else {
debugPrint(
'Change password failed: ${response.statusCode}, ${response.body}');
}
setLoading(false);
notifyListeners();
} catch (error) {
debugPrint("Change password failed: $error");
}
}
Future<void> resetPassword(String email) async {
try {
setLoading(true);
......
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:library_app/src/widgets/forms/change_password_form.dart';
import 'package:library_app/src/widgets/forms/login_form.dart';
import 'package:library_app/src/widgets/forms/reset_password_form.dart';
......@@ -89,6 +90,21 @@ class SignUpScreen extends StatelessWidget {
}
}
class ChangePasswordScreen extends StatelessWidget {
const ChangePasswordScreen({super.key});
@override
Widget build(BuildContext context) {
String title = "Change Password";
return FormScreen(
title: title,
backRoute: "/",
body: const ChangePasswordForm(),
);
}
}
class ResetPasswordScreen extends StatelessWidget {
const ResetPasswordScreen({super.key});
......
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:go_router/go_router.dart';
import 'package:library_app/src/providers/auth_provider.dart';
import 'package:library_app/src/widgets/loading.dart';
import 'package:provider/provider.dart';
class ChangePasswordForm extends StatefulWidget {
const ChangePasswordForm({super.key});
@override
State<ChangePasswordForm> createState() => _ChangePasswordForm();
}
class _ChangePasswordForm extends State<ChangePasswordForm> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
final oldPasswordController = TextEditingController();
final newPasswordController = TextEditingController();
bool passwordVisible = false;
@override
void initState() {
super.initState();
passwordVisible = true;
}
@override
void dispose() {
oldPasswordController.dispose();
newPasswordController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
Size screenSize = MediaQuery.of(context).size;
return Consumer<AuthProvider>(builder: (context, authProvider, child) {
return Column(
children: [
Padding(
padding:
const EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
child: SvgPicture.asset(
"assets/images/reset_password_image.svg",
semanticsLabel: "change password",
height: screenSize.height * 0.2,
),
),
const Text(
"Change Password",
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
Container(
padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 20.0),
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
controller: oldPasswordController,
obscureText: passwordVisible,
decoration: InputDecoration(
hintText: "Enter your Old Password",
labelText: "Old Password",
suffixIcon: IconButton(
icon: Icon(passwordVisible
? Icons.visibility
: Icons.visibility_off),
onPressed: () {
setState(
() {
passwordVisible = !passwordVisible;
},
);
},
),
),
validator: (String? value) {
if (value == null || value.isEmpty) {
return "Please enter your old password";
} else {
return null;
}
},
keyboardType: TextInputType.visiblePassword,
),
TextFormField(
controller: newPasswordController,
obscureText: passwordVisible,
decoration: InputDecoration(
hintText: "Enter your New Password",
labelText: "New Password",
suffixIcon: IconButton(
icon: Icon(passwordVisible
? Icons.visibility
: Icons.visibility_off),
onPressed: () {
setState(
() {
passwordVisible = !passwordVisible;
},
);
},
),
),
validator: (String? value) {
if (value == null || value.isEmpty) {
return "Please enter your new password";
} else {
return null;
}
},
keyboardType: TextInputType.visiblePassword,
),
const SizedBox(
height: 20.0,
),
Column(
children: [
SizedBox(
width: double.infinity,
child: FilledButton(
onPressed: () async {
if (_formKey.currentState!.validate()) {}
authProvider.changePassword(
context,
authProvider.user!.accountId,
oldPasswordController.text,
newPasswordController.text,
);
},
child: authProvider.isLoading
? const Loading()
: const Text("Submit"),
),
),
SizedBox(
width: double.infinity,
child: TextButton(
child: const Text("Cancel"),
onPressed: () => context.pop("/"),
),
),
],
),
],
),
),
)
],
);
});
}
}
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:library_app/src/providers/auth_provider.dart';
import 'package:library_app/src/models/user.dart';
......@@ -121,13 +122,7 @@ class _ProfileEditForm extends State<ProfileEditForm> {
child: SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const ResetPasswordScreen(),
),
);
},
onPressed: () => context.go("/change-password"),
child: const Text("Change Password"),
),
),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment