0
votes

On my application, i'm using the login from the Steam API.

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.

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); .

On this function it works, if i debug the Auth::user() he returns correctly.

On the view i can use the Auth::guest() too and it works.

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).

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');
    }
}
1

1 Answers

3
votes

Due to Laravel's architecture, Auth::user() will always return null if called directly from a controller's construct.

Instead you should reference the 'auth' middleware like the following:

class ProfileController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
...