Why Laravel 5 hashes password twice? - php

I set hash mutator for User model:
public function setPasswordAttribute($value)
{
\Log::info('Hash');
$this->attributes['password'] = \Hash::make($value);
}
Then I have a register function. The password hashes twice (checked in logs)
public function register(RegisterNewUser $request)
{
$user = new User();
$user->first_name = $request->first_name;
$user->last_name = $request->last_name;
$user->email = $request->email;
$user->password = $request->password;
$user->admin = true;
$user->save();
$this->auth->login($user);
return redirect()->route('dashboard');
}
Actually the function was originally written like this:
$user = new User($request->all());
$user->admin = true;
$user->save();
With the same result. Password gets hashed twice and I can't figure why.
I even tried:
get('test', function() {
$user = \CRM\EloquentModels\User::create([
'email' => 'zzz#zzz.zzz',
'last_name' => 'zzz',
'password' => 123
]);
});
And it gets hashed twice. No idea...

Related

credentials do not match our records

I am currently working on POS in laravel 8. I am having an error when I login in that "credentials do not match our records" When I login in through the default Register page than it works fine but when I register through internal panel than the data goes to the database but I face error when I log in.
Code for registration through panel:
public function store(Request $request)
{
$users = new User;
$users->name = $request->name;
$users->email = $request->email;
$users->password = Hash::make($request->name);
// $users->password = bcrypt($request->name);
$users->is_admin = $request->is_admin;
$users->save();
if($users){
return redirect()->back()->with('User Created Successfully');
}
return redirect()->back()->with('User Failed Created');
}
Default register code:
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
Your panel registration form is hashing the name input and not the password input.
$users->password = Hash::make($request->name);
Should probably be
$users->password = Hash::make($request->password);
#Code for registration through panel code should be like this#
public function store(Request $request){
$users = new User;
$users->name = $request->name;
$users->email = $request->email;
$users->password = Hash::make($request->password);
$users->is_admin = $request->is_admin;
$users->save();
if($users){
return redirect()->back()->with('User Created Successfully');
}
return redirect()->back()->with('User Failed Created');
}

Creating default object from empty value laravel 5.7

I have an error in my update user admin form where I want to update the profile that's corresponding to the user if he has a profile. But it throws this error:
Creating default object from empty value
I have set relationships in my model between the User and profile Profile::where('id', $user->profile_id)->first(); but when I dd it's always a null, even if there is a corresponding profile.
User.php
public function profile()
{
return $this->hasOne('App\Profile', 'user_id', 'id');
}
Profile.php
public function user()
{
return $this->belongsTo('App\User');
}
function error is in
public function update(Request $request, User $user)
{
if(\Auth::check()) {
if(\Auth::user()->type == 'admin') {
$validated = $request->validate([
'name' => 'required',
'email' => 'required|email',
'password' => 'confirmed'
]);
if(!empty($validated['password'])){
if(!$user->profile){
//Has no profile
$user->name = $validated['name'];
$user->email = $validated['email'];
$user->password = bcrypt($validated['password']);
$user->update();
} else {
//Has profile
$profile = Profile::where('id', $user->profile_id)->first();
$profile->username = $validated['name'];
$profile->email = $validated['email'];
$profile->update();
$user->name = $validated['name'];
$user->email = $validated['email'];
$user->password = bcrypt($validated['password']);
$user->update();
}
} else {
if(!$user->profile){
//Has no profile
$user->name = $validated['name'];
$user->email = $validated['email'];
$user->update();
} else {
//Has profile
$profile = Profile::where('id', $user->profile_id)->first();
$profile->username = $validated['name'];
$profile->email = $validated['email'];
$profile->update();
$user->name = $validated['name'];
$user->email = $validated['email'];
$user->update();
}
}
}
}
}
on these lines $profile = Profile::where('id', $user->profile_id)->first();
Since you are using user_id as a foreign key in the profiles table, you would expect this as your statement:
Profile::where('user_id', $user->id)->first();
Or, by using the relation:
$user->profile;
You can just use your relation and also compress the logic as :
$user->name = $validated['name'];
$user->email = $validated['email'];
if(!empty($validated['password'])){
$user->password = bcrypt($validated['password']);
$profile = $user->profile;
$profile->username = $validated['name'];
$profile->email = $validated['email'];
$profile->save();
}
$user->save();

Laravel hash password in controller

