简体   繁体   English

Laravel 4 Auth :: attempt在正确值上返回false

[英]Laravel 4 Auth::attempt returns false on correct values

I have login code that is supposed to work by attempting to authenticate the user using Laravel's Auth::attempt() method. 我有登录代码,该代码应通过尝试使用Laravel的Auth::attempt()方法对用户进行身份验证来起作用。 This code works on another site of mine, I have altered it as instead of the Password in the database, it is stored as passwdEncrypted . 该代码在我的另一个站点上有效,我将其更改为数据库中的passwdEncrypted ,而不是数据库中的Password I cannot change it as the database is in use by another application as well. 我不能更改它,因为该数据库也被另一个应用程序使用。

The code is below: 代码如下:

// check if in database
        $isInDb = User::where('ReferenceCode', $username)->first();
        if($isInDb) {
            // is in database
            // check if password is encrypted yet
            if($isInDb->passwdEncrypted) {
                // password is encrypted
                if(Auth::attempt(array('ReferenceCode' => $username, 'passwdEncrypted' => $password))) {
                    // authenticated
                    $array = array();
                    $array['verified'] = 'true';
                    $array['type'] = Auth::user()->UserType;
                    return Response::json($array);
                } else {
                    // not authenticated
                    $array = array();
                    $array['verified'] = 'false';
                    $array['type'] = $type;
                    return Response::json($array);
                }
            } else {
                // password is not encrypted
                // check if plain text password is correct
                if($isInDb->Password == $password) {
                    // plain text password is correct
                    $hashed = Hash::make($password);
                    $arr = array('passwdEncrypted' => $hashed);
                    $updated = User::where('rsmsOnlineUserID', $isInDb->rsmsOnlineUserID)->update($arr);
                    if($updated) {
                        $newUser = User::find($isInDb->rsmsOnlineUserID);
                        echo $newUser->passwdEncrypted;
                        if(Auth::attempt(array('ReferenceCode' => $username, 'passwdEncrypted' => $password))) {
                            echo 'logged in';
                        } else {
                            dd(DB::getQueryLog());
                            echo 'could not log in';
                        }
                    } else {
                        echo 'did not update';
                    }
                } else {
                    // plain text password is incorrect
                    $array = array();
                    $array['verified'] = 'false';
                    $array['type'] = $type;
                    return Response::json($array);
                }
            }
        } else {
            // not in database
            return Respone::json(array('success' => 'false'));
        }

What is happening: I can't log in, the username and password in the database is 1234 , even if I hard code that, it does not work. 发生了什么:我无法登录,数据库中的用户名和密码为1234 ,即使我对此进行了硬编码,也无法正常工作。

It first checks to see if the user is in the database, if it is, it checks to see if there is an encrypted password, if there is not, it will create one from the password given if it matches the plain text password in the database and then log the user in (I have no choice but to have the plain text password stored in the database, that is how they want it). 它首先检查用户是否在数据库中,如果存在,则检查是否存在加密密码,如果没有,则将根据给​​定的密码创建一个与密码中的纯文本密码匹配的密码。然后将用户登录(我别无选择,只能将纯文本密码存储在数据库中,这就是他们想要的方式)。

But it returns the {"verified":"false","type":"prospective_employee"} from the not authenticated part of the code. 但是它从not authenticated的代码部分返回{"verified":"false","type":"prospective_employee"} So neither of the Auth::attempt() blocks work. 因此, Auth::attempt()块都不起作用。

I was logging them in manually but even Auth::login() won't work. 我是手动登录的,但是即使Auth::login()也不起作用。

I have the following in my User model (with the main database table): 我的User模型中有以下内容(包含主数据库表):

public function getAuthPassword()
{
    return $this->Password;
}

/**
 * Get the token value for the "remember me" session.
 *
 * @return string
 */
public function getRememberToken() {
    return $this->remember_token;
}


public function setRememberToken($value) {
    $this->remember_token = $value;
}


public function getRememberTokenName() {
    return 'remember_token';
}

/**
 * Get the e-mail address where password reminders are sent.
 *
 * @return string
 */
public function getReminderEmail()
{
    return $this->email;
}

Please note that there is a field in the table called Password , but that is the plain text password, I need to authenticate against the passwdEncrypted field. 请注意,表中有一个名为Password的字段,但这是纯文本密码,我需要针对passwdEncrypted字段进行身份验证。

You cannot do this with Laravel, and for good reason, but it is ridiciously unsecure and dangerous. 出于充分的原因,您不能使用Laravel进行此操作,但这是可笑的不安全且危险的。

I have no choice but to have the plain text password stored in the database, that is how they want it 我别无选择,只能将纯文本密码存储在数据库中,这就是他们想要的方式

I dont understand - why are you storing BOTH an "unencrypted" and "encrypted" password? 我不明白-为什么您要同时存储“未加密”和“已加密”密码? There is no point. 无关紧要。 You should only ever store encrypted passwords. 您应该只存储加密的密码。 There is no need for any other way, and you need to educate the people as to why. 不需要任何其他方式,您需要教育人们为什么。

This code works on another site of mine, I have altered it as instead of the Password in the database, it is stored as passwdEncrypted. 该代码可在我的另一个站点上运行,我将其更改为数据库中的passwdEncrypted而不是数据库中的密码。 I cannot change it as the database is in use by another application as well. 我不能更改它,因为该数据库也被另一个应用程序使用。

The Laravel Auth code is hard coded to use the "password" column. Laravel Auth代码经过硬编码以使用“密码”列。 You cannot simply change it to another colum. 您不能简单地将其更改为另一个列。 That is why your code is failing. 这就是您的代码失败的原因。

Since you are not using the password column, and since you are not using encrypted passwords, you might as well just create your own unsecure login system, customised to suit your requirements. 由于您没有使用密码列,也没有使用加密的密码,因此您最好只创建自己的不安全的登录系统,并根据需要进行定制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM