Laravel change password issue - laravel

For Laravel change password I did like this but why it is not working.. Its not updating password. I have done login page, registration everything working. But this giving me lot of trouble. Below is my code.
$returnValue = DB::table('users')->where('users_id', $users_id)->where('password', bcrypt($request->opassword))->update(['password'=>bcrypt($request->npassword)]);
if($returnValue >= 1)
{
$success['message'] = "Password updated successfully..";
return $this->sendResponse($success);
}
else
{
$error = "Entered Old password is not valid..";
return $this->sendResponse($error);
}

At first import Hash at the top of the controller. like this below
use Illuminate\Support\Facades\Hash;
After this , validate old password match with your database like this below
$user = User::findOrFail($users_id);
if (Hash::check($request->password, $user->password)) {
$user->password = Hash::make($request->opassword);
$user->update(); // $user->save();
$success['message'] = "Password updated successfully..";
return $this->sendResponse($success);
} else {
$error = "Entered Old password is not valid..";
return $this->sendResponse($error);
}
Try this.

You have issues with your keys:
$returnValue = DB::table('users')
->where('id', $users_id)
->where('password', bcrypt($request->opassword))
->update(['password'=>bcrypt($request->npassword)]);

Related

Laravel Date assign errorr

I have small problem in app. Backend is Laravel and Front end is Nuxtjs.
When User registered on app,then users must be wait antill Administartor approved.
When Admin approved this user we give them 3 months subscription.
In my code this part is not working.
$user->activated_at = now();
$user->activated_at = date('Y-m-d H:i');
{
$user = User::find($id);
if (is_null($user)) {
return $this->sendError('admin_messages.user_not_found');
}
$user->status = User::ACTIVE;
$user->activated_at = now();
$user->activated_at = date('Y-m-d H:i');
dd($user->activated_at);
$user->save();
Mail::to($user->email)->queue(new ApproveNotificationMail($user));
return $this->sendResponse('admin_messages.user_activated');
}
$user->activated_at = \Carbon\Carbon::now()->format('Y-m-d H:i');
You can use mutator in your User model, to force activated_at format:
public function setActivatedAtAttribute( $value ) {
$this->attributes['activated_at'] = (new Carbon($value))->format('Y-m-d H:i');
}
You can define a format for date columns using:
protected $dateFormat = 'Y-m-d H:i';
Note that you can directly use:
$user->activated_at = \Carbon\Carbon::now()->format('Y-m-d H:i');

Auth with laravel

I do not know why the auth of laravel not work to me.. I have tried all but it still does not work, I use this code to login:
$user = User::where('rut', $request->rut)
->where('password', md5($request->password))
->first();
$employee = Employee::where('id_user', $user->id_user)
->first();
$permissions = User_Type_Permission::where('id_user_type', $user->id_user_type)->pluck('id_permission')->toArray();
$request->session()->put('id_user_type', $user->id_user_type);
$request->session()->put('id_branch_office', $employee->branch_office);
$request->session()->put($permissions);
if(Auth::login($user))
{
echo 1;
die();
return redirect('/account');
}
else
{
echo 2;
die();
return redirect('/login');
}
It returns "2" every time, and the user is not empty, it comes with values from database
I have used Attempt too:
$user = User::where('rut', $request->rut)
->where('password', md5($request->password))
->first();
$employee = Employee::where('id_user', $user->id_user)
->first();
$permissions = User_Type_Permission::where('id_user_type', $user->id_user_type)->pluck('id_permission')->toArray();
$request->session()->put('id_user_type', $user->id_user_type);
$request->session()->put('id_branch_office', $employee->branch_office);
$request->session()->put($permissions);
$credentials = [
'rut' => $user->rut,
'password' => md5($request->password),
];
if(Auth::attempt($credentials))
{
echo 1;
die();
return redirect('/account');
}
else
{
echo 2;
die();
return redirect('/login');
}
and it returns false too, the login information is correct, but Auth does not work at all, what can it be?
Thanks!
You should remove md5.. if you store the hashed password by bcrypt..
Auth::attempt() automatically check the plain text password in the request with the hashed in the database.
So the code should be
$credentials = [
'rut' => $user->rut,
'password' =>$request->password,
];
if(Auth::attempt($credentials))
{
echo 1;
die();
return redirect('/account');
}else{return redirect('/login')}
And put all the code you have written in the top to the true condition..
what you write is not correct ..
if authenticated make what i want..
Not put the code then check if authenticated!
I Hope this work with you!
Good luck!

resetting password in codeigniter

