我该如何呼叫laravel护照的忘记密码并使用Angular 8验证api?

时间:2019-06-15 11:24:47

标签: angular laravel

我正在尝试使用Laravel护照在角度8中实施验证电子邮件并忘记密码,但我找不到他们的api。

我手动写了一些api,但是这些都行不通。

Route::get('email/verify', 'Auth\VerificationController@verify');
Route::get('forgot','Auth\ForgotPasswordController');

有人知道我该怎么做吗?

2 个答案:

答案 0 :(得分:0)

在您的api.php中添加这些路由,

// forget password
Route::post('forget', 'Auth\ForgotPasswordController@getResetToken');

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

//user verification
Route::get('email/verify/{token}', 'Auth\VerificationController@verify');

在ForgetPasswordController中添加它,

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;
class ForgotPasswordController extends Controller
{
    use SendsPasswordResetEmails;
    public function __construct()
    {
        $this->middleware('guest');
    }

    public function getResetToken(Request $request)
    {
        $this->validate($request, ['email' => 'required|email']);
        $sent = $this->sendResetLinkEmail($request);

        return ($sent) 
            ? response()->json(['message'=>'Success'])
            : response()->json(['message'=>'Failed']);

    }

    public function sendResetLinkEmail(Request $request)
    {
        $this->validateEmail($request);
        $response = $this->broker()->sendResetLink(
            $request->only('email')
        );
        return $response == Password::RESET_LINK_SENT ? 1 : 0;
    }
}

创建特征SendsPasswordResetEmails,并添加如下代码。在App \ Http \ Traits \ SendsPasswordResetEmails位置,您可以在自己喜欢的位置创建。

<?php
namespace App\Http\Traits;
use Illuminate\Http\Request;
use App\Http\Traits\Password;
use Illuminate\Http\RedirectResponse;
trait SendsPasswordResetEmails
{

    public function showLinkRequestForm()
    {
        return  new RedirectResponse("https://www.followmyfamily.net/password/reset");
    }

    public function sendResetLinkEmail(Request $request)
    {
        $this->validateEmail($request);
        $response = $this->broker()->sendResetLink(
            $request->only('email')
        );
        return $response == Password::RESET_LINK_SENT
                    ? $this->sendResetLinkResponse($response)
                    : $this->sendResetLinkFailedResponse($request, $response);
    }

    protected function validateEmail(Request $request)
    {
        $this->validate($request, ['email' => 'required|email']);
    }

    protected function sendResetLinkResponse($response)
    {
        return back()->with('status', trans($response));
    }

    protected function sendResetLinkFailedResponse(Request $request, $response)
    {
        return back()->withErrors(
            ['email' => trans($response)]
        );
    }

    public function broker()
    {
        return Password::broker();
    }
}


ResetPasswordController就是这样,

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Transformers\Json;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;
use Illuminate\Http\RedirectResponse;

class ResetPasswordController extends Controller
{
    public function __construct()
    {
        $this->middleware('guest');
    }

    public function reset(Request $request)
    {
        $this->validate($request, $this->rules(), $this->validationErrorMessages());
        $response = $this->broker()->reset(
            $this->credentials($request), function ($user, $password) {
                $this->resetPassword($user, $password);
            }
        );
        if ($request->wantsJson()) {
            if ($response == Password::PASSWORD_RESET) {
                return response()->json(['data'=>trans('passwords.reset')]);
            } else {
                return response()->json(['email' => $request->input('email'), 'data'=>trans($response)]);
            }
        }
        $response == Password::PASSWORD_RESET
        ? $this->sendResetResponse($response)
        : $this->sendResetFailedResponse($request, $response);

        return  new RedirectResponse(env("yourdomain")+"?verified=$response");

    }
}

最后,在VerificationController中创建create verify方法,它看起来像

public function verifyUser($token)
    {
        $verifyUser = VerifyUser::where('token', $token)->first();
        if (isset($verifyUser)) {
            $user = $verifyUser->user;
            if (!$user->verified) {
                $verifyUser->user->verified = 1;
                $verifyUser->user->save();
                $status = "success";
            } else {
                $status = "already-verified";
            }
            return new RedirectResponse(env("yourdomain")+"/profile/edit?verified=$status");
        } else {
            $status = "duplicate-email";
            return new RedirectResponse(env("yourdomain")+"/profile/edit?verified=$status");
        }
        return new RedirectResponse(env("yourdomain")+"?andParams=$status");

    }

*您的代码中的名称空间可能不同,并且控制器或方法可能不同。其余逻辑将相同。

答案 1 :(得分:0)

api.php

Route::post('password/forgot-password', 'AuthAPIController@forgotPassword');
Route::post('password/reset', 'AuthAPIController@passwordReset');

AuthAPIController

别忘了导入这些小玩意

use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Password;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

忘记密码方法

public function forgotPassword(Request $request){
    $input = $request->only('email');
    $validator = Validator::make($input, [
        'email' => "required|email"
    ]);
    if ($validator->fails()) {
        return response()->json($validator->errors());
    }
    $response = Password::sendResetLink($input);

    $message = $response == Password::RESET_LINK_SENT ? 'Mail send successfully' : GLOBAL_SOMETHING_WANTS_TO_WRONG;
    
    return response()->json($message);
}

重置密码方法

public function passwordReset(Request $request){
    $input = $request->only('email','token', 'password', 'password_confirmation');
    $validator = Validator::make($input, [
        'token' => 'required',
        'email' => 'required|email',
        'password' => 'required|confirmed|min:8',
    ]);
    if ($validator->fails()) {
        return response()->json($validator->errors());
    }
    $response = Password::reset($input, function ($user, $password) {
        $user->password = Hash::make($password);
        $user->save();
    });
    $message = $response == Password::PASSWORD_RESET ? 'Password reset successfully' : GLOBAL_SOMETHING_WANTS_TO_WRONG;
    return response()->json($message);
}