Laravel basic-auth

你说的曾经没有我的故事 提交于 2019-12-23 22:51:32

问题


I want to use basic.auth for my web page but authentication donst work

routes.php

admin - authentication

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

create - create test user

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

config

  • app/config/app - has defined key (that created Laravel installation)
  • app/config/auth - has defined model (User) and table (users)

filters.php

auth.basic

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

test

I call /create to create User test@test.com:password

Here is users table after:

Then I call /admin to login

But it doesnt let me in. After Login - it just clear inputs. After Cancel - it return Invalid credentials..


User model

I tried implement 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;
    }
}

Problem solved

I had typo in User model return $this->passsword; There is 3 s.

Now I use default Laravel User model.


回答1:


Ensure that in app/config/auth.php - driver is set to eloquent.

You may also need to implement the UserInterface interface (class User extends Eloquent implements UserInterface) - then you'll need to include the methods in your model:

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


来源:https://stackoverflow.com/questions/17447617/laravel-basic-auth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!