获取Auth :: user返回null

时间:2015-10-26 02:55:26

标签: javascript api laravel laravel-5

我正在尝试从外部应用程序访问我的Authenticated用户,但即使在我成功登录用户后,使用Auth::user()也会返回null。

前端:

    //First, let user register
    API.Auth.register(params).done(function (result) {
        ...
        //Then automatically create a new game using their Auth'ed $user in backend
        API.User.newGame().done(function (result) {

Laravel:

public function postRegister(Request $request)
{
    ...

    //Login the new user
    Auth::login($this->create($request->all()));

    //Successfully returns auth'ed user
    return new Response(Auth::user(), 200);
}

public function newGame()
{
    $user = Auth::user(); //Returns null

    $totalUserGames = UserStats::where('user_id', '=', $user->id)->count();
    ...

错误:Trying to get property of non-object

newGame()只能成功找到Auth'ed用户:

Auth::loginUsingId(1);
$user = Auth::user(); //returns user

但是Auth::user()应该已经存储了用户在postRegister()初始创建的时间。

0 个答案:

没有答案