Undefined index: password Laravel 5.5 - logging - php

In my database password column name is "new_password".
then when I try to log, it says Undefined index: password.
But if password column name is "password" it works properly. I need to use previous column name.
How can I fix this?
$credentials = array(
'user_name' => Input::get('username'),
'new_password' => Input::get('password')
);
$user = Auth::attempt($credentials);

Hello, Thisaru
If you need to use your password field as new_password
1) First thing is laravel password field by default set password into your model. So If you change the password as new_password add this line in your Model
protected $primaryKey = 'new_password';
I hope this code help to solve your problem

Related

Authenticate user on login - Laravel

I am trying to authenticate use on login using this
//Attempt to auth the user
if(! auth()->attempt(request(['email','password' => Hash::make(request('password'))])))
{
//If not redirect back
return back()->withErrors([
'message' => 'Please check your email or password'
]);
}
But I am getting this error..
QueryException in Connection.php line 647:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '$2y$10$5ysCvqUiloxmtRo2comd9uaiaNkLJ0eiW6x5pDFGWESAbXr5jm5N6' in 'where clause' (SQL: select * from users where email is null and $2y$10$5ysCvqUiloxmtRo2comd9uaiaNkLJ0eiW6x5pDFGWESAbXr5jm5N6 is null limit 1)
Can please somebody help me this.
Thank you :)
This error happens because you are misusing the request() helper method.
If you do like you did:
request(['email','password' => Hash::make(request('password'))])
It will produce this array:
['email' => 'myemail#lol.com', 's9audh87h2ueahjdbas' => null]
The request() method expects an array with its values representing the name of the inputs provided in the request. So if you give it an associative array, it will ignore completely the keys and it will only grab its values.
So, to get the input from the request and use it in the authorization attempt, you have to do like this:
$userInput = request('email', 'password');
if (! auth()->attempt($userInput))
{
//do something.
}
This is true assuming that your user table has an email and password columns.
Try this:
//Attempt to auth the user
if(! auth()->attempt([
'email' => request()->get('email'),
'password' => request()->get('password')
])
{
//If not redirect back
return back()->withErrors([
'message' => 'Please check your email or password'
]);
}
a) You don't need to hash the password, Laravel does that already
b) I'm not sure what you were passing as parameters. Some type of mixed array

How Can I manual pass information to the register controller in Laravel

To create a user without the end-user having to type in the details.
Something akin to
User::classcreate([
'email' => $email,
'password' => $rand_pass,
.....
]);
Thanks for the ideas and feedback in advance :)
The use case is.
The end-user invites another user to use the service by typing in their email and it creates a user with a random password before sending a email to the new user with their created details.
You're almost there with your code. It should look like this:
User::create([
'email' => $email,
'password' => bcrypt($rand_pass),
]);
But if you want to hash the password, you should also send the password to that user through email (which is not very secure). When the users logs in for the first time, you should at least require him to change the password.
You can simply use create() method of your User model:
$userData = [
'email' => 'some#email.com',
'password' => 'somepassword'
];
$newUser = User::create($userData);
You'll also need your password hashed in order for it to work with Laravel's authorization. Add the following to your user model - it will hash password before it's saved to the database:
public function setPasswordAttribute($password)
{
$this->attributes['password'] = Hash::make($password);
}

Get a user by username and password with Laravel

I need to do some extra checks on a user, I would like to get the user by username and password.
Firstly:
Is there a built in function that gets a user by username and password without authenticating them?
Secondly:
If the above is no, then how do I correctly hash the password, because if I use Hash::make( $password ) and then compare to the database, it is not the same... You would usually use Hash::check but I need to actually get the user by username and password.
In Laravel 5.2
You can use Auth::once($credentials) to validate credentials and thereafter Auth::getUser(); to get the user.
$credentials = Request::only('username', 'password');
if(!Auth::once($credentials)) {
// Invalid user credentials; throw Exception.
}
$user = Auth::getUser();
First:
If you want to check if user data to authentication is correct you can use:
if (Auth::validate($credentials))
{
//
}
But if you want to get user from database with user and password, you can use:
$user = User::whereName($username)->wherePassword(Hash::make($password))->first();
Second
To store password in database you should use Hash::make($password) as you showed and it works without any problems. Using Auth::validate should solve the issue.
Yes, there is a built in function you should use. I recommend you to read the docs. But here's a good example, it's pretty self-evident:
$input = array(
'username' => Input::get('username'),
'password' => Input::get('password'),
);
$remember = (boolean)Input::get('remember'); //'Remember me' checkbox
if (Auth::attempt($input, $remember)) {
return Redirect::intended('dashboard')->with("success", "You're logged in!"); //Redirect the user to the page intended to go to, with the dashboard page as default
}
Registering a user looks something like this:
$input = array(
'username' => Input::get('username'),
'email' => Input::get('email'),
'password' => Hash::make(Input::get('password')) //Encrypt password
);
$user = User::create($input);
I also recommend you to read about input validation. I hope this helps, good luck.
Edit: I didn't read the "without authenticating them" part. You should use Auth::validate($input) as Marcin already explained.
Laravel 5.7
To check a users credentials without logging them in, I had to do this:
$user = User::whereEmail($request->email)->first();
$user = password_verify($request->password, optional($user)->getAuthPassword()) ? $user : false;
Laravel auth validation makes use of https://www.php.net/manual/en/function.password-verify.php

laravel auth::attempt() gives error when specifying a known username

Hi i'm new to laravel and was trying to use the auth system.
the problem i'm having is that when verifying the users username and password by auth::attemp() I end up with a 'whoops something went wrong page' but only when there is a known username being specified. When i'm inputting an unkown username i get the expected error page.
i've created an user by:
// add user
$user = new User;
$user->username = 'John';
$user->password = Hash::make('Doe');
$user->email = 'info#mail.com';
$user->save();
the route i've created for the post method looks like this:
Route::post('login', function(){
$credentials = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
// check credentials
if (Auth::attempt($credentials)) {
// go to index if login is successful
return Redirect::to('/');
}
// fail
return Redirect::to('login')->with('error' , 'Wrong username or password');
});
Now as i've said the error message is being shown if I specify an username wich is not (in this case) 'John'. when is do use 'John' I end up with an error no matter the password is correct or incorrect.
my question: What am i doing wrong here? and how to fix it?
It turned out to be a really stupid mistake. I edited the User model (app/models/User.php), this was causing the problem. I've reverted back to the original and everything works fine now!
Thanks for the help guys!

Changing yii default username field to email field for login

To login in yii, i want to do it on a database table (mysql) instead of the default "admin" and "demo" default values provided in the UserIdentity class. The thing is, my user table in the database that i created does not use username and password fields for authentication, but rather email and password fields. So when i change all the "username" to "email" variables in UserIdentity.php, LoginForm.php, SiteController.php, login.php, i get an error:
Property "UserIdentity.email" is not defined.
and the error points to the change i made here by replacing username with email as shown below:
public function authenticate()
{
$user = User::model()->findByAttributes(array(
'email' => $this->email
));
Help on this would be much appreciated.
You may change email on login in UserIdentity.
$user = User::model()->findByAttributes(array(
'email' => $this->username
));
Or add new field 'email' to UserIdentity and use it.

Categories