我周围检查了有关如果用户是否获得批准而如何检查登录信息,然后重定向到登录或出错的信息。现在我有点困惑,因为在互联网上有很多帖子,每一个都是不同的。那么任何人都可以帮我解决这个问题吗?也很好解释它是如何工作的(sintaxes和所有其他东西)
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
同样在我的DB中,布尔字段“激活”默认为0
答案 0 :(得分:1)
@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
答案 1 :(得分: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);
}