簡體   English   中英

Laravel 4-Auth :: attempt()無法正常工作

[英]Laravel 4 - Auth::attempt() not working

我一直在到處搜索,但無法弄清楚為什么Auth :: attempt()一直失敗。 請幫幫我。

這是我的表格:

<h2>Admin Login</h2>
<form method="post" action="<?php echo url('login-process') ?>">
    Username: <input type="text" name="username" /> <br/>
    Password: <input type="password" name="password" />
    <button type="submit">Login</button>
</form>

這是我的登錄功能:

$username = Input::get('username');
$password = Input::get('password');

if (Auth::attempt(array('username' => $username, 'password' => $password))) {

        echo "success!"; //just for testing purposes
} else {
        echo "fail!";

我的User.php是默認的。 看起來像這樣:

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {


/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users';

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('password');

/**
 * Get the unique identifier for the user.
 *
 * @return mixed
 */
public function getAuthIdentifier()
{
    return $this->getKey();
}

/**
 * Get the password for the user.
 *
 * @return string
 */
public function getAuthPassword()
{
    return $this->password;
}

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

}

這就是我保存新用戶信息的方式(使用Eloquent):

$user = new User();
$user->username = Input::get('username');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->save();

但是當用戶使用正確的憑據登錄時,我總是會失敗。 已經為此工作了幾天,不禁感到我缺少一些瑣碎的東西。 請幫忙一下嗎? 謝謝!

如果您在此遇到更多錯誤,則會更容易出現錯誤消息或其他內容。 但是,以下是我登錄用戶的方式。自從最新安裝Laravel 4.1.26以來,新方法給我帶來了錯誤。

它不在下面的部分中,但我認為它的格式更加簡潔。 它可能是新的記住令牌的方法。 請提供更多錯誤信息。 您是否收到Laravel錯誤。 您所擁有的回聲將不會顯示,因為它將無法保持該屏幕。 也許使用return var_dump('Success')會中斷該過程。

$credentials = [
   "username" => Input::get("username"),
   "password" => Input::get("password")
];
if(Auth::attempt($credentials)) {
   return true;
} else {
   return false;
}

從4.1.26開始,在User模型中實現這些方法是一件好事:

public function getRememberToken() {
   return $this->remember_token;
}


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


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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM