dtwxmn8741 2017-04-24 11:05
浏览 158

使用laravel 5.3的两个表的多个auth中的错误

The error when i login as Admin

I have two tables one is Normal user And other is for Branch. when i am login as Normal user the its login without any error. Secondly I want to Login Admin with the branch Email and password and it save in branch table but when i login it show an error.

BranchController:

public function ggetlogin()
{
    return view('branch_admin.badmin');
}

public function authenticateBranchAdmin(Request $request){
    $validator = Validator::make($request->all(), [
        'email' => 'required|email',
        'password' => 'required'
    ]);

    if($validator->passes()){
        if(Auth::guard('admin')->attempt([
            'email' =>  $request->email,
            'password'  =>  $request->password,
        ])){
            if(Auth::guard('admin')->branch()->email === $request->email){
                return redirect('/branch/'.Branch::branch()->id);
            }

        }else{
            if($this->AdminIsVerified($request->email)){
                $request->session()->flash('message', 'Invalid email or password');
            }
            return redirect('/Admin/login');
        }
    }else{
        return redirect('/Admin/login')->withErrors($validator)->withInput();
    }
}

AddBranchView:

<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
<form class="form-horizontal branch-form" action="/add/branch" method="post">
        <div class="form-group">
        <label class="col-sm-3 control-label">BranchName</label>
        <div class="col-sm-9">
            <input type="text" class="form-control" name="name" placeholder="Name">
            @if($errors->has('name'))
                {{$errors->first('name')}}
            @endif
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label">B_Email</label>
        <div class="col-sm-9">
            <input type="text" class="form-control" name="email" placeholder="Email">
            @if($errors->has('email'))
                {{$errors->first('email')}}
            @endif
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-3 control-label">Password</label>
        <div class="col-sm-9">
            <input type="text" class="form-control" name="password" placeholder="password">
            @if($errors->has('password'))
                {{$errors->first('password')}}
            @endif
        </div>
    </div>

        @if(Session::has('message'))
            <div class="form-group">
                <label class="col-sm-3 control-label"></label>
                <div class="col-sm-9">
                    <div class="alert alert-info" role="alert">{{Session::get('message')}}</div>
                </div>
            </div>
        @endif

    <div class="form-group">
        <label class="col-sm-3 control-label"></label>
        <div class="col-sm-9">
            <button type="submit" value="add branch" class="btn btn-default">Submit</button>
        </div>
    </div>
    {{csrf_field()}}
</form>
</div>

Auth.php

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],

    'admin'=> [
        'driver' => 'session',
        'provider' => 'branch',
    ],
],

Providers:

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,

    'admin' => [
        'driver' => 'eloquent',
        'model' => App\Branch::class,
    ],
],

LoginView:

<form class="form-horizontal login-form" action="/Admin/login" method="post">

    <div class="form-group" >
        <label class="col-sm-3 control-label">EMAIL</label>
        <div class="col-sm-9">
            <input type="email" class="form-control" name="email" placeholder="Email">
            @if($errors->has('email'))
                {{$errors->first('email')}}
            @endif
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label">PASSWORD</label>
        <div class="col-sm-9">
            <input type="password" class="form-control" name="password" placeholder="Password">
            @if($errors->has('password'))
                {{$errors->first('password')}}
            @endif
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-3 control-label"></label>
        <div class="col-sm-9">
            <button type="submit" value="login" class="btn btn-default">Login</button>
        </div>
    </div>

    {{csrf_field()}}
</form>
  • 写回答

1条回答 默认 最新

  • douciping4283 2017-04-25 20:05
    关注

    guards.admin is looking for a provider called branch You need to have a branch in your providers array.

    So your auth.php should look like:

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
    
        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    
        'admin'=> [
            'driver' => 'session',
            'provider' => 'branch', // This one says there should be a branch in providers
        ],
    ],
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
    
        'branch' => [ // This is where guards.admin is looking
            'driver' => 'eloquent',
            'model' => App\Branch::class,
        ],
    ],
    

    Also your Branch model must implement Illuminate\Contracts\Auth\Authenticatable If your Branch table has the same db colums like, id, password and remember_token; the easiest way would be as same as the User model.

    namespace App;
    //...
    use Illuminate\Foundation\Auth\User as Authenticatable;
    //...
    
    class Branch extends Authenticatable
    {
       //...
    }
    

    Then you can retrieve your branch as:

    Auth::guard('admin')->user()->email
    

    Plese note that you need to use user() to get your branch object. not branch().


    I'd recommend you to utilize middlewares instead of code in your controller methods. Its more manageable and much easier.

    routes/web.php

    Route::get('/some-page-for-normal-users', "AController@index")->middleware('auth:web');
    
    Route::get('/some-page-for-branch-users', "BController@index")->middleware('auth:admin');
    

    Then anywhere in your views etc. You will be able to user auth()->user() to get your Branch or User whichever is set in your middleware.

    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算