0

I need to change the request parameter i.e. 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.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
hdrajani
  • 73
  • 1
  • 2
  • 10

3 Answers3

0

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.

OMR
  • 11,736
  • 5
  • 20
  • 35
  • Thanks for your response, I tried doing this but it isn't updating the email, I tried this $request = $request->all(); $request['email'] = $acc->email; This updated the email, but on $this->attemptLogin($request) it shows error: Argument 1 passed to App\Http\Controllers\Auth\LoginController::attemptLogin() must be an instance of Illuminate\Http\Request, array given – hdrajani Nov 06 '20 at 14:43
  • try the second way – OMR Nov 06 '20 at 14:47
  • It doesn't work either. Request object doesn't get updated. – hdrajani Nov 06 '20 at 15:03
  • do you have: use Illuminate\Http\Request; in top of your class? – OMR Nov 06 '20 at 15:05
  • Yes I have imported – hdrajani Nov 06 '20 at 15:07
  • Thanks bro for the help, this updated the request variable with email, but still not accepted by $this->attemptLogin($req), Any idea ? – hdrajani Nov 06 '20 at 15:37
0

You could try something like this:

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

jsjsjs
  • 46
  • 3
0

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;
hdrajani
  • 73
  • 1
  • 2
  • 10