how password reset in Flutter with API Laravel in Flutter

I am resetting a password in Flutter with an API built with Laravel. This example assumes that you have already set up the necessary endpoints in your Laravel API and that you are using the Flutter http package to make HTTP requests.

Flutter code for sending a password reset request:

Future<void> sendPasswordResetRequest(String email) async {
  final url = '';
  final response = await
    body: {'email': email},
  if (response.statusCode == 200) {
    // Password reset request sent successfully
  } else {
    throw Exception('Failed to send password reset request');

Laravel code for generating a password reset token:

Create a controller that will handle the password reset logic. You can use the php artisan make:controller Auth\ForgotPasswordController command to create the controller.

public function sendPasswordResetEmail(Request $request)
    $request->validate(['email' => 'required|email']);

    $user = User::where('email', $request->email)->first();

    if (!$user) {
        return response()->json(['message' => 'User not found'], 404);

    $token = Str::random(60);

        'email' => $request->email,
        'token' => Hash::make($token),
        'created_at' => Carbon::now()

    Mail::to($request->email)->send(new PasswordResetMail($token));

    return response()->json(['message' => 'Password reset email sent']);

Laravel code for verifying the password reset token and updating the password:

public function resetPassword(Request $request)
        'token' => 'required',
        'email' => 'required|email',
        'password' => 'required|confirmed|min:8'

    $reset = DB::table('password_resets')
        ->where('email', $request->email)
        ->where('token', $request->token)

    if (!$reset) {
        return response()->json(['message' => 'Invalid password reset token'], 400);

    $user = User::where('email', $request->email)->first();

    if (!$user) {
        return response()->json(['message' => 'User not found'], 404);

    $user->password = Hash::make($request->password);

    DB::table('password_resets')->where('email', $request->email)->delete();

    return response()->json(['message' => 'Password reset successfully']);
Notify of

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

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x