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

 
  
 
 

你可能感兴趣的:(#,Laravel框架)