Laravel 5用户无法成功验证后进行检索 [英] Laravel 5 user can not be retrieved after successful authentication

查看:178
本文介绍了Laravel 5用户无法成功验证后进行检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Laravel 5.1,并试图实现用户身份验证。基本上没有路线应该是不用登录访问。
内置的特质( AuthenticatesAndRegistersUsers ),因为,一个成功的认证之后,重定向器将无法通过来拉出用户无法正常工作验证用户::() $这个 - > auth->用户 $请求 - >用户()

I am using Laravel 5.1 and trying to implement user authentication. Basically no routes should be accessible without loggin in. The built in trait (AuthenticatesAndRegistersUsers) does not work correctly since, after a successful authentication, the redirected controller will not be able to pull out the user via Auth::user() or $this->auth->user or $request->user().

我并验证它之后的一次成功尝试通过VAR倾销用户 $这个 - > auth-方式>尝试()

I did verify that it is a successful attempt by var-dumping the user right after $this->auth->attempt().

返回重定向()重定向后,但是 - 方式>意(...),没有用户可拉

我现在试图跳过 AuthenticatesAndRegistersUsers 来实现它,并直接在 AuthController 写作,但再次遇到同样的问题。

I am now trying to implement it by skipping the AuthenticatesAndRegistersUsers and writing it directly in the AuthController but again encountering the same problem.

这是做验证在 AuthController 部分:

$this->validate($request, [
    'email'    => 'required|email',
    'password' => 'required',
]);

$credentials = $request->only('email', 'password');

//attempt to authenticate
//if successful, redirect to the intended url or the default redirectPath
if ($this->auth->attempt($credentials, $request->has('remember')))
{
    //when I do dd($this->auth->user()) here it shows the correct user record
    return redirect()->intended($this->redirectPath);
}

据重定向到正确的URL(的HomeController ),但是用户无法检索(即显示在方法或任何其它方法):

It redirects to the correct url (HomeController) but then the user can not be retrieved (within the show method or any other method):

dd(Auth::user()); //null
dd(Auth::check());//null

的HomeController 扩展我的 BaseController ,它使用 AUTH 中间件

HomeController extends my BaseController which uses the auth middleware

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

我有Laravel 4自定义身份验证,这是一切工作正常,我还是新来的中间件认证概念Laravel 5。

I had Laravel 4 custom authentication and it was all working fine, I'm still new to the middleware authentication concept in Laravel 5.

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

如何Laravel 5.1使用内置的身份验证

How to use the build-in authentication in Laravel 5.1

Laravel 5.1已建立的认证,它的工作方式几乎开箱即用。

Laravel 5.1 has build in authentication and it works almost out of the box.

有几个步骤要做,所以你将能够使用认证模板漂亮的默认构建在Laravel 5.1。所以,你不需要再创造那个轮子像Laravel 4.0。

There are few step to do, so you will be able to use the nice default build in Authentication template in Laravel 5.1. So you do not need to invent that wheel again like Laravel 4.0.

1安装Laravel的全新副本(按照此 http://laravel.com/docs/5.1),我做到了,只需使用此命令
作曲家创建项目laravel / laravel - preFER - 距离

1- Install fresh copy of Laravel (follow this http://laravel.com/docs/5.1) I did it by simply using this command composer create-project laravel/laravel --prefer-dist

2 - 创建意见资源\\意见\\ AUTH AUTH 文件夹和文件复制所有资源从它< A HREF =htt​​ps://github.com/laravel/laravel/tree/5.0/resources/views/auth相对=nofollow> https://github.com/laravel/laravel/tree/5.0/resources/意见/ AUTH

2- Create auth folder in views resources\views\auth and copy all resources files to it from https://github.com/laravel/laravel/tree/5.0/resources/views/auth

3创建 CSS 字体文件夹里面公共文件夹,并将从资源这两个文件夹的内容 https://github.com/laravel/laravel/树/ 5.0 /公共

3- Create css and fonts folder inside public folder and copy both folders content from resources https://github.com/laravel/laravel/tree/5.0/public

4-更新您的路由文件如下面

4- Update your route file like below

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

关于向 https://github.com/laravel/ laravel / BLOB / 5.0 /应用/ HTTP / routes.php文件

5在你的MySQL创建空数据库。

5- Create empty database in your MySQL.

6更新 .ENV 与数据库信息等文件。

6- Update your .env file with database info etc.

7运行 PHP工匠迁移

8往浏览器和写入的http:// YourLaravelHostProject /认证/注册

9-(可选)如果要更改登录/注册过程后修改 /家居着陆页 AuthController.php 应用程序\\ HTTP \\ \\控制器验证

9- (Optional) If you want to change /home landing page after login/registration process edit AuthController.php in app\Http\Controllers\Auth:

这行之后

use AuthenticatesAndRegistersUsers, ThrottlesLogins;

添加code。与您的位置

Add the code with your location

//forward after login
protected $redirectPath = '/YourLocation'; 

这是它。现在你有默认的登录/注册/重置密码模板的工作。你可以修改它继续使用。

That is it. Now you have the default login/register/reset password template working. You can modify it for further use.

PHP工匠迁移结果

登录页面的浏览器的快照

另外,也可以通过以下Laravel文档,以较简单的认证模板

It is also possible to make a simpler authentication template by following Laravel documentation.

Laravel身份验证文档 http://laravel.com/docs/5.1/authentication

Laravel authentication documentation http://laravel.com/docs/5.1/authentication

这篇关于Laravel 5用户无法成功验证后进行检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