I want to make a password hash inside the Controller so that the information is stored to the database.
My storing function is;
public function store(Request $request)
{
$validator = \Validator::make($request->all(), ['fullname' => 'required',
'email' => 'required|email|max:255|unique:users',
'username' => 'required|max:255|unique:users',
'password' => 'required',]);
if ($validator->fails()) {
return response()->json([
'1' => 'Your information not stored!',
]);
}
else {
$newuser = new User;
$newuser -> fullname = request('fullname');
$newuser -> email = request('email');
$newuser -> password = request('password');
$newuser -> username = request('username');
$newuser -> status = 1;
$newuser -> role_id = 1;
$newuser -> save();
return response()->json([
'2' => 'Your information stored successfuly!',
]);
}
This works correctly. But, I want to hash the password.
https://laravel.com/docs/5.0/hashing
https://laracasts.com/discuss/channels/requests/how-to-hash-user-input-password-when-using-form-validation-in-form-request-laravel-5
public function setPasswordAttribute($value)
{
$this->attributes['password'] = bcrypt($value);
}
This does not work. Does any of you have any suggestions?
just change
$newuser -> password = bcrypt(request('password'));
Change:
$newuser->password = request('password');
To:
$newuser->password = bcrypt(request('password'));
See the docs

Unexpected T Variable Laravel

I'm getting an error on the following on:
$user->email = Input::get('email');
I'm really unsure what is wrong with the code, it seems perfectly fine. I looked up t variable errors, simply involve missing a bracket or semi colon. But as far as I'm aware it seems fine.
If anyone could help me out, that would be great.
If there is any other code, could you list it as a comment and i'll happily add it.
Thanks!
public function doRegister()
{
$rules = array(
'name' => 'required|min:3', // name
'email' => 'required|email', // make sure the email is an actual email
'password' => 'required|alphaNum|min:3' // password can only be alphanumeric and has to be greater than 3 characters
);
// run the validation rules on the inputs from the form
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()){
// validation not successful, send back to form
Redirect::back()->withErrors;
} else {
$user = Input::all();
User::addNewUser();
if (Auth::attempt($user)) {
return Redirect::to('member');
}
}
}
User model
public static function addNewUser()
{
$user = new User;
$user->name     = Input::get('name');
$user->email    = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->save();
}
It's because of $user->save; it's a method not a property and it should be called like
$user->save();
Instead of
$user->save;
Update : Also, it's U not u
$user = new user;
should be
$user = new User; // capital U
Also, after if ($validator->fails())
Redirect::back()->withErrors;
should be
return Redirect::back()->withErrors($validator);
Update : So, after fixing 3 errors (so far), your full code should be
public function doRegister()
{
$rules = array(
'name' => 'required|min:3',
'email' => 'required|email',
'password' => 'required|alphaNum|min:3'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()){
return Redirect::back()->withErrors($validator);
}
else {
$user = new User;
$user->name =Input::get('name');
$user->email= Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->save();
if (Auth::attempt($user)) {
return Redirect::to('member');
}
}
}

Auth::attempt($credentials) always returns false

Hi there I am new to laravel and I am trying to code functionality of my login form here are the codes:
this is how I create new user (works well)
public function action_newuser()
{
$email = Input::get('email');
$password = Input::get('password');
$new_user = Input::get('new_user', 'off');
$first_name= Input::get('firstname');
$last_name= Input::get('last_name');
$username= Input::get('username');
$user = new User();
$user->email = $email;
$user->password = Hash::make($password);
$user->first_name =$first_name;
$user->last_name= $last_name;
$user->username= $username;
try{
$user->save();
}
catch(Exception $e) {
echo "Failed to create new user!";
}
This is my login function:
public function action_login()
{
$password = Input::get('password');
$username= Input::get('username');
$remember= Input::get('remember', 'off');
if($remember =='off') $remember=FALSE;
if($remember =='on') $remember=TRUE;
$credentials = array(
'username' => $username,
'password' => $password,
'remember' => $remember
);
if( Auth::attempt($credentials))
{
return Redirect::to('dashboard/index');
}
else
{
#I put this line to check are the values passed from my form are correct.
echo $password.' ' .$username.' '. $remember;
}
}
When I submit login form it always shows a blank page has values of $password, $username and $remember values. This means Auth::attempt() is not working well.
What can be the problem?
Fool me!
Although I have checked it many times, I could not see the line 'username' => 'email', in auth config file.
it should be 'username' => 'username', since I am going to use username for login.
Check to make sure that your database password field is 60 characters.
To all laravel 4 developers who are facing the Auth::attempt() login failure with valid creditials!
Solution:
In app/config/app.php change type: 'cipher' => MCRYPT_RIJNDAEL_128 to 'cipher' => MCRYPT_RIJNDAEL_256 and re-hash all passwords
In Model add method:
public function setPasswordAttribute($pass) {
$this->attributes['password'] = Hash::make($pass);
}
In UserController:
//Create User or Register
public function postCreate() {
$input = Input::all();
$user = new User;
$user->username = Input::get('username');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user = User::create($input);
return Redirect::action("Frontend\HomeController#index");
}
//Login or Sign in
public function postSignin() {
$userdata = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
if (Auth::attempt($userdata, true)) {
Session::put('username', Auth::user()->username);
return Redirect::action("Frontend\HomeController#index");
} else {
return Redirect::action("Frontend\UserController#getLogin")
->with('message', 'Your username/password combination was incorrect')
->withInput();
}
Hope i have helped someone out there

Categories