Laravel- Auth登录验证

-- 文章仅供个人学习参考,如有不恰当的地方,希望前辈们指教。--

1、修改config\auth.php配置文件

'model' => App\Model\Users::class,
'table' => 'users',

2、 修改Users模型类如下:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class Users extends Model implements AuthenticatableContract,  AuthorizableContract,  CanResetPasswordContract
{
  //使用auth验证必须继承接口
  use Authenticatable, Authorizable, CanResetPassword;
  //修改为自己的用户表
  protected $table = 'users';
 
  protected $fillable = ['name', 'email', 'password'];

  protected $hidden = ['password', 'remember_token'];
}

3、修改AuthController控制器

class AuthController extends Controller
{
  use ThrottlesLogins /* , AuthenticatesAndRegistersUsers*/ ; //因为没有注册功能可以将AuthenticatesAndRegistersUsers注释掉
  protected $redirectPath = '/admin';
  protected $redirectAfterLogout = '/auth/login';  //注销后的跳转地址

  public function __construct()
  {
    $this->middleware('guest', ['except' => ['getlogout']]); //在中间件中排除 getlogout函数 ,否则无法注销
  }
  function login() {
    return view('admin.users.login');
  }
  function getlogout() {
  if(Auth::check()){
      Auth::logout();  //退出登录
  }
      return redirect('/auth/login');
  }
  function auth(Request $request) {
    $user=$request->only(['name','password']);
    if(Auth::attempt($user,$request->remember)){ //验证密码 通过后合一用Auth::user() 获取登录用户信息 也可以使用 Auth::loginUsingId($user->id) 来登录
        return redirect()->intended('/admin');
    }else{
      return redirect('/auth/login')->withErrors(['login failed'])->withInput($request->except('password'));
    }
  }
  public function redirectPath()  //重构未登录时的跳转地址
  {
    return '/admin';
  }
}

4、修改文件不同文件 redirectPath

guest中间件:app/Http/Middleware/RedirectIfAuthenticated.php

public function handle($request, Closure $next)
  {
  if ($this->auth->check()) {
                return redirect('/admin');  //修改登录页面默认登陆后的跳转地址
  }
  return $next($request);
  }

auth中间件:app/Http/Middleware/Authenticate.php

public function handle($request, Closure $next)
    {
        if ($this->auth->guest()) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('auth/login');
            }
        }
        return $next($request);
    }

5、添加路由

Route::group(['namespace' => 'Auth','prefix'=>'auth'],function(){ 
    route::get('/login','AuthController@login'); 
    route::get('/logout','AuthController@getlogout'); 
    route::post('/login','AuthController@auth'); 
});
route::get('/admin',['namespace' => 'Admin','middleware' => 'auth'],'AdminController@index');


参考教程:http://laravelacademy.org/tutorials/blog

GitHub:https://github.com/CiaraLi/blogtest


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值