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

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.

You could try something like this:
$request->merge(['email' => $acc->email]);

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.

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;

Related

Laravel value of variable will not hold

I never saw this before. Hopefully I can explain it to you that you'll understand it.
My code:
$google2fa = app(Google2FA::class);
$secret = $google2fa->generateSecretKey();
$user = $this->verifyToken('authentication', $request->mail, $secret);
$g2faUrl = $google2fa->getQRCodeUrl(
'name',
$request->mail,
$user['google_authentication']
);
$writer = new Writer(
new ImageRenderer(
new RendererStyle(400),
new ImagickImageBackEnd()
)
);
$qrcode_image = base64_encode($writer->writeString($g2faUrl));
return view('auth.register.admin.index', ['qrcode' => '', 'secret' => $user['google_authentication']]);
Above you see the array variable: $user['google_authentication'] on the first part I create a secret token, then I save the secret in the verify method verifyToken().
When I publish this secret on the view, it shows me a different secret then that i saved. But the weird part is, when I place a var_dump() in the code, then the secret in the view is the same as I saved.
The verify method just do a simple post api request to my middleware where I save the secret under the user, nothing special.
What is wrong in here?
The view doesn't load twice, I already checked Laravel debug bar
And why does it hold the value of the variable ($user['google_authentication']) when I use var_dump() in the code.

Using variable name in Laravel request object

I need to be able to loop through a list of laravel request variables and do something with them. I want to be able to use a variable when calling the request object so that I can run it in a loop instead of writing a line of code for every one.
For example, my text inputs may have names that look something like this
contact_main_name
contact_main_telephone
contact_main_email
contact_sub_name
contact_sub_telephone
contact_sub_email
contact_backup_name
contact_backup_telephone
contact_backup_email
In my request, I don't want to have to write
$request->contact_main_name
$request->contact_main_telephone
For each different type of contact I may have, I want to be able to loop through them like so
$contactTypes = [
'main',
'sub',
'backup',
'head'
];
foreach($contactTypes as $type){
//Start a new contact
$contact = new Contact;
$contact->type = $type;
$contact->name = $request->${"contact_".$type."_name"};
$contact->telephone = $request->${"contact_".$type."_telephone"};
$contact->email = $request->${"contact_".$type."_email"};
$contact->save();
}
How would i use a variable name when calling a laravel $request so that I can just build an array of possible types and loop through them all?
Note
I know i can edit the input fields themselves to look something like name="contact[type][name]" and then loop through them, but I cant be changing the input names, I have to do it via php in the controller itself.
As answered in comments, to do this, change the method of calling the input and use the actual input() function itself.
$contactTypes = [
'main',
'sub',
'backup',
'head'
];
foreach($contactTypes as $type){
//Start a new contact
$contact = new Contact;
$contact->type = $type;
$contact->name = $request->input("contact_".$type."_name");
$contact->telephone = $request->input("contact_".$type."_telephone");
$contact->email = $request->input("contact_".$type."_email");
$contact->save();
}
As an aside, you could also modify it slightly to use array indices matching the field names; this would allow you to add fields later by adding the appropriate field to the database and HTML without touching the code, and use array_keys() to retrieve the types submitted to allow seamless addition of types. As long as your validations are tight, this is probably the most automated way to allow future expansion...
Ex. Field Names:
contact[main][name]
contact[main][telephone]
...
contact[backup][email]
Ex. Code:
foreach(array_keys($request->input('contact')) as $type) {
$contact = Contact::create($request->input('contact.'.$type));
$contact->type = $type;
$contact->save();
}

how to validate email exists in database using luman while create new user?

how to validate email exists in database using luman while create new user?.
my registration controller code
$borrower = borrowerRegistration::create($request->all());
$last_borrower_id=DB::getPdo()->lastInsertId();
$store_borrower_array=array();
$store_borrower_array["borrower_id"]=$last_borrower_id;
$borrower_result = array('status' => 'true','message' =>'The First step borrower registration successfully.','content'=>array('registration'=>$store_borrower_array));
return json_encode($borrower_result);
please give a valuable suggestions.
You can try this way. here User is your Model (I am assuming)
if (User::where('email', '=', Input::get('email'))->exists()) {
// user found
}
Replace Input::get('email') to your email address from where you are getting and storing it.

ZF2: How to change Request object globally for all Controllers when using forward plugin?

