Laravel Auth ::登录 [英] Laravel Auth::login
问题描述
在我的应用程序上,我正在使用Steam API的登录名.
On my application, i'm using the login from the Steam API.
当用户通过Steam进行身份验证时,如果数据库中不存在该应用程序,则该应用程序会创建一个新用户,否则它将带来用户数据.
When the user authenticate on steam, the app create a new user if he doesn't exists on the database, else he bring the user data.
在此过程中,即使我创建或仅选择用户信息,我也会从用户那里获得一个数组,然后执行Auth :: login($ user,true);.
In this process, even if i create or just select the user info, i get an array from the user, and i do the Auth::login($user, true); .
在此功能上,如果我调试Auth :: user()他可以正确返回,则可以正常工作.
On this function it works, if i debug the Auth::user() he returns correctly.
在视图上,我也可以使用Auth :: guest(),它可以正常工作.
On the view i can use the Auth::guest() too and it works.
但是,如果我转到另一个页面,则只有登录的用户可以加入,Auth :: guest()返回true,Auth :: check()返回false,Auth :: user()返回NULL ...(在控制器).
But if i go to another page, that only logged users can join, Auth::guest() returns true, Auth::check() returns false, Auth::user() returns NULL... (on the controller).
如何在新控制器上访问auth方法?
How can i access the auth methods on the new controller?
由于身份验证失败的控制器:
Controller that fails with auth:
<?php
namespace App\Http\Controllers\Profile;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use Auth;
class ProfileController extends Controller
{
public function __construct()
{
if(Auth::guest()) {
return redirect()->route('home');
}
}
public function index()
{
// die(var_dump(Auth::user()->id));
return view('pages/profile/profile');
}
}
推荐答案
由于Laravel的体系结构,如果直接从控制器的构造中调用,Auth::user()
将始终返回null
.
Due to Laravel's architecture, Auth::user()
will always return null
if called directly from a controller's construct.
相反,您应该像下面这样引用"auth"中间件:
Instead you should reference the 'auth' middleware like the following:
class ProfileController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
...
这篇关于Laravel Auth ::登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!