2017-09-25 103 views
-1

我全面檢查了有關如何檢查登錄(如果用戶被批准或不是,然後重定向到登錄或發生錯誤)的信息。現在我很困惑,因爲在互聯網上有很多帖子,每一個都是不同的。那麼任何人都可以幫助我解決這個問題嗎?此外,它會是非常好的解釋它是如何工作(sintaxes和所有其他的東西)如果用戶未經批准,請防止登錄laravel 5.4

user.php的:

namespace App; 

    use Illuminate\Notifications\Notifiable; 
    use Illuminate\Foundation\Auth\User as Authenticatable; 

    class User extends Authenticatable 
    { 
     use Notifiable; 

    /** 
    * The attributes that are mass assignable. 
    * 
    * @var array 
    */ 
    protected $fillable = [ 
    'companyname', 'email', 'password', 'VAT', 'companyphone', 
    'companystreet', 'companycity', 'companycountry', 'companypostcode' 
    ]; 

    /** 
    * The attributes that should be hidden for arrays. 
    * 
* @var array 
*/ 
protected $hidden = [ 
    'password', 'remember_token', 
]; 

}

的LoginController:

namespace App\Http\Controllers\Auth; 

    use App\Http\Controllers\Controller; 
    use Illuminate\Foundation\Auth\AuthenticatesUsers; 

    class LoginController extends Controller 
    { 
/* 
|-------------------------------------------------------------------------- 
| Login Controller 
|-------------------------------------------------------------------------- 
| 
| This controller handles authenticating users for the application and 
| redirecting them to your home screen. The controller uses a trait 
| to conveniently provide its functionality to your applications. 
| 
*/ 

use AuthenticatesUsers; 

/** 
* Where to redirect users after login. 
* 
* @var string 
*/ 
protected $redirectTo = '/'; 

/** 
* Create a new controller instance. 
* 
* @return void 
*/ 
public function __construct() 
{ 
    $this->middleware('guest')->except('logout'); 
} 


    } 

login.blade .php:

@extends('layouts.app') 

    @section('content') 
     <div class="container"> 
<div class="row"> 
    <div class="col-md-8 col-md-offset-2"> 
     <div class="panel panel-default"> 
      <div class="panel-heading">Login</div> 

      <div class="panel-body"> 
       @if($status = Session::get('status')) 
        <div class ="alert alert-info"> 
         {{$status}} 
        </div> 
       @endif 
       <form class="form-horizontal" method="POST" action="{{ route('login') }}"> 
        {{ csrf_field() }} 

        <div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}"> 
         <label for="email" class="col-md-4 control-label">E-Mail Address</label> 

         <div class="col-md-6"> 
          <input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus> 

          @if ($errors->has('email')) 
           <span class="help-block"> 
            <strong>{{ $errors->first('email') }}</strong> 
           </span> 
          @endif 
         </div> 
        </div> 

        <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}"> 
         <label for="password" class="col-md-4 control-label">Password</label> 

         <div class="col-md-6"> 
          <input id="password" type="password" class="form-control" name="password" required> 

          @if ($errors->has('password')) 
           <span class="help-block"> 
            <strong>{{ $errors->first('password') }}</strong> 
           </span> 
          @endif 
         </div> 
        </div> 

        <div class="form-group"> 
         <div class="col-md-6 col-md-offset-4"> 
          <div class="checkbox"> 
           <label> 
            <input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me 
           </label> 
          </div> 
         </div> 
        </div> 

        <div class="form-group"> 
         <div class="col-md-8 col-md-offset-4"> 
          <button type="submit" class="btn btn-primary"> 
           Login 
          </button> 

          <a class="btn btn-link" href="{{ route('password.request') }}"> 
           Forgot Your Password? 
          </a> 
         </div> 
        </div> 
       </form> 
      </div> 
     </div> 
    </div> 
</div> 

@endsection

而且在我的數據庫是布爾字段 「激活」 默認爲0

回答

0

隨着laravel它很簡單。你必須創建一個新的中間件或者擴展app/Http/Middleware/RedirectIfAuthenticated.php中間件。 一個很好的文件建立,你可以在這裏找到:https://laravel.com/docs/5.5/middleware

例如:

public function handle($request, Closure $next, $guard = null) 
{ 
    if (Auth::user()->activated) { 
     return redirect('/home'); 
    } 

    return $next($request); 
} 
0

@Karlis Pokkers

中間件是一種選擇,但我想去通過Laravel文檔提供小巧的黑客工具。

您可以覆蓋Laravel的attemptLogin方法。

將此代碼添加到您的app > Http > Controllers > Auth > LoginController

/** 
* Attempt to log the user into the application. 
* 
* @param \Illuminate\Http\Request $request 
* @return bool 
*/ 

protected function attemptLogin(Request $request) 
{ 
    return Auth::attempt(['username' => $request->username, 'password' => $request->password, 'activated' => 1 ]); 
} 

無需編寫你自己的LoginController。使用Laravel的默認身份驗證控制器。

你可以查看不同的網站。 Answer on Laracast

相關問題