2016-05-05 17 views
0

我是laravel的新手。我看到一些相关的帖子,但我找不到正确的答案。我做了auth使用php artisan make:auth,我有一个目录publicPages。我已将auth.login视图更改为我的自定义publicPages.loginpublicPages.register。注册过程正常,将数据插入users表。但它不会登录用户。当我去login查看和。它既不会给出任何错误,也不会记录用户的身份,只是将其重定向回来。注册过程正常运行,但登录视图不工作laravel认证

这里是路线:

Route::auth(); 
Route::controllers([ 
    'auth' => 'Auth\AuthController', 
    'password' => 'Auth\PasswordController' 
]); 

Route::group(array('namespace' => 'UserControllers'), function() 
{ 
    Route::group(['middleware' => ['web']], function() { 
     Route::group(['middleware' => 'auth'], function() { 
     Route::get('community', '[email protected]'); 
     Route::post('communities', '[email protected]'); 
     Route::get('edit/{id}', ['as' => 'edit', 'uses' => '[email protected]']); 
     Route::get('delete/{id}', '[email protected]'); 
     Route::post('update/{id}', ['as' => 'update', 'uses' => '[email protected]']); 
     Route::get('create', '[email protected]'); 
     Route::post('idea', '[email protected]'); 
     Route::get('users', '[email protected]'); 
     Route::get('deleteUser/{id}', '[email protected]'); 
     Route::get('delete/idea/{id}', '[email protected]'); 
     Route::get('approve/{id}', '[email protected]'); 
    }); 
    }); 

控制器:

class UserController extends Controller { 

    //constructor 
    public function __construct() { 
     $this->middleware('auth'); 
    } 

我知道什么时候我会用$this->middleware('auth');它会重定向到登录页面,每UserController function。这工作正常。

查看:

<form id="login-form" class="clearfix" action="{{url('/login')}}" method="post"> 
<input type="hidden" name="_token" value="{{ csrf_token() }}"> 
<p class="rs pb30">Please Provide your Credentials to Verify Your Identity</p> 
<div class="form form-post-comment"> 
<div class="fa-align-center"> 
<label for="login_email"> 
<input id="login_email" type="email" name="email" class="txt fill-width txt-name{{ $errors->has('email') ? ' has-error' : '' }}" placeholder="Enter Your Email" required="required"/> 
</label> 
@if ($errors->has('email')) 
    <span class="help-block"> 
    <strong>{{ $errors->first('email') }}</strong> 
    </span> 
@endif 
<br><br> 
<label for="password"> 
<input id="password" type="password" name="login_password" class="txt fill-width txt-email" placeholder="Enter Your Password" required="required"/> 
</label> <br><br> 
<label for="links"> 
<a href="/changepassword" class="txt-emphasis">Forgotten Password?</a> 
<p>Don't have an account? <a href="/register" class="txt-emphasis">Register Here</a> </p> 
</label> 
</div> 

<div class="clear"></div> 
<p > 
<span id="response"></span> 
{{--<input type="submit" class="btn btn-submit-comment" value="Login">--}} 
<button class="btn btn-submit-comment" form="login-form">Login</button> 
</p> 
</div> 

这里是AuthenticatesUsers文件:

<?php 

namespace Illuminate\Foundation\Auth; 

use Illuminate\Http\Request; 
use Illuminate\Support\Facades\Auth; 
use Illuminate\Support\Facades\Lang; 
//use App\Http\Requests\UserRequest; 

trait AuthenticatesUsers 
{ 
    use RedirectsUsers; 

    /** 
    * Show the application login form. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function getLogin() 
    { 
     return $this->showLoginForm(); 
    } 

    /** 
    * Show the application login form. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function showLoginForm() 
    { 
     $view = property_exists($this, 'loginView') 
        ? $this->loginView : 'auth.authenticate'; 

     if (view()->exists($view)) { 
      return view($view); 
     } 

     return view('publicPages.login'); 
    } 

    /** 
    * Handle a login request to the application. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @return \Illuminate\Http\Response 
    */ 
    public function postLogin(Request $request) 
    { 
     return $this->login($request); 
    } 

    /** 
    * Handle a login request to the application. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @return \Illuminate\Http\Response 
    */ 
    public function login(Request $request) 
    { 
     $this->validateLogin($request); 

     // If the class is using the ThrottlesLogins trait, we can automatically throttle 
     // the login attempts for this application. We'll key this by the username and 
     // the IP address of the client making these requests into this application. 
     $throttles = $this->isUsingThrottlesLoginsTrait(); 

     if ($throttles && $lockedOut = $this->hasTooManyLoginAttempts($request)) { 
      $this->fireLockoutEvent($request); 

      return $this->sendLockoutResponse($request); 
     } 

     $credentials = $this->getCredentials($request); 

     if (Auth::guard($this->getGuard())->attempt($credentials, $request->has('remember'))) { 
      return $this->handleUserWasAuthenticated($request, $throttles); 
     } 

     // If the login attempt was unsuccessful we will increment the number of attempts 
     // to login and redirect the user back to the login form. Of course, when this 
     // user surpasses their maximum number of attempts they will get locked out. 
     if ($throttles && ! $lockedOut) { 
      $this->incrementLoginAttempts($request); 
     } 

     return $this->sendFailedLoginResponse($request); 
    } 

    /** 
    * Validate the user login request. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @return void 
    */ 
    protected function validateLogin(Request $request) 
    { 
     $this->validate($request, [ 
      $this->loginUsername() => 'required', 'password' => 'required', 
     ]); 
    } 

    /** 
    * Send the response after the user was authenticated. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param bool $throttles 
    * @return \Illuminate\Http\Response 
    */ 
    protected function handleUserWasAuthenticated(Request $request, $throttles) 
    { 
     if ($throttles) { 
      $this->clearLoginAttempts($request); 
     } 

     if (method_exists($this, 'authenticated')) { 
      return $this->authenticated($request, Auth::guard($this->getGuard())->user()); 
     } 

     return redirect()->intended($this->redirectPath()); 
    } 

    /** 
    * Get the failed login response instance. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @return \Illuminate\Http\Response 
    */ 
    protected function sendFailedLoginResponse(Request $request) 
    { 
     return redirect()->back() 
      ->withInput($request->only($this->loginUsername(), 'remember')) 
      ->withErrors([ 
       $this->loginUsername() => $this->getFailedLoginMessage(), 
      ]); 
    } 

    /** 
    * Get the failed login message. 
    * 
    * @return string 
    */ 
    protected function getFailedLoginMessage() 
    { 
     return Lang::has('auth.failed') 
       ? Lang::get('auth.failed') 
       : 'These credentials do not match our records.'; 
    } 

    /** 
    * Get the needed authorization credentials from the request. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @return array 
    */ 
    protected function getCredentials(Request $request) 
    { 
     return $request->only($this->loginUsername(), 'password'); 
    } 

    /** 
    * Log the user out of the application. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function getLogout() 
    { 
     return $this->logout(); 
    } 

    /** 
    * Log the user out of the application. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function logout() 
    { 
     Auth::guard($this->getGuard())->logout(); 

     return redirect(property_exists($this, 'redirectAfterLogout') ? $this->redirectAfterLogout : '/'); 
    } 

    /** 
    * Get the guest middleware for the application. 
    */ 
    public function guestMiddleware() 
    { 
     $guard = $this->getGuard(); 

     return $guard ? 'guest:'.$guard : 'guest'; 
    } 

    /** 
    * Get the login username to be used by the controller. 
    * 
    * @return string 
    */ 
    public function loginUsername() 
    { 
     return property_exists($this, 'username') ? $this->username : 'email'; 
    } 

    /** 
    * Determine if the class is using the ThrottlesLogins trait. 
    * 
    * @return bool 
    */ 
    protected function isUsingThrottlesLoginsTrait() 
    { 
     return in_array(
      ThrottlesLogins::class, class_uses_recursive(static::class) 
     ); 
    } 

    /** 
    * Get the guard to be used during authentication. 
    * 
    * @return string|null 
    */ 
    protected function getGuard() 
    { 
     return property_exists($this, 'guard') ? $this->guard : null; 
    } 
} 

请问有什么需要postLoginForm()改变?由于

更新2: 我已经改变postLoginForm()函数的代码

if (Auth::attempt(['email' => $email, 'password' => $password])) { 
      // Authentication passed... 
      return redirect()->intended('dashboard'); 
     } else { 
echo 'not logged in'; 
} 

现在终于它打印not logged in但即使我使用true凭据它说not logged in

回答

0

Try to remove web middleware from routes.php,因为它会自动应用自5.2.27开始,如果您手动应用它,可能会导致类似于您的错误。

+0

感谢您的回复,我也试过了。起初我没有'route.php'。 @Alexey Mezenin –

+0

你是否更新过'showLoginForm'函数中的视图路径? – Vikas

+0

这是showLoginForm'公共函数showLoginForm() { $ view = property_exists($ this,'loginView') ?$ this-> loginView:'auth.authenticate'; if(view() - > exists($ view)){ return view($ view); } return view('auth.login'); }'我必须将'return view('auth.login')'改为'return view('publicPages.login)''? –

0

根据您的路线文件,您的登录路线是/auth/login而不是/login。因此,我建议你为改变行动在您的视图文件:

action="{{url('/auth/login')}}" 

编辑

由于Route::auth();在你的路由文件的顶部已经添加, 你不需要在你的路由以下文件:

Route::controllers([ 
    'auth' => 'Auth\AuthController', 
    'password' => 'Auth\PasswordController' 
]); 

请删除并恢复登录表单中的网址。

+0

我也改变了它。但没有任何工作。登录视图既不会在错误的输入上给出任何验证错误,也不会登录用户。 –

+0

@QasimAli - 我已经更新了我的回答 – Vikas

+0

当我删除了建议的部分代码。它抛出一个'NotFoundHttpException' @Vikas –