If I want to change the password, the codes are working properly, do not
show any mistakes, but it looks incorrect if you want to log in with a new
password. I saw in the database, there was no password change.
public function postPasswordReset(Request $request)
{
$validator = validator::make($request->all(), [
'email' => 'required|exists:users,email',
'password' => 'required|alpha_num|between:6,32',
're-password' => 'required|same:password'
]);
if($validator->passes()){
$user = User::where('email', $request->email)->first();
$user->update(['password' bcrypt($request->password)]);
return redirect()->route('auth.login');
}
return redirect()->back()->withErrors($validator->errors())-
>withInput();
}
In your update method you are using an array to update in a wrong format. Try this one
$user->update(['password' => bcrypt($request->password)]);
Related
I'm trying to implement the sometimes validation rule into one of my projects (Laravel 5.6).
I have a profile page that a user can update their name and password, but i want to make it so that if the user doesnt enter a password, it wont update that field, which is what i thought the sometimes rule was.
The complete update method i am using in my controller is below.
If i leave the password field blank, then it returns a string or min error which it shouldn't be doing.
public function update()
{
$user = Auth::user();
$this->validate(request(), [
'name' => 'required',
'password' => 'sometimes|string|min:6'
]);
$user->name = request('name');
$user->password = bcrypt(request('password'));
$user->save();
return back();
}
Any help would be greatly appreciated.
The problem is that if you leave the password field empty, it is still present in the request. But filled with null
Try this instead:
public function update()
{
$user = Auth::user();
$this->validate(request(), [
'name' => 'required',
'password' => 'nullable|string|min:6'
]);
$user->name = request('name');
if(!is_null(request('password'))) {
$user->password = bcrypt(request('password'));
}
$user->save();
return back();
}
Try to add nullable in validation rule
$this->validate(request(), [
'name' => 'required',
'password' => 'sometimes|nullable|string|min:6'
]);
From Laravel docs:
nullable
The field under validation may be null. This is particularly useful
when validating primitive such as strings and integers that can
contain null values.
I have done programming with other frameworks but this is new to me, thanks for your time in advanced, I'm trying to create new user on a condition that if there is data in another table then new user is created otherwise not I have put my code inside if else statement and is throwing errors.
my function for creating new user is listed below:
protected function create(array $data)
{
/*$exists = \DB::table('received_pay')->where('email', $data['email'])->first(); */
$exists=\DB::table('received_pay')->where('email', '=', $data['email'])->where('token', $data['token'])->exists();
if ($exists === null) {
// user doesn't exist
return User::create([
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'token' => $data['token'],
]);
}else{
return null;
}
}
}
this is the error it throws
You need to return User instance from the RegisterController#create method by default, you can't return null. So, do this instead of return null;:
return User::where('email', $data['email'])->first();
If it's an option to check if the user exists in the users table, you can use the firstOrCreate method:
protected function create(array $data)
{
return User::firstOrCreate(['email' => $data['email'], [
'username' => $data['username'],
'password' => bcrypt($data['password']),
'token' => $data['token'],
]);
}
Also, you if want to check if a user exists in the received_pay table, you can leave original RegisterController#create method and add this rule to the RegisterController#validator method:
'email' => 'unique:received_pay,email',
This will not allow Laravel to create a new user if a user with the same email already exists in the received_pay table.
Will try make this clear as much as I can.
Im rolled out a make Auth call in order to use the login and registeration function of laravel and later just used the template to provide the needs I wanted that is.
If user is admin he/she can register a new user.
public function openNewUser(){
return view('auth.register');
}
NB. Part for update.
public function registerNewUser(Request $request){
$this->validate($request,[
'email' => 'required|email|unique:users',
'name' => 'required|max:120',
'password' => 'required|min:4|confirmed']);
$user = new User();
$user->name = $request->name;
$user->email = $request->email;
$user->password = encrypt($request->password);
if (Gate::denies('register-user')) {
return redirect()->back();
}
$user->save();
return view('home');
}
Problem 1 - I also want to update user , which is giving problems. The password inputs return empty fields , which i understand. When I try to change it doenst work the confirm password always give a mismatch even though they are the same. When I leave it blank too it doesnt work because the field is required to be filled. I took them off the form and tried if i could edit the email only but only didnt work.
public function userUpdate (Request $request,$user_id) {
$this->validate($request,[
'email' => 'required|email',
'name' => 'required|max:120',
'password' => 'required|min:4|confirmed']);
$user = new User();
$user->name = $request->name;
$user->email = $request->email;
$user->password = encrypt($request->password);
if (Gate::allows('register-user')) {
$user->save();
$user->roles()->attach($request->roles);
return redirect()->route('view_users');
}elseif (Gate::denies('register-user')) {
if (Auth::id() == $user_id) {
$user->save();
$user->roles()->attach($request->roles);
return redirect()->route('view_users');
}else{
return redirect()->back();
}
}
}
Problem 2. I just realized all logins I am doing with my new registration gives These credentials do not match our records.Even though the credentials are there and was registered correctly.
I am using the login provided by laravel but I created my own registration.
Please how can I edit and update my users and also be able to login after registration
What version of Laravel are you using?
Here is my (v5.3) register() method in RegisterController.php, at least part for registration:
public function register(Request $request)
{
...
// save and login user
$user = $this->create($request->all());
$this->guard()->login($user);
...
}
...
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'lastname' => $data['lastname'],
'phone' => $data['phone'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
and the login() method from LoginController.php
public function login(Request $request)
{
$credentials = $this->credentials($request);
...
if ($this->guard()->attempt($credentials, $request->has('remember'))) {
return $this->sendLoginResponse($request);
}
}
Hopefully I haven't miss anything.
Keep in mind that things have changed here from version 5.2.
I found out what was wrong , Since I am using Laravel's login my registration had to use bycrypt for the encryption which is what Laravel registration was using , but I was using encrypt when I created my own registration so there was a conflict when logging in. (Remembere I was using Laravels login not my own written Login). I hope this helps someone
I am writing an API. I was wondering how could I return proper response.
data is not valid (only if email aready exists)
data is not valid (general validation errors)
Here is my code so far:
$data = Input::all();
$validator = Validator::make($request->all(), [
'email' => 'required|email|unique:users,email',
'some_other_field' => 'required'
]);
if ($validator->fails()) {
// NOTE: using a or b, this are my custom methods
a) return $this->existsResponse($data);
b) return $this->badRequestResponse($data);
}
How could I figure out if email already exists?
You should look at the Validator Class. I think the Methods invalid() and valid() should be the right for you. This methods gives you an array of keys back.
http://laravel.com/api/5.0/Illuminate/Validation/Validator.html#method_invalid
Well, you're trying to do two different types of validations, so it would make sense to use two different validators:
$data = $request->all();
// validate email
$validator = Validator::make($data, [
'email' => 'required|email|unique:users,email',
]);
if ($validator->fails()) {
return $this->existsResponse($data);
}
// email valid, so validate other data
$validator = Validator::make($data, [
'some_other_field' => 'required'
]);
if ($validator->fails()) {
return $this->badRequestResponse($data);
}
// if here, all data is valid
I have solved it like this.
public function store(Request $request)
{
$data = Input::all();
// validating data
$validator = Validator::make($request->all(), [
'email' => 'required|email',
'some_other_field' => 'required'
]);
if ($validator->fails()) {
return $this->badRequestResponse($data);
}
// validating if resource exists
$userExists = User::where('email', $request->email)->count();
if($userExists) {
return $this->existsResponse($data);
}
// inserting data
$user = new User();
$user->fill($data);
$user->save();
if($user->save()) {
return $this->createdResponse($data);
}
}
I have a login form with
username, password and remember me
remember me is a checkbox (true or false).
How do I create the validation rule in Laravel?
http://laravel.com/docs/validation#basic-usage
The only relevant one it seems was in and you specify the values but the values in this case are booleans and using this method they would be specified as string?
in:true,false
There's a validator for boolean. Assuming you're using one of the packages that simplifies model validation, e.g. EsensiModel, it's as simple as adding the following to your Model:
protected $rules = [
'email' => 'required|email',
'password' => 'required',
'remember_me' => 'boolean',
];
You may try something like this:
$rules = array('email' => 'required|email', 'password' => 'required');
$inputs = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
$validator = Validator::make($inputs, $rules);
if($validator->fails()) {
return Redirect::back()->withInput()->withErrorts($validator);
}
else {
$remember = Input::get('remember', FALSE);
if(Auth::attempt($inputs, !!$remember)) {
// Log in successful
return Redirect::to('/'); // redirect to home or wherever you want
}
}
I've used email which is recommended but if you use username other than email then just change the email to username and in the rule for username use something like this:
'username' => 'required|alpha|min:6' // Accepts only a-z and minimum 6 letters