I'm trying to make a custom login function in Laravel 4 but I don't know how to compare password with hash password that has been stored.
This is my controller
public function login($email,$password)
{
$user=UserMobile::where('email','=',$email)->count();
if($user==0)
{
return Response::json("Invalid");
}
else
{
$user=UserMobile::where('email','=',$email)->first();
if(Hash::check($password,$user->password))
{
return Response::json($user);
}
else
{
return Response::json("Error");
}
}
}
When I try to check it in postman, it keeps returning the return Response::json("Error");. Can somebody help please? thanks in advance
Related
I'm Using codeigniter framework and i'm having a problem with a session.. I was trying to create login page, but there was a problem when I used set_userdata().
It doesn't work. When I checked, there was no session created.
Is there any wrong with my code?? Any one has same problem with it??
function __construct(){
$this->CI=&get_instance();
$auth=$this->CI->config->item('auth');
$this->CI->load->helper('cookie');
$this->CI->load->library('session');
$this->CI->load->model('Auth_users_model');
}
function login($username,$password){
$result=$this->CI->Auth_users_model->get_login_info($username);
if($result){
$password=md5($password);
if($password==$result->password){
$user_ses=array('user_id_hcp'=>$result->id,'user_access_hcp'=>$result->access, 'user_username_hcp'=>$username);
$this->CI->session->set_userdata($user_ses);
return TRUE;
}else{
return FALSE;
}
}else{
return FALSE;
}
}
}
Instead of $this->CI->session->set_userdata($user_ses);
try using simply
$this->session->set_userdata($user_ses);
I have ACL set up with laravel. I have the following helper:
function user($user_id = null)
{
if (!$user_id) return Auth::user();
return User::where('id', $user_id)->first();
}
I have this helper function so I don't have to type the long Auth::user().
Here's the problem. I have ACL set up so when I do something like the following in my model and the user is not logged in, I get an error.
if (user()->can('edit-blog')) {
echo 'you can edit blog!';
}
This is the error if the user is not logged in:
Call to a member function can() on null
Now, I have the following code everywhere in my site. I am using ACL in almost every view and model and controller. I don't want to have to do something like the following just to not get the error.
if (user() && user()->can('edit-blog')) {
// do stuff
}
How can I return false if there is a "null" error or an exception?
** What I have tried **
I have tried try/catch but it does not do anything:
try {
if (!$user_id) return Auth::user();
return User::where('id', $user_id)->first();
} catch (Exception $ex) {
return false;
}
Thank you for your support.
You need to check if the user is logged in before calling can
if(Auth::check() && user()->can('edit-post')){
Return true;
}else{
Return false;
}
I have a problem on getting a property from model User . Simply the code should get the type of user whether its admin or normal user then each of them redirect to their controllers .
code at controller :
public function actionIndex()
{
if (Yii::$app->user->isGuest) {
return $this->render('index');
} else {
// Administrator
if (Yii::$app->session->get('user.type') == 1) {
return $this->redirect('admin');
//user
Yii::$app->session->get('user.type') == 2) {
return $this->redirect('normal-user');
}
else {
Yii::$app->user->logout();
return $this->refresh();
}
}
}
This type is from My database table and it only have two values "1" or "2" . when i check this its getting "0" don't know how .so it always logout() any suggestions might be not taken on my consideration !!!
What If I have an input field for example:
UserEmail:
UserPhoneNumber:
UserOldPasword:
UserNewPassword:
UserRetypePassword:
And on my action update I will only require UserNewPassword if UserOldPassword is not empty else the only thing that needs to be updated is either UserEmail or UserPhoneNumber. How will I implement this rule?only on update? Or should I create a custom validator on my model?
so on my afterFind() I have this code to avoid the output of the hashed password:
public function afterFind()
{
//reset the password to null because we don't want the hash to be shown.
$this->old_password = $this->password;
$this->password = null;
parent::afterFind();
}
and I created a custom validation. it does validates, the problem is even if I submitted the form with empty UserNewPassword I still get an error and $this->password field is now returning a hashed value from my database
Please see my code and correct my mistakes:
public function checkForEmpty($attribute,$params){
if(!empty($this->$attribute)){
if(empty($params['oldPassword'])){
$this->addError('oldPassword','We require your old password to proceed changing password');
}
if(empty($params['retypePassword'])){
$this->addError('retype_password','To assure that you type the right password please retype your password');
}
if(!empty($params['oldPassword']) && !empty($params['retypePassword'])){
$this->compareOldPass($params['oldPassword'],$this->old_password);
}
}
}
Thanks in advance...
you can do a beforeSave() in your model. I think it's the best solution because of your complicated logic, and not being a global issue of your app.
UPDATE:
public function beforeSave()
{
if(parent::beforeSave())
{
//implement you logic here
//or check it is a new record
if($this->isNewRecord)
{
// do stuff here if new
}
//or you can return false if it doesn't meet your logic
return true;
}
else
return false;
}
user_model.php
class User_model extends CI_Model{
function get_fullname_by_username($username){
$query=$this->db->select('user_first_name,user_last_name')->where('user_name',$username)->get('user');
if($query->num_rows()==0){
return FALSE;
} else {
return $query->row();
}
}
}
post_model.php
class Post_model extends CI_Model{
function input_post($content,$privacy==FALSE){
$this->load->library('privacy');
$this->load->helper('post');
$uid=uid();//user id based on sessions
if($privacy==FALSE){
$privacy=$this->privacy->post_privacy($uid);
} else {
$privacy=$privacy;
}
$content=mention($content);
$input=array('post_uid'=>$uid,'post_content'=>$content,'post_privacy'=>$privacy);
if($this->db->insert('posts',$input)){
return $this->fetch_single_post_data($this->db->insert_id());
} else {
return FALSE;
}
}
function fetch_single_post_data($post_id){
$query=$this->db->select('id,post_uid,post_content,post_privacy,post_created')->where('id',$post_id)->get('posts');
if($query->num_rows()==0){
return FALSE;
} else {
return $query->row();
}
}
}
post_helper.php
function get_mention_name($username){
$username=strtolower($username);
$CI=&get_instance();
$CI->load->model('user_model');
$name=$CI->user_model->get_fullname_by_username($username);
if($name==FALSE){
return "#".$username;
} else {
return "{$name->user_first_name} {$name->user_last_name}";
}
}
function mention($post_content){
return preg_replace_callback("REGEX","get_mention_name",$post_content);
}
First off all, English isn't my native language. So, please if my grammar is bad forgive me.
For my school final project i just want to create Facebook like website (social networking). My problem is, i want to create mention feature, based on username (user database). If at Facebook after I type the # symbol, Facebook system begin to query most possibly Friend/Page with cool ajax list display. But i don't want be like that, at my system there's no ajax list display.
If user post status update with string like #bias or #tegaralaga or #admin "#tegaralaga where are you?" for the example. My system check on database is there any user with username #tegaralaga, if yes based on user_model.php function get_fullname_by_username(); it will return user_first_name and user_last_name data. But if no it will give FALSE return. On my user table there's user with tegaralaga username, the user_first_name is Bias and user_last_name is Tegaralaga.
Move at post_helper.php, if $name==FALSE it will give current string, #tegaralaga. But if the username is exists, it will return "{$name->user_first_name} {$name->user_last_name}".
If exists, the string become
"Bias Tegaralaga where are you?"
If doesn't, the string still
"#tegaralaga where are you?"
So my question is :
1. Is it possible with my code above using preg_replace_callback? (take a look at post_helper.php)
2. If possible, what is the perfect REGEX if we can mention more than 1 username, and the exception for email address (because email address contains # symbol too)
This should work for you.. on your callback function you receive all the matches from the regexp.. you need to extract the part you need:
function get_mention_name($match){
$username=strtolower($match[1]);
$CI=&get_instance();
$CI->load->model('user_model');
$name=$CI->user_model->get_fullname_by_username($username);
if(empty($name)){
return "#".$username;
} else {
return "{$name->user_first_name} {$name->user_last_name}";
}
}
function mention($post_content){
return preg_replace_callback(
"#(?<!\w)#(\w+)#",
"get_mention_name",
$post_content);
}