简体   繁体   中英

Alter Request parameter(Email) and then Authenticate User in Laravel

I need to change the request parameter ie email, and then attempt login with the new email. What I am trying:

$user_handle = $request->email;
$gook = Gookarma::where('handle', '=', $user_handle)->firstOrFail();
$acc = Account::find($gook->karmable_id);
$request->email = $acc->email;
            
if ($this->attemptLogin($request)) {
    return $this->sendLoginResponse($request);
}

But it doesn't update the request, and login attempt goes with the previous email field input. Previous email I'm pulling up from API.

I tried with request->all() but when attempt login after the request update, it displays an error.

you can use merge method :

$request->merge([
    'email' =>$acc->email,
]);

or you can build an array to replace the entire request input like:

$arrayToReplace=$request->all();
$arrayToReplace['email']=$acc->email;

and then use replace method:

 $request->replace($arrayToReplace);

if neither of the above ways did not work, try making a request your self:

 $array=$request->all();
 $array['email']=$acc->email;
 $req = new Request([$array]);

then use the new request for your operations.

你可以尝试这样的事情:

$request->merge(['email' => $acc->email]);

Creating a new variable of Request instance and assigning email and password from the previous request variable to the new variable worked for me.

$req = new Request([$request]);
$req['email']=$acc->email;
$req['password'] = $request->password;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM