πŸš€ DevOps Certified Professional
πŸ“… Starting: 1st of Every Month 🀝 +91 8409492687 | 🀝 +1 (469) 756-6329 πŸ” Contact@DevOpsSchool.com

How to reset a password in the Flutter app with Laravel API

DevOps

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Step 1: Create a Forgot Password

Center(
child: Card(
elevation: 0,
color: Colors.blue,
child: TextButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => HomeReset(),
),
);
_callApi(email);
},
child: const SizedBox(
child: Center(
child: Text(
'Reset Password',
style: TextStyle(
fontSize: 20.0,
color: Colors.black,
),
),
),
),
),
),
),

Step 2: Create a Forgot Password page

import 'package:flutter/material.dart';
import 'login_screen.dart';
void main() {
runApp(const HomeReset());
}
class HomeReset extends StatelessWidget {
const HomeReset({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(colorSchemeSeed: const Color(0xff6750a4)),
home: Scaffold(
appBar: AppBar(title: const Text('Patient Reset Link Send')),
body: Column(
children: const <Widget>[
ElevatedCardExample(),
FilledCardExample(),
],
),
),
);
}
}
class ElevatedCardExample extends StatelessWidget {
const ElevatedCardExample({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return const Center(
child: Card(
child: SizedBox(
width: 300,
height: 100,
child: Center(
child: Text(
' Please check inbox of your email and follow the instructions given to reset your account Password',
style: TextStyle(
fontSize: 20.0,
color: Colors.black,
// backgroundColor: Colors.black,
))),
),
),
);
}
}
class FilledCardExample extends StatelessWidget {
const FilledCardExample({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: Card(
elevation: 0,
color: Colors.blue,
child: TextButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => LoginScreen(),
));
},
child: const SizedBox(
width: 250,
height: 50,
child: Center(
child: Text('After that click here to login again',
style: TextStyle(
fontSize: 15.0,
color: Colors.white,
// backgroundColor: Colors.black,
)),
),
),
),
),
);
}
}

Step 3: Create API

Future<void> _callApi(String email) async {
try {
if (email.isNotEmpty) {
var response = await authServices.getUserResetPassword(email);
if (response.statusCode == 200) {
print('Password reset link sent successfully.');
} else {
print('Failed to send reset link.');
}
} else {
print('User email not found. Cannot reset password.');
}
} catch (e) {
print('Error occurred: $e');
}
}
Future<http.Response> getUserResetPassword(String email) async {
final prefs = await SharedPreferences.getInstance();
final token = prefs.getString('token');
final pemail = prefs.getString('email');
Map<String, dynamic> data = {
"email": email,
};
var body = json.encode(data);
var url = Uri.parse(baseURL + 'password/reset');
var response = await http.post(
url,
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer $token',
'Content-Type': 'application/json',
},
body: body,
);
return response;
}

Step 4: Create a Route in the App.php in Laravel

	Route::post('/password/reset', 'Auth\ResetPasswordController@resetApp');

Step 5: Create a function in Laravel

public function resetApp(Request $request)
{
log::info('yaha tak aa raha hai');
$request->validate([
'email' => 'required|email',
]);
$response = $this->broker()->sendResetLink($request->only('email'));
return $response == Password::RESET_LINK_SENT
? response()->json(['message' => 'Reset link sent successfully'], 200)
: response()->json(['message' => 'Unable to send reset link'], 400);
}

Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x