如何为Laravel 5.1 Auth

时间:2015-09-18 02:31:14

标签: php laravel

我的页面中有一个现有的身份验证,n =现在我想要的是为我的管理页面实现另一个身份验证。

我使用这个包但是我收到了一个错误:

https://github.com/Kbwebs/MultiAuth

这就是我的所作所为。

  1. 我遵循包教程中给出的所有步骤,但密码重置除外。
  2. 我为Admin创建了一个模型和一个迁移。我的管理员中的列与用户表相同,但没有姓氏,名字和生日
  3. 现在,当我尝试使用此网址登录时: http://localhost:8000/auth/login 它说我提供的身份验证详细信息在记录中不匹配。我认为不是使用users表进行登录,而是使用admin表进行登录,我没有任何记录。
  4. 当我尝试使用此网址注册时:http://localhost:8000/auth/register。我在保存时没有任何错误,但在登录后它会重定向到注册URL。 以下是我在此页面中的错误:
  5.   

    传递给Illuminate \ Auth \ Guard :: login()的参数1必须实现   interface Illuminate \ Contracts \ Auth \ Authenticatable,none given

    您是否可以逐步指导我使用我找到的包裹?或者你可以帮我找出我错过的部分。

    好的,这是我的一些代码:

    用户模型

    class User extends Model implements AuthenticatableContract,
                                        AuthorizableContract,
                                        CanResetPasswordContract
    {
        use Authenticatable, Authorizable, CanResetPassword;
    
        /**
         * The database table used by the model.
         *
         * @var string
         */
        protected $table = 'users';
    
        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = ['username', 'lastname', 'firstname', 'contact',  'email', 'birthday', 'status', 'password'];
    
        /**
         * The attributes excluded from the model's JSON form.
         *
         * @var array
         */
        protected $hidden = ['password', 'remember_token'];
    }
    

    AuthController

    <?php
    
    namespace App\Http\Controllers\Auth;
    
    use App\User;
    use Validator;
    use App\Http\Controllers\Controller;
    use Illuminate\Foundation\Auth\ThrottlesLogins;
    use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
    
    class AuthController extends Controller
    {
        /*
        |--------------------------------------------------------------------------
        | Registration & Login Controller
        |--------------------------------------------------------------------------
        |
        | This controller handles the registration of new users, as well as the
        | authentication of existing users. By default, this controller uses
        | a simple trait to add these behaviors. Why don't you explore it?
        |
        */
    
        use AuthenticatesAndRegistersUsers, ThrottlesLogins;
    
        protected $username = 'username'; //choose username instead of email
        protected $redirectPath = '/dashboard'; //if successful login
        protected $loginPath = 'auth/login'; //if not login
        protected $redirectAfterLogout = 'auth/login'; //redirect after login
    
        /**
         * Create a new authentication controller instance.
         *
         * @return void
         */
        public function __construct()
        {
            $this->middleware('guest', ['except' => 'getLogout']);
        }
    
        /**
         * Get a validator for an incoming registration request.
         *
         * @param  array  $data
         * @return \Illuminate\Contracts\Validation\Validator
         */
        protected function validator(array $data)
        {
            return Validator::make($data, [
                'username'  => 'required|min:8|max:16|unique:users',
                'lastname'  => 'required',
                'firstname' => 'required',
                'contact'   => 'required',
                'birthday'  => 'date_format: Y-m-d',
                'email'     => 'required|email|max:255|unique:users',
                'password'  => 'required|confirmed|min:6',
            ]);
        }
    
        /**
         * Create a new user instance after a valid registration.
         *
         * @param  array  $data
         * @return User
         */
        protected function create(array $data)
        {
    
            //dd($data);
    
            return User::create([
                'username'  =>  $data['username'],
                'lastname'  =>  $data['lastname'],
                'firstname' =>  $data['firstname'],
                'birthday'  =>  $data['birthday'],
                'contact'   =>  $data['contact'],
                'email'     =>  $data['email'],
                'status'    =>  1,
                'password'  =>  bcrypt($data['password']),
            ]);
    
        }
    }
    

    管理员迁移

    <?php
    
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class CreateAdminsTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('admins', function (Blueprint $table) {
                $table->increments('id');
                $table->string('username');
                $table->string('name');
                $table->string('contact');
                $table->string('password', 60);
                $table->rememberToken();
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::drop('admins');
        }
    }
    

    路线

    <?php
    
    /*
    |--------------------------------------------------------------------------
    | Application Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register all of the routes for an application.
    | It's a breeze. Simply tell Laravel the URIs it should respond to
    | and give it the controller to call when that URI is requested.
    |
    */
    
    Route::get('/', function () {
        return view('welcome');
    });
    
    //login user authenication
    Route::get('auth/login', 'Auth\AuthController@getLogin');
    Route::post('auth/login', 'Auth\AuthController@postLogin');
    Route::get('auth/logout', 'Auth\AuthController@getLogout');
    
    //registration user
    Route::get('auth/register', 'Auth\AuthController@getRegister');
    Route::post('auth/register', 'Auth\AuthController@postRegister');
    
    Route::get('dashboard', array(
        'middleware' => 'auth',
        'uses' => 'DashboardController@index'
    ));
    

    login.blade.php

    @extends('app')
    @section('header_title', 'Flax User Login')
    
    @section('content')
    
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <h3><span class="fa fa-user"></span> Account Login</h3>
            <div id="login-form">
                <div class="well">
                    {!! Form::open(array('routes' => 'auth/login')) !!}
    
                        <div class="form-group {{ ($errors->first('username')) ? 'has-error' : '' }}">
                            {!! Form::label('username', 'Username', array('class' => 'control-label')) !!}
                            {!! Form::text('username', null, array('class' => 'form-control input-md', 'placeholder' => 'Your Username')) !!}
                            @if($errors->has('username'))
                                <p class="error">{{ $errors->first('username') }}</p>
                            @endif
                        </div>
    
                        <div class="form-group {{ ($errors->first('password')) ? 'has-error' : '' }}">
                            {!! Form::label('password', 'Password', array('class' => 'control-label')) !!}
                            {!! Form::password('password', array('class' => 'form-control input-md', 'placeholder' => 'Your Password')) !!}
                            @if($errors->has('password'))
                                <p class="error">{{ $errors->first('password') }}</p>
                            @endif
                        </div>
    
                        <div class="form-group">
                            <label><input type="checkbox" name="" />&nbsp;&nbsp;Remember Me</label>
                            <a href="{{ url('account/revovery') }}" class="pull-right">Forgot Password?</a>
                        </div>
    
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary"><span class="fa fa-key"></span> Login</button>
                            <span>&nbsp;&nbsp;or&nbsp;&nbsp;</span>
                            <a href="{{ url('auth/register') }}">Create New Account</a>
                        </div>
    
                    {!! Form::close() !!}
    
                </div>
            </div>
        </div>
    </div>
    
    
    @endsection
    

    register.blade.php

    @extends('app')
    @section('header_title', 'Flax User Login')
    
    @section('content')
    
    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <h3><span class="fa fa-user"></span> Account Registration</h3>
            <div id="login-form">
                <div class="well">
                    {!! Form::open(array('routes' => 'auth/register')) !!}
    
                        <div class="form-group {{ ($errors->first('username')) ? 'has-error' : '' }}">
                            {!! Form::label('username', 'Username', array('class' => 'control-label')) !!}
                            {!! Form::text('username', null, array('class' => 'form-control input-md', 'placeholder' => 'Your Username')) !!}
                            @if($errors->has('username'))
                                <p class="error">{{ $errors->first('username') }}</p>
                            @endif
                        </div>
    
                        <div class="form-group {{ ($errors->first('lastname')) ? 'has-error' : '' }}">
                            {!! Form::label('lastname', 'Last Name', array('class' => 'control-label')) !!}
                            {!! Form::text('lastname', null, array('class' => 'form-control input-md', 'placeholder' => 'Your Last Name')) !!}
                            @if($errors->has('lastname'))
                                <p class="error">{{ $errors->first('lastname') }}</p>
                            @endif
                        </div>
    
                        <div class="form-group {{ ($errors->first('firstname')) ? 'has-error' : '' }}">
                            {!! Form::label('firstname', 'First Name', array('class' => 'control-label')) !!}
                            {!! Form::text('firstname', null, array('class' => 'form-control input-md', 'placeholder' => 'Your First Name')) !!}
                            @if($errors->has('firstname'))
                                <p class="error">{{ $errors->first('firstname') }}</p>
                            @endif
                        </div>
    
                        <div class="form-group {{ ($errors->first('email')) ? 'has-error' : '' }}">
                            {!! Form::label('email', 'Email Address', array('class' => 'control-label')) !!}
                            {!! Form::text('email', null, array('class' => 'form-control input-md', 'placeholder' => 'Your Email Address')) !!}
                            @if($errors->has('email'))
                                <p class="error">{{ $errors->first('email') }}</p>
                            @endif
                        </div>
    
                        <div class="form-group {{ ($errors->first('email')) ? 'has-error' : '' }}">
                            {!! Form::label('contact', 'Contact Number', array('class' => 'control-label')) !!}
                            {!! Form::text('contact', null, array('class' => 'form-control input-md', 'placeholder' => 'Your Contact Number')) !!}
                            @if($errors->has('contact'))
                                <p class="error">{{ $errors->first('contact') }}</p>
                            @endif
                        </div>
    
    
                        <div class="form-group {{ ($errors->first('birthday')) ? 'has-error' : '' }}">
                            {!! Form::label('birthday', 'Birthday', array('class' => 'control-label')) !!}
                            {!! Form::text('birthday', null, array('class' => 'form-control input-md', 'placeholder' => 'Your Birthday')) !!}
                            @if($errors->has('birthday'))
                                <p class="error">{{ $errors->first('birthday') }}</p>
                            @endif
                        </div>
    
                        <div class="form-group {{ ($errors->first('password')) ? 'has-error' : '' }}">
                            {!! Form::label('password', 'Password', array('class' => 'control-label')) !!}
                            {!! Form::password('password', array('class' => 'form-control input-md', 'placeholder' => 'Your Password')) !!}
                            @if($errors->has('password'))
                                <p class="error">{{ $errors->first('password') }}</p>
                            @endif
                        </div>
    
                        <div class="form-group">
                            {!! Form::label('password_confirmation', 'Password Confirmation', array('class' => 'control-label')) !!}
                            {!! Form::password('password_confirmation', array('class' => 'form-control input-md', 'placeholder' => 'Retype Password')) !!}
                        </div>
    
                        <p>Please review the information before saving it.</p>
    
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary"><span class="fa fa-key"></span> Create Account</button>&nbsp;&nbsp;
                            <a href="{{ url('auth/login') }}" class="btn btn-default"><span class="fa fa-arrow-left"></span> Back to Login</a>
                        </div>
    
                    {!! Form::close() !!}
                </div>
            </div>
        </div>
    </div>
    
    
    @endsection
    

0 个答案:

没有答案