Yunus Emre
Yunus Emre

Reputation: 25

How to show password reset link in response

I have the code below which sends the password reset link and it works fine. What I want to do is show password reset link or token in response. (Yes, I know it's dangerous.)

        $status = Password::sendResetLink(
            $request->only('email'),
            function ($user, $token) {
                (\DB::table('password_resets')
                    ->updateOrInsert(
                        ['email' => $user->email],
                        [
                            'token' => md5($token)
                        ]
                    ))
                    ? $user->sendPasswordResetNotification(md5($token))
                    : null;
            }

        );

        if ($request->expectsJson()) {
            if ($status === Password::RESET_LINK_SENT) {
                return response()->json(['message' => __($status)], 200);
            } else {
                return response()->json(['message' => __($status)], 422);
            }
        }

        return $status === Password::RESET_LINK_SENT
            ? back()->with(['status' => __($status)])
            : back()->withErrors(['email' => __($status)]);
    }

I changed the code as below but I am getting 500 Internal Server Error. What's wrong?

        $status = Password::sendResetLink(
            $request->only('email'),
            function ($user, $token) use (&$token) {
                (\DB::table('password_resets')
                    ->updateOrInsert(
                        ['email' => $user->email],
                        [
                            'token' => md5($token)
                        ]
                    ))
                    ? $user->sendPasswordResetNotification(md5($token))
                    : null;
            }

        );

        if ($request->expectsJson()) {
            if ($status === Password::RESET_LINK_SENT) {
                return response()->json(['message' => __($status), 'link' => md5($token)], 200);
            } else {
                return response()->json(['message' => __($status)], 422);
            }
        }

        return $status === Password::RESET_LINK_SENT
            ? back()->with(['status' => __($status)])
            : back()->withErrors(['email' => __($status)]);
    }

Upvotes: 0

Views: 228

Answers (1)

Hosein Shendabadi
Hosein Shendabadi

Reputation: 362

One way would be like this:

$token = DB::table('password_resets')->latest('created_at')->first()->token;

You get the last record inserted in the table (by sorting created_at column) which is the latest password reset and you can get the token from that.

return response()->json(['message' => __($status), 'link' => $token], 200);

And you getting the 500 Server Error could be from the addition of use (&$token) in the second code. use is the way to use the variables out of the function scope in the function and you don't have any variable named $token (Don't confuse with the $token in the function). And you're using the use to use a function variable outside of its scope which won't work. Also you could set APP_DEBUG to true (NOT IN PRODUCTION MODE) to see the reason for 500 error.

Upvotes: 2

Related Questions