简体   繁体   English

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

[英]An error in multiple auth for two tables using laravel 5.3

我以管理员身份登录时出错

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: 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: 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 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: 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>

guards.admin is looking for a provider called branch You need to have a branch in your providers array. guards.admin正在寻找一个名为branchprovider你需要在provider数组中有一个branch

So your auth.php should look like: 所以你的auth.php应该是这样的:

'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 ; 您的Branch模型还必须实现Illuminate \\ Contracts \\ Auth \\ Authenticatable如果您的Branch表具有相同的db colums,如idpasswordremember_token ; the easiest way would be as same as the User model. 最简单的方法与User模型相同。

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. 请注意,您需要使用user()来获取分支对象。 not branch() . 不是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. 然后在您的视图中的任何位置等。您将能够使用auth() - > user()来获取您的中间件中设置的分支或用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM