Skip to content
Dirk Tepe edited this page Jul 8, 2024 · 6 revisions

Basic Usage Example:

Route Middleware

In your routes/web.php:

Route::get('/', function () {
    return view('welcome');
})->middleware(\Subfission\Cas\Middleware\CASAuth::class);

Or with an alias:

Route::get('/', function () {
    return view('welcome');
})->middleware('cas.auth');

You may also use route groups to assign middleware to multiple routes:

Route::middleware([\Subfission\Cas\Middleware\CASAuth::class])->group(function () {
    Route::get('/', function () {
        return view('welcome');
    });

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

Via Routes Login

get('/auth/login', function(){
    cas()->authenticate();
});

You would now have access to cas()->user(), usually collected by middleware or custom authenticating guard.

Via Middleware Login

public function handle($request, Closure $next)
{
    if( ! cas()->checkAuthentication() )
    {
        if ($request->ajax()) {
            return response('Unauthorized.', 401);
        }
        cas()->authenticate();
    }
    session()->put('cas_user', cas()->user() );
    return $next($request);
}

You would now have access to the cas user via session or by cas()->user().

Logout From route

get('/auth/logout', [
    'middleware' => 'cas.auth', 
    function(){ 
        cas()->logout(); 
    }
]);

Note: The cas.auth middleware is optional, but you will need to handle the error when a user tries to logout when they do not have a cas session.