laravel 认证模块 login 输入用户名密码后 登陆失败 返回login页面。
PHPz
PHPz 2017-05-16 16:52:37
0
1
588

采用laravel的认证模块来进行用户注册和登陆。
目前注册可用。 mysql里有数据。
但是在登陆(login)的时候失败。返回登录页面。
请教各位大侠怎么解决。
目前的登陆不成功,所以获取认证用户信息Auth::user()都是NULL
下面是我的代码:


Route::group(['middleware' => ['web']], function () {
  //认证路由
  Route::get('auth/login','Auth\AuthController@getLogin');
  Route::post('auth/login','Auth\AuthController@postLogin');
  Route::get('auth/logout','Auth\AuthController@getLogout');
  //注册路由
  Route::get('auth/register','Auth\AuthController@getRegister');
  Route::post('auth/register','Auth\AuthController@postRegister');

});
<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

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

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
}

注册页面

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    {{Form::open([
      'url'=>'/auth/register',
      'method'=>'POST'
      ])}}
      {!! csrf_field() !!}

   <p>
       Name
       <input type="text" name="name" value="{{ old('name') }}">
   </p>

   <p>
       Email
       <input type="email" name="email" value="{{ old('email') }}">
   </p>

   <p>
       Password
       <input type="password" name="password">
   </p>

   <p>
       Confirm Password
       <input type="password" name="password_confirmation">
   </p>

   <p>
       <button type="submit">Register</button>
   </p>

    {{ Form::close() }}
  </body>
</html>

登陆页面

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    {{ Form::open([
      'url'=>'/auth/login',
      'method'=>'POST'
      ]) }}
      {{ csrf_field() }}
      <p>
        Email:
        <input type="email" name="name" value="{{ old('email') }}">
      </p>
      <p>
        Password:
        <input type="password" name="password" id="password"/>
      </p>
      <p>
        <input type="checkbox" name="remember">Remember Me
      </p>
      <p>
        <button type="submit">Login</button>
      </p>
    {{Form::close()}}
  </body>
</html>
PHPz
PHPz

学习是最好的投资!

全部回复(1)
小葫芦

链接描述

你看看我这篇文章:

public function logout()  //其实是他
    {
        Auth::guard($this->getGuard())->logout();

        return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/auth/login');  
        //这里需要注意的是,这里logout的重定向地址需要设置一下,默认是/但是如果/是要登录才可以看的话,那么就会出错。
    }
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!