I'm using forward plugin in testing and performance purposes.
At first IndexController data passes through normal POST request.
There I get this requst and POST data and I need add one more parameter to it.
$this->getRequest()->getPost()->subsystem = 'avia';
Than I use forward plugin
$result = $this->forward()->dispatch(
"Port\\Controller",
[
'controller' => 'Port\\Controller',
'action' => 'port',
]
);
And whan I'm in this PortController I would get my request POST data again and it SHOULD contain my changes from IndexController
$post = $this->getRequest()->getPost();
isset($post['subsystem']) //true
But it does't. It get's request object without changes.
isset($post['subsystem']) //FALSE
How to change Request globally for all controllers in current request process?
What i'm already trying?
//#1
$params = $this->getServiceLocator()->get('ControllerPluginManager')->get('params');
$params->getController()->getRequest()
->getPost()->subsystem
= 'avia';
//#2
$this->getRequest()->getPost()->subsystem = 'avia';
//#3
$post = $this->getRequest()->getPost();
$post['subsystem'] = 'avia';
//NEED UPDATE GLOBALLY !
$this->getRequest()->setPost($post);
//#4
$event = $this->getEvent();
$event->getRequest()->getPost()->subsystem = 'avia';
Debug::vars($event->getRequest()->getPost());
//#5
$_POST = $post->toArray();
And all this variances not working.
I'm already read this answer
ZF2: How to pass parameters to forward plugin which I can then get in the method I forward them to?
But I don't want pass data through params, I need change Request.
UPD
But now i'm tested and maybe it was because on receiver side I tried to get request this way
$request = $this->bodyParams();
But I should use it like this
if (!$request['subsystem']) {
$request = $this->getRequest()->getPost()->toArray();
}
It was because I used Apigility RPC service and placed post data in JSON format in Request Content field, not in POST. And in another place I tried get it
$params = $this->serviceLocator->get('ControllerPluginManager')->get('params');
$requestContent = $params->getController()->getRequest()->getContent();
$request = Json::decode($requestContent, Json::TYPE_ARRAY);
But after I started to use POST and that's why it started to be confused.
I am not sure if this is really something you should do but I think you should be able to achieve it like this:
$parameters = $this->getRequest()->getPost();
$parameters->set('subsystem', 'avia');
$parameters is instance of Zend\Stdlib\Parameters.

Signup email activation confirmation issue. Activation code string missing from email URL even though concatenated

I have this method:
public function activation_code()
{ //activation code sent into db
$activation_code = random_string('alnum', 32);
return $activation_code;
}
What I'm trying to do is provide this with the post data that gets sent to my database but also provide a copy of that same activation code so I can concatenate it with the "click here to confirm email" url that is in my confirmation email that is sent to users upon registration.
How can I do this? I can't provide the method because if I do the database code and the email URL code will be different so user wouldn't be able to match them and confirm their email address.
I've tried many other ways such as providing the method in one place e.g.
public function create()
{ //get post data and insert into db
$dbcolumn->group_id = 2; //group 1 for admin group 2 for member
$dbcolumn->first_name = $this->input->post('first_name');
$dbcolumn->last_name = $this->input->post('last_name');
$dbcolumn->email = $this->input->post('email');
$dbcolumn->password = $this->hashed();
$dbcolumn->birthday = $this->input->post('year') .
'-' . $this->input->post('month') . '-' . $this->input->post('day');
$dbcolumn->sex = $this->input->post('sex');
$dbcolumn->activation_code = $this->activation_code();
// date and time user joined the website
$dbcolumn->created_on = date('Y-m-d H:i:s', now());
$this->db->insert('users', $dbcolumn);
}
If you look at the dbcolumn->activation code line you'll see what I've done. That works and the code is stored in the database. If I provide the same "$this->activation_code() method to the email that's sent the codes will obviously be different.
public function send_confirmation_email()
{ //receives variable from create method
$this->load->library('email');
$this->email->from('wengerarsen#gmail.com', 'my site');
$this->email->to($this->input->post('email'));
$this->email->subject('my site - Activate your account');
//copy of activation code returned from create method
$this->email->message('We\'re back, please click the link to activate your account ' . anchor('http://mysite.com/activation/' . $this->activation_code(), 'Activate my account'));
$this->email->send();
}
As you can see I have the same method $activation_code() pulled into my send confirmation email method. This will just generate a whole new code meaning I won't be able to match the database activation code and the URI segment code in the users email.
I have tried to make the variable in the return public and call it in the send confirmaton email method but it doesn't work. The code ends up missing from he end of the URL in the email.
I've tried so many different ways and nothings working.
Maybe I'm missing something here?
Advice, examples etc will be much appreciated.
Every time you're calling activation_code() you're going to be creating a new code, because you're only storing it in the scope of that function.
A better idea would be to store it as an object property, like follows:
public var $_activation_code = null;
public function activation_code() {
if (is_null($this->_activation_code)) {
$this->_activation_code = random_string('alnum', 32);
}
return $this->_activation_code;
}
This will create the code if it hasn't already been done so for this object, or will simply return the current code if the method has been called more than once, meaning the code will be consistent across the object.

Categories