Resetting Email/Password - php

I have a page setup to reset the user's email address password and the users zip_code for an application. I have it working correctly however, if for example I want to just change the zip code my validation will not allow it due to it reading the email as already existing in the database and returns an error. What is the best way around this? I could create a controller for each field and do it individually but I feel there is some if statement I could use maybe?
Controller:
enter
public function getEditUser() {
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password'),
'confirm_password' => Input::get('confirm_password'),
'user_zip_code' => Input::get('user_zip_code')
);
$rules = array(
'email' => 'required|email|unique:users,email',
'password' => 'required|min:5',
'confirm_password' => 'required|same:password',
'user_zip_code' => 'required'
);
$validation = Validator::make($userdata, $rules);
if ($validation->fails()) {
return Redirect::to('dashboard/edit/account')->withErrors($validation)
->withInput();
}
$userdata['password'] = Hash::make($userdata['password']);
$userdata['confirm_password'] = Hash::make($userdata['confirm_password']);
User::find(Auth::user()->id)->update($userdata);
return Redirect::to('dashboard');
}
Unique needs to be in place obviously so it doesn't get matched with another account
Also my email is set through Auth..Auth::user()->email if that helps

You can set the validation unique rule to ignore the current id
$rules = array(
'email' => 'required|email|unique:users,email,'.Auth::user()->id,
'password' => 'required|min:5',
'confirm_password' => 'required|same:password',
'user_zip_code' => 'required'
);
See here for more info from the docs

Related

How validate param only if user is guest in laravel?

I want validate name and email (do required) only if user is guest. How I can do it correctly?
I tried paste the if condition. But I don't think this is correct:
if (Auth::check()) {
$request->validate([
'name' => 'required|string',
'email' => 'required|string',
'body' => 'required|string',
'type' => 'required|integer|in:1,2,3'
]);
} else {
$request->validate([
'body' => 'required|string',
'type' => 'required|integer|in:1,2,3'
]);
}
I can't think of a validation rule that'll do this, but you can at least get the code a little cleaner by skipping the else condition, for instance like so:
$rules = [
'body' => 'required|string',
'type' => 'required|integer|in:1,2,3',
];
if (Auth::guest()) {
$rules['name'] = 'required|string';
$rules['email'] = 'required|string';
}
$request->validate($rules);
Another option would be to simply check the requirement within the array and just skip the field in the validation for authorised users. It's shorter, but not quite as clean:
$request->validate([
'body' => 'required|string',
'type' => 'required|integer|in:1,2,3',
'name' => Auth::guest() ? 'required|string' : '',
'email' => Auth::guest() ? 'required|string' : '',
]);
By the way, I would recommend validating the email address as an email address: https://laravel.com/docs/5.7/validation#rule-email

How can I check i 2 fields of a form have the same content using a rule array of laravelcollective/html validator?

I am pretty new in Laravel and I have the following problem.
I have this method that handle the submit of a form:
public function store(Request $request) {
Log::info('store() START');
$data = Input::all();
Log::info('INSERTED DATA: '.implode("|", $data));
$rules = array(
'name' => 'required',
'surname' => 'required',
'login' => 'required',
'email' => 'required|email',
'emailConfirm' => 'required|email',
'pass' => 'required',
'passlConfirm' => 'required',
'g-recaptcha-response' => 'required|captcha',
);
$validator = Validator::make($data, $rules);
if ($validator->fails()){
return Redirect::to('/registration')->withInput()->withErrors($validator);
}
else{
// Do your stuff.
}
}
As you can see it contains a $rules array containing the validation rules.
It works pretty fine but I want also perform the following 2 checks:
The email field have to contains the same text than the emailConfirm field.
The pass field have to contains the same text than the passConfirm field.
Can I implement this kind of check into the $rules array?
Actually Laravel comes with the validation rule called confirmed. You will need to change your fields to email_confirmation and pass_confirmation and add confirmed rule to email and pass fields.
As answered by #Mariusz, Laravel come up with value comparison rule as confirmed rule. To acheive your output you should write,
$rules = array(
'name' => 'required',
'surname' => 'required',
'login' => 'required',
'email' => 'required|confirmed|email',
'pass' => 'required|confirmed',
'g-recaptcha-response' => 'required|captcha',
);
As you have mentioned, you are using laravelcollective/html, then you have to also add following code to generate confirmation fields
echo Form::email("email_confirmation",null, $attributes = []);
echo Form::password("pass_confirmation",null, $attributes = []);

Laravel 5.3 check if email exists only if password field is filled in

My application uses the standard validator, and my form makes the user provide an email address. They may continue as a guest, but if they do want to create an account; the only thing they will have to provide is a password and in combination with that email address will create the user account.
However, my issue is I am not sure how to use the validator exists only if the password field has been filled in.
$this->validate($request, [
'first_name' => 'required',
'email' => 'required|confirmed|email',
'last_name' => 'required',
'street_1' => 'required',
'zip_code' => 'required',
'phone_1' => 'required',
'password' => 'required_if:account,1|confirmed',
]);
I could do a check and return redirect with an error message, but I'd prefer to go through the validator if I can.
The simplest solution is to put your validation rules into an array then perform your desired check. So if the user checked the "account creation" checkbox, add the rules.
$rules = [
'first_name' => 'required',
'last_name' => 'required',
'street_1' => 'required',
'zip_code' => 'required',
'phone_1' => 'required',
'password' => 'required_if:account,1|confirmed',
]
if ($request->input('acount') == 1) {
$rules['email'] = 'required|confirmed|email'
}

How to make auto login after registration in laravel

I have encounterd a problem while regiserting user in laravel, $user suppose to be array which contains all array element while auto login the following code results false. The password save in the database is hash::make('password').
$user_id = $this->user_model->addUser($user);
$post = array('password' => $pass_for_auth, 'email' => $email);
var_dump(Auth::attempt($post,true));die;
Can any one tell me what is the correct problem
You can try to login the user through his $user_id. So your code will be:
$user_id = $this->user_model->addUser($user);
$post = array('password' => $pass_for_auth, 'email' => $email);
Auth::loginUsingId($user_id);
You created the user so it returns an user_id, with the user_id you can login the user.
Hope this works!
More information at: https://laravel.com/docs/5.2/authentication#other-authentication-methods
This would be the standard way to do user registration - you create a new user model instance and pass it to Auth::login() :
// do not forget validation!
$this->validate($request, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
$data = $request->all();
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
Auth::login($user);

Username/password Authentication Rules in Laravel

Currently, when a user logs into my Laravel app I use the following rules...
// Validation rules
$rules = array(
'email' => 'required|email|exists:users,email',
'password' => 'required'
);
What I'm looking for is a validation rule for checking the password against the user.
From docs:
if (Auth::attempt(array('email' => $email, 'password' => $password))) {
return Redirect::intended('dashboard');
}
Example:
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
$rules = array(
'email' => 'required|email|exists:users,email',
'password' => 'required'
);
// Validate the inputs.
$validator = Validator::make($userdata, $rules);
// Check if the form validates with success.
if ($validator->passes()) {
// Try to log the user in.
if (Auth::attempt($userdata)) {
// Redirect to homepage
return Redirect::to('')->with('success', 'You have logged in successfully');
} else {
// Redirect to the login page.
return Redirect::to('login')->withErrors(array('password' => 'Password invalid'))->withInput(Input::except('password'));
}
}

Categories