Ad

Laravel 5.2 Authentication ~ Routes Based On Authentication State

- 1 answer

I am trying to do something that I believe to be simple, I'm just having some trouble along the way. My code below includes my routes.php and my verify.php that authenticates a user. Basically, I want the routing to the homepage to change based on if the user is authenticated or not.

So first, I have the controller (verify.php)

<?php
namespace App\Http\Controllers;
use App\Email;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Google_Client; 
use Auth;
use App\User;
class verify extends Controller
{

public function verifyIdToken(Request $request)
  {
$user = User::where('name', 'Molly')->first();
    Auth::login($user);
    return;
}
  }

This script is hit in an ajax request when the user first satisfies some conditions to be logged in, but that is client-side logic and not important at this point. The idea is that once the script is hit, the user is logged in.

To add more detail:

The header of my routes.php is this:

use Auth;
use Illuminate\Http\Request;

Is there something I am missing?

My routes.php logic is now this:

if (Auth::guest()) {
    Route::get('/', function () {
        return view('welcome');
    });
} else {
    Route::get('/', function () {
        return view('mainview');
    });
}

But it still doesn't work, even though in verify.php I have added in the following confirmation:

if (Auth::check($user))
{
    return view('aviewII')->with(['verify' => json_encode("Yes!")]);
}

Which I am getting returned in my ajax requests.

So even though that tells me I am authenticated, I still can't get the authenticated view. What should I try next?

Edit: My routes.php in its entirety:

<?php

/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
use App\punsmodel;
use Illuminate\Http\Request;

if (Auth::guest()) {
    Route::get('/', function () {
        return view('welcome');
    });
} else {
    Route::group(['middleware' => 'auth'], function () {
    Route::get('/', function ()    {
       return view('mainview');
    });

     Route::get('mainview', function ()    {
       return view('mainviewMolly');
    });
});
}

Route::get('puns', function () {
    return view('puns');
});

Route::post('google' ,  [
    'as' => 'verify.index',
    'uses' => '[email protected]'
]);

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/

Route::group(['middleware' => ['web']], function () {
    //
});
Ad

Answer

I’d just have a conditional in routes.php:

if (Auth::guest()) {
    Route::get('/', function () {
        return view('home.logged_out');
    });
} else {
    Route::get('/', function () {
        return view('home.logged_in');
    });
}

Also, a couple of styling tips:

  • Classes should have “StudlyCased” names, so class verify should be class Verify.
  • Order your use statements alphabetically, makes scanning much easier ;)

UPDATE:

If you want to check the authentication status of a user, you need to make sure those routes are inside the group with middleware applied:

Route::group(['middleware' => 'web'], function () {
    if (Auth::guest()) {
        return view('home.logged_out');
    } else {
        return view('home.logged_in');
    }
});
Ad
source: stackoverflow.com
Ad