i'm new to codeigniter, and i am attempting to create a password reset system
this is my controller:
public function changePassword(){
if($this->session->userdata('loginuser'))
{
$session_data = $this->session->userdata('loginuser');
$email = $this->session->userdata('email');
$data['email'] = $email;
$data['title'] = 'Change my Password | Watch Stop';
$this->load->view('template/header', $data);
$this->load->view('watch_stop/vpassword', $data);
$this->load->view('template/footer');
}
else
{
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
public function reset_password(){
if($this->session->userdata('loginuser'))
{
$session_data = $this->session->userdata('loginuser');
$email = $this->session->userdata('email');
$data['email'] = $email;
//validating form
$this->form_validation->set_rules('old_password','Old Password','trim|required|min_length[5]|md5');
$this->form_validation->set_rules('new_password','New Password','trim|required|min_length[5]|matches[cnew_password]|md5');
$this->form_validation->set_rules('cnew_password','Confirm Password','trim|required||md5');
if ($this->form_validation->run() == FALSE)
{
$this->changePassword();
//$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Failed to update password</div>');
}else {
$query=$this->customer_model->change_password();
$data = array( "main_content" => 'includes/memberadmin/memberadmin_cpass_process',
"query" => $query
);
$this->load->view('includes/memberadmin/template',$data);
}
}
else
{
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
this is my model:
function change_password(){
$this->db->select('id');
$this->db->where('email',$this->session->userdata('email'));
$this->db->where('password',$this->input->post('old_password'));
$query=$this->db->get('user');
if ($query->num_rows() > 0)
{
$row = $query->row();
if($row->email===$this->session->userdata('email'))
{
$data = array(
'new_password' => $this->input->post('new_password')
);
$this->db->where('email',$this->session->userdata('email'));
$this->db->where('new_password',$this->input->post('old_password'));
if($this->db->update('user', $data))
{
return "Password Changed Successfully";
}else{
return "Something Went Wrong, Password Not Changed";
}
}else{
return "Something Went Wrong, Password Not Changed";
}
}else{
return "Wrong Old Password";
}
}
When i click on the update button in my reset password page, i am getting the following error for my new password confirmation field: Unable to access an error message corresponding to your field name Confirm Password.()
please help!
1) there are two pipe signs near required||md5
$this->form_validation->set_rules('cnew_password','Confirm Password','trim|required||md5');
change it to
$this->form_validation->set_rules('cnew_password','Confirm Password','trim|required|md5');
2) changing input to md5 at this stage is not good.
You have to use password_hash function.
Read More >> http://php.net/manual/en/function.password-hash.php
3) You forgot to load model. $this->load->model('customer_model');

Session always returns null facebook php sdk

Hi I'm trying to get data from a user from facebook after he logs in whit facebook. But my session variable is always null .I'm currently using the laravel 4 framework. The code that you are seeing is being called as the callback function from facebook login.
$code = Input::get('code');
if (strlen($code) == 0) return Redirect::to('/')->with('message', 'There was an error communicating with Facebook');
$facebook = new Facebook(Config::get('facebook'));
$uid = $facebook->getUser();
if ($uid == 0) return Redirect::to('/')->with('message', 'There was an error');
$me = $facebook->api('/me');
$profile = Profile::whereUid($uid)->first();
if (empty($profile)) {
$user = new User;
$user->name = $me['first_name'].' '.$me['last_name'];
$user->email = $me['email'];
$user->save();
$profile = new Profile();
$profile->uid = $uid;
return $uid;
$profile->username = $me['name'];
$profile = $user->profiles()->save($profile);
}
$profile->access_token = $facebook->getAccessToken();
$profile->save();
$user = $profile->user;
Auth::login($user);
FacebookSession::setDefaultApplication(
Config::get('facebook.appId'),
Config::get('facebook.secret')
);
$helper = new FacebookRedirectLoginHelper('http://projectweb.app:8000/');
$session = $helper->getSessionFromRedirect();
if(isset($session))
{
$request = new FacebookRequest($session,'GET','/me');
$response = $request->execute();
$graph = $response->getGraphObject(GraphUser::classname());
$name = $graph->getName();
return $name;
}
else
{
return 'no sesssion';
}
You better try using a Laravel specific Package for that, here it is one, where you also have the details about installing and using it.
https://github.com/SammyK/LaravelFacebookSdk

Laravel 4: Updating a user

I'm trying to update the password of a user I've pulled from the database in Laravel 4. Here is what I have:
$user = User::where('username', $username)->get();
if (!empty($user)) {
$user->password = Hash::make($password);
$user->save();
}
But Laravel is telling me save is an unknown method. What am I doing wrong here?
get returns an array of users. You need to use first to get a single user object that will allow you to adjust the password and save the record.
if ($user = User::where('username', $username)->first())
{
$user->password = Hash::make($password);
$user->save();
}
Are you sure $user actually has a 'user' in it?
It wont be 'empty' - it will be BOOL false. Try var_dump($user) and confirm something is returned?
Try this:
$user = User::where('username', $username)->get();
var_dump($user);
if ($user) {
$user->password = Hash::make($password);
$user->save();
}

Resources