Laravel 5 Auth :: attempt()总是失败

时间:2015-10-26 20:10:59

标签: php laravel-5

我的Auth :: attempt()每次都返回失败,我正在使用自己的订阅者表,我已经更改了config / auth.php中的设置以使用订阅者表。

下面是我的logincontroller

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Subscriber;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Input;

class LoginController extends Controller
{
    public function login()
    {
        $email = Input::get('email');
        $password = Input::get('password');

//        $subscriber = Subscriber::where('email', $email)->first();
//        $checked = Hash::check($password, $subscriber->pass);


        if (Auth::attempt(['email' => $email, 'pass' => $password]))
        {
            return 'it workedddd.';
        }
        else
            return 'failed';

    }
}

订户模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Subscriber extends Model
{
    protected $fillable = [

        'firstname',
        'lastname',
        'email',
        'pass',
        'phone',
        'address',
        'city',
        'state',
        'zip'
    ];

    protected $hidden = ['password', 'remember_token'];
}

订户控制器

<?php

namespace App\Http\Controllers;

use App\Subscriber;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Requests;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Input;

class SubscribersController extends Controller
{

    public function newSubscriber(Request $request)  //function to register new subscriber
    {

        $validator = Validator::make($request->all(), [
            'firstname' => 'required',
            'lastname' => 'required',
            'email' => 'required|email|unique:subscribers,email',
            'password' => 'required|min:5',
            'address' => 'required',
            'city' => 'required',
            'state' => 'required',
            'zip' => 'required',
            'phone' => 'required|unique:subscribers,phone'
        ]);

        if ($validator->fails()) {
            return redirect('/register')
                ->withErrors($validator)
                ->withInput();
        }


        $subscriber = new Subscriber();
        $subscriber->firstname = Input::get('firstname');
        $subscriber->lastname = Input::get('lastname');
        $subscriber->email = Input::get('email');
        $subscriber->pass =  Hash::make(Input::get('password'));
        $subscriber->address = Input::get('address');
        $subscriber->city = Input::get('city');
        $subscriber->state = Input::get('state');
        $subscriber->zip = Input::get('zip');
        $subscriber->phone = Input::get('phone');
        $subscriber->trial_time = 30;
        $subscriber->date_signed_up = Carbon::now();
        $subscriber->next_bill_date =Carbon::now()->addMonth(1);
        $subscriber->account_status = 'trial';
        $subscriber->save();
        return $subscriber;
    }
}

路由

Route::get('/', function () {
    return view('home');
});

Route::post('/', 'LoginController@login');

Route::get('contact', function() {
    return view('contact');
});

Route::get('register', function() {
    return view('register');
});

Route::post('register', 'SubscribersController@newSubscriber');

Route::get('feedback', function() {
   return view('feedback');
});

Route::get('dashboard', array(
    'before' => 'auth.basic',
    function(){
   return view('dashboard.user');
}));

2 个答案:

答案 0 :(得分:0)

使用Auth::attemp()时你无法做到:

Auth::attempt(['email' => $email, 'pass' => $password])

应该是:

Auth::attempt(['email' => $email, 'password' => $password])

因此,在您的情况下,您应该将以下内容添加到User模型中:

public function getAuthPassword()
{
     return $this->attributes['pass'];
}

答案 1 :(得分:0)

在newSubscriber方法更改

"CitingArticle","Cited","Subject"
"Locke et al., 2015","Laosinchai, P.","Agricultural and Biological  Sciences"
"Locke et al., 2015","Laosinchai, P.","Biochemistry, Genetics and Molecular Biology"
"Yasri, 2015","Yasri, P.","Social Sciences"
 "Yang et al., 2015","Panjaburee, P.","Computer Science"
 "Yang et al., 2015","Panjaburee, P.","Social Sciences"
 "Chu et al., 2015","Panjaburee, P.","Computer Science"
 "Chu et al., 2015","Panjaburee, P.","Social Sciences"
 "Yang et al., 2015","Panjaburee, P.","Computer Science"
 "Yang et al., 2015","Panjaburee, P.","Social Sciences"
  "Chu et al., 2015","Panjaburee, P.","Computer Science"
 "Chu et al., 2015","Panjaburee, P.","Social Sciences"
 "Buaraphan & Abedin Forhad, 2015","Buaraphan, K.","Social Sciences"
 "Kim & Choi, 2015","Yodyingyong, S.","Chemical Engineering"
  "Kim & Choi, 2015","Yodyingyong, S.","Chemistry"
  "Kim & Choi, 2015","Yodyingyong, S.","Engineering"
  "Kim & Choi, 2015","Yodyingyong, S.","Materials Science"
  "Kim & Choi, 2015","Yodyingyong, S.","Physics and Astronomy"
  "Bretz & McClary, 2015","Ratanaroutai, T.","Chemistry"
  "Bretz & McClary, 2015","Ratanaroutai, T.","Social Sciences"
   "Panijpan et al., 2015","Sriwattanarothai, N.","Biochemistry, Genetics and Molecular Biology"
  "Panijpan et al., 2015","Sriwattanarothai, N.","Medicine"
  "Durand & Borsa, 2015","Sriwattanarothai, N.","Agricultural and Biological Sciences"
  "Durand & Borsa, 2015","Sriwattanarothai, N.","Biochemistry, Genetics and Molecular Biology"
  "Durand & Borsa, 2015","Sriwattanarothai, N.","Immunology and Microbiology"
 "Durand & Borsa, 2015","Sriwattanarothai, N.","Medicine"
 "Vu et al., 2015","Yodyingyong, S.","Chemical Engineering"
 "Vu et al., 2015","Yodyingyong, S.","Environmental Science"
 "Chang et al., 2015","Yodyingyong, S.","Materials Science"
 "Zhang et al., 2015","Ketpichainarong, W.","Social Sciences"
 "Buaraphan & Abedin Forhad, 2015","Buaraphan, K.","Social Sciences"
 "Cil, 2015","Buaraphan, K.","Social Sciences"
 "Buaraphan & Abedin Forhad, 2015","Buaraphan, K.","Social Sciences"
 "Di Russo et al., 2015","Chenprakhon, P.","Biochemistry, Genetics and Molecular Biology"
 "De Sancho et al., 2015","Chenprakhon, P.","Chemistry"
 "De Sancho et al., 2015","Chenprakhon, P.","Computer Science"
 "Cazade et al., 2015","Chenprakhon, P.","Chemistry"
 "Cazade et al., 2015","Chenprakhon, P.","Medicine"
 "Cazade et al., 2015","Chenprakhon, P.","Physics and Astronomy"

$subscriber->pass =  Hash::make(Input::get('password'));

您的订阅者模型应如下所示..

$subscriber->pass =  bcrypt(Input::get('password'));

希望这会奏效。