dongwoqin7034 2014-03-24 16:03
浏览 58
已采纳

错误:使用Sentry和Laravel需要登录属性

Since today, my login controller (using Sentry) doesn't work anymore. I already changed it 10203902 times but it keeps giving me the error: "a [login] attribute is required". For the past 2-3 weeks this controller worked fine.

LoginController

 <?php namespace Digitus\Auth\Controllers;

use Illuminate\View\Environment as View;
use Cartalyst\Sentry\Sentry;
use Illuminate\Support\Facades\Input;
use Illuminate\Routing\Redirector as Redirect;

class LoginController extends \Digitus\Base\Controllers\BController{

    public function __construct(Input $input, View $view, Sentry $sentry, Redirect $redirect){
        $this->input    = $input;
        $this->view     = $view;
        $this->sentry   = $sentry;
        $this->user     = $this->sentry->getUser();
        $this->redirect = $redirect;

        if($this->sentry->check()) {
            $user = $this->sentry->getUser();
        } else {
            $user = null;
        }
        $this->view->share('loggedUser', $user);
    }

    protected $input;
    protected $view;
    protected $sentry;
    protected $redirect;

    public function index()
    {
        return $this->view->make('user.login');
    }

    public function login()
    {
        $user = $this->user->where('username', $this->input->get('email_or_username'))->orWhere('email', $this->input->get('email_or_username'))->first();
        $credentials = array(
            'email' => Input::get('email_or_username') ?: null,
            'password' => $this->input->get('password'),
        );
        try {
            $user = $this->sentry->authenticate($credentials, false);

            if($this->sentry->check())
            {
                return $this->redirect->to('admin');
            } else {
                return $this->redirect->to('');
            }
        }
        catch (\Exception $e)
        {
            return $this->redirect->to('login')->withErrors(array('login'=> $e->getMessage()));
        }
    }

}

View

@extends('layouts.main')

@section('content')

<div class="col-md-4 col-md-offset-4">
    <div class="panel panel-info">
        <div class="panel-heading">PLease Login</div>
        <div class="panel-body">
            {{ Form::open(array('url' => 'login')) }}
            @if($errors->has('login'))
                <div class="alert alert-danger">
                    <a href="#" class="close" data-dismiss="alert">&times;</a>
                    {{ $errors->first('login', ':message') }}
                </div>
            @endif
            <div class="form-group">
                {{ Form::label('email_or_username', 'Email Address or Username') }}
                {{ Form::email('email_or_username', '', array('class'=>'form-control','placeholder'=>'Please enter your Email Address or Username to login', 'autofocus')) }}
            </div>
            <div class="form-group">
                {{ Form::label('password', 'Password') }}
                {{ Form::password('password', array('class'=>'form-control', 'placeholder'=>'Password')) }}
            </div>
            <div class="form-group">
                {{ Form::submit('Login', array('class'=>'btn btn-success')) }}
                {{ HTML::link('/', 'Cancel', array('class'=>'btn btn-danger')) }}
            </div>
            <hr>
            <div class="form-group">
                <h2>Don't have an account?</h2>
                {{ HTML::link('register', 'Please register here', array('class'=>'btn btn-primary btn-xs'))}}
            </div>
            {{ Form::close() }}
        </div>
    </div>
</div>

@stop

I've already asked many people for solutions or fixes etc. They all didn't work..

PS & FYI: I also published the Sentry config, and the login_attribute is email. Any help or input would be great. Thanks in advance..

  • 写回答

1条回答 默认 最新

  • duanfan8360 2014-03-24 16:21
    关注

    Sentry also uses the

    protected static $loginAttribute = 'email';
    

    To get the login data, check if you have made changes to it. Those are the lines that are giving you a hard time:

    $loginName = $this->userProvider->getEmptyUser()->getLoginName();
    
    $loginCredentialKey = (isset($credentials[$loginName])) ? $loginName : 'login';
    

    Basically if $loginAttribute is not available it will use 'login' as the credential key. So you can also test to give what it wants:

    $credentials = array(
        'email' => Input::get('email_or_username') ?: null,
        'login' => Input::get('email_or_username') ?: null,
        'password' => $this->input->get('password'),
    );
    

    Unfortunately, you cannot have two different fields to login your user, so if you need this you'll have to do your check manually, so you probably will be able to do something like:

    public function login()
    {
        $user = $this->sentry->getUserProvider()->getEmptyUser();
    
        $user = $user->where('email', $this->input->get('email_or_username'))
                            ->orWhere('username', $this->input->get('email_or_username'))
                            ->first();
    
        try 
        {
            if ($user && Hash::check($this->input->get('password'), $user->password)) /// here you check if a user was found
            {
                $this->sentry->login($user, false); /// remember = false /// here is the login happening in case of true
            }
            else
            {
                // user not found or wrong password
            }
        }
        catch (\Exception $e)
        {
            return $this->redirect->to('login')->withErrors(array('login'=> $e->getMessage()));
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况