I want to visit homepage as a guest but facing login page continuously in Laravel

I’m facing login page again and again when try to go to the homepage. I didn’t add any middleware to homepage route but still I’m facing this issue.

My Login Controller

protected $redirectTo = '/';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function redirectToProvider()
    {
        return Socialite::driver(request()->provider)->redirect();
    }


    public function handleProviderCallback()
    {
        $provider = request()->provider;
        $providerUser = Socialite::driver($provider)->user();
        if($providerUser->getEmail() == null) {
            $user = User::where($provider . '_id', $providerUser->getId())->first();
        } else {
            $user = User::where('email', $providerUser->getEmail())->first();
        }
        if($user && $user->$provider . '_id' == null) {
            dd('test');
            $user->update([$provider . '_id' => $providerUser->getId()]);
        }
        if(!$user) {
            $user = User::create([
                'email' => $providerUser->getEmail(),
                'name' => $providerUser->getName(),
                $provider . '_id' => $providerUser->getId(),
            ]);
        }

        auth()->login($user, true);

        return redirect($this->redirectTo);

        // $user->token;
    }

    public function showLoginForm()
    {
        session()->put('previousUrl', url()->previous());

        return view('auth.login');
    }

    public function redirectTo()
    {
        return str_replace(url('/'), '', session()->get('previousUrl', '/'));
    }

I don’t know the issue is in controller or in routes.

Routes

Route::get('/', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1047757c737f7d7540717775537f7e64627f7c7c756250797e747568">[email protected]</a>')->name('welcome');
Auth::routes();
Route::get('/login/{provider}', 'Auth<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b8f4d7dfd1d6fbd7d6cccad7d4d4ddcaf8cadddcd1cadddbccecd7e8cad7ced1dcddca">[email protected]</a>');
Route::get('/login/{provider}/callback', 'Auth<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eea281898780ad81809a9c8182828b9cae868f808a828bbe9c8198878a8b9cad8f82828c8f8d85">[email protected]</a>');

I can visit the homepage only when I logged in but I want to see it as a guest.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The home route is protected, you can see this in construct method of HomeController. To make it unprotected, try to comment the line in constrct method.
To prevent errors in home view, you have to edit this view too.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x