Laravel basic-auth

时间:2013-07-03 12:11:42

标签: laravel laravel-4

我想在我的网页上使用basic.auth,但身份验证无效

routes.php文件

admin - 身份验证

Route::get('admin', array('before' => 'auth.basic', function()
{
    return 'Top secret';
}));

create - 创建测试用户

Route::get('create', function()
{
    $user = new User;
    $user->email = 'test@test.com';
    $user->username = 'test';
    $user->password = Hash::make('password');
    $user->save();
});

配置

  • app/config/app - 定义了key(创建了Laravel安装)
  • app/config/auth - 已定义modelUser)和tableusers

filters.php

auth.basic

Route::filter('auth.basic', function()
{
    return Auth::basic();
});

测试

我致电/create创建用户test@test.compassword

以下是users表: enter image description here

然后我致电/admin登录

enter image description here

但它不会让我进去。在登录之后 - 它只是清除输入。在取消之后 - 它返回Invalid credentials.


用户模型

我尝试了实施UserInterface

<?php
use Illuminate\Auth\UserInterface;

class User extends Eloquent implements UserInterface {

    protected $table = 'users';

    /**
     * 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->passsword;
    }
}

问题解决了

我在User模型return $this->passsword;中输了错误有3 s

现在我使用默认的Laravel User model

1 个答案:

答案 0 :(得分:4)

确保在app / config / auth.php中将driver设置为eloquent

您可能还需要实施UserInterface界面(class User extends Eloquent implements UserInterface) - 然后您需要在模型中包含这些方法:

/**
 * 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;
}