Set up Laravel Auth guards using Eloquent in seconds

Overview

Nightguard

Set up Auth guards using Eloquent in seconds.

Unit Tests

Introduction

Laravel guards provide a super convenient way of authorizing different areas of your application based on the type of your user. For a lot of projects, gates and permissions suffice, but guards go one step further by allowing you to use dedicated eloquent models for each area of authentication.

However, guards can be a little confusing to set up, especially if you're new to it all. The documentation is thorough, but because of the low level nature of guards, it can be overwhelming.

That's why Nightguard was created. Rather than you having to dig into config files and jump from file to file, you can have a guard set up around your own custom Eloquent model with a single line of code!

Installation

You can install Nightguard via composer:

composer require lukeraymonddowning/nightguard

Usage

Imagine we want an admin panel for our application. We want to have a dedicated administrators table, and an Administrator Eloquent model. Only administrators should be allowed into the admin panel. Even if a User model is logged in under the default guard, they should not have access to any admin route.

Let's start by creating a model and migration for our administrators. Nightguard provides a super convenient Artisan command that will create a model and migration with all the columns you'll need for authentication:

php artisan nightguard:model Administrator

That was simple!

With that out of the way, all that's left is to set up the guard. Head in to your AuthServiceProvider, and add the following code to your boot method:

public function boot() 
{
    Nightguard::create(App\Models\Administrator::class);
}

...and voilà! You've successfully registered a gate that only an authenticated Administrator can access.

If you create a route protected by your new 'administrator' gate, it will be protected:

Route::get('example-url', fn() => 'Super secret!')->middleware('auth:administrator');

The name of the guard is set automatically based on the class name of the model you pass to the create method. The guard name will always be singular and kebab-cased.

Nightguard Facade

The Nightguard Facade includes the following methods:

create($model, $guard = null)

This is how you register a new Eloquent guard. The first parameter should be the class name of your eloquent model (eg: App\Models\Administrator::class).

The second parameter is entirely optional and is only needed if you want to use a custom guard name. When omitted, Nightguard will guess the guard name based on your model name.

You should place these method calls in the boot method of one of your Service Providers.

Commands

Nightguard provides the following Artisan commands for you to use:

php artisan nightguard:model YourModelName

This command allows you to quickly scaffold files for your new Authenticatable Eloquent model. The database will have all the required columns, and the model will extend the correct classes and have the desired traits and casts out of the box.

You might also like...
Simple PASETO Auth support for Laravel PHP Framework

Laravel PASETO Simple PASETO Auth for Laravel PHP Framework using paragonie/paseto under the hood. Installation Standard Composer package installation

Light-weight role-based permissions system for Laravel 6+ built in Auth system.

Kodeine/Laravel-ACL Laravel ACL adds role based permissions to built in Auth System of Laravel 8.0+. ACL middleware protects routes and even crud cont

Stateless HTTP basic auth for Laravel without the need for a database.
Stateless HTTP basic auth for Laravel without the need for a database.

Laravel Very Basic Auth Documentation available in: 🇬🇧 English 🇯🇵 日本語 This package allows you to add a HTTP Basic Auth filter on your routes, with

Hierarchical Rol-Permission Based Laravel Auth Package with Limitless Hierarchical Level of Organizations
Hierarchical Rol-Permission Based Laravel Auth Package with Limitless Hierarchical Level of Organizations

AAuth for Laravel Hierarchical Rol-Permission Based Laravel Auth Package with Limitless Hierarchical Level of Organizations Features Organization Base

Google Auth Library for PHP

This is Google's officially supported PHP client library for using OAuth 2.0 authorization and authentication with Google APIs.

Sliding captcha for dcat-admin auth / dcat-admin登陆 滑动验证插件 多平台支持
Sliding captcha for dcat-admin auth / dcat-admin登陆 滑动验证插件 多平台支持

dcat-admin登陆 滑动验证插件 多平台支持 dcat-admin登陆 滑动验证插件 多平台支持 另有 laravel-admin版 Demo演示 演示站点(暂时无,目前地址为laravel-admin版的演示地址) 支持(按照字母顺序) 顶象 ✔️ 极验 ✔️ hCaptcha(和谷歌Rec

HTTP Basic Auth Guard for Lumen 5.x

HTTP Basic Auth Guard HTTP Basic Auth Guard is a Lumen Package that lets you use basic as your driver for the authentication guard in your application

Configurable Basic Auth based on Pimcore Documents

CORS Property Basic Auth This bundles allows to add basic auth based on Properties on Pimcore Documents. Simply use these properties password_enabled

Auth is a module for the Yii PHP framework that provides a web user interface for Yii's built-in authorization manager

Auth is a module for the Yii PHP framework that provides a web user interface for Yii's built-in authorization manager (CAuthManager). You can read more about Yii's authorization manager in the framework documentation under Authentication and Authorization.

Releases(0.2.0)
Owner
Luke Downing
Luke Downing
CakeDC Auth Objects is a refactor of the existing Auth objects present in the CakeDC Users Plugin, to let anyone else use them in their projects.

CakeDC Auth Objects is a refactor of the existing Auth objects present in the CakeDC Users Plugin, to let anyone else use them in their projects.

Cake Development Corporation 24 Sep 23, 2022
A Native PHP MVC With Auth. If you will build your own PHP project in MVC with router and Auth, you can clone this ready to use MVC pattern repo.

If you will build your own PHP project in MVC with router and Auth, you can clone this ready to use MVC pattern repo. Auth system is implemented. Works with bootstrap 5. Composer with autoload are implemented too for future composer require.

null 2 Jun 6, 2022
A PHP boilerplate based on Slim Framework, for start projects with Eloquent ORM, Validation, Auth (JWT), Repositories and Transformers ready

A PHP boilerplate based on Slim Framework, for start projects with Eloquent ORM, Validation, Auth (JWT), Repositories and Transformers ready.

Damiano Petrungaro 58 Aug 10, 2022
Laravel auth-boilerplate using sanctum

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Jigar Bhaliya 3 Mar 2, 2022
Laravel Auth is a Complete Build of Laravel 8 with Email Registration Verification, Social Authentication, User Roles and Permissions, User Profiles, and Admin restricted user management system.

Laravel Auth is a Complete Build of Laravel 8 with Email Registration Verification, Social Authentication, User Roles and Permissions, User Profiles, and Admin restricted user management system. Built on Bootstrap 4.

Jeremy Kenedy 2.8k Dec 31, 2022
Laravel starter kit with Livewire & Bootstrap 5 auth scaffolding.

Laravel Livewire Auth Laravel starter kit with Livewire & Bootstrap 5 auth scaffolding. Requirements NPM Installation Create a new Laravel app: larave

null 8 Sep 11, 2021
JWT auth for Laravel and Lumen

JWT Artisan Token auth for Laravel and Lumen web artisans JWT is a great solution for authenticating API requests between various services. This packa

⑅ Generation Tux ⑅ 141 Dec 21, 2022
Laravel Auth guard for FusionAuth JWT

Laravel FusionAuth JWT Implement an Auth guard for FusionAuth JWTs in Laravel. It ships with also a middleware to check against the user role. Install

Theraloss 7 Feb 21, 2022
Files Course Laravel Micro Auth and Authorization

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

EspecializaTi 8 Oct 22, 2022
Simple JWT Auth support for Laravel PHP Framework

Laravel JWT Simple JWT Auth for Laravel PHP Framework using Firebase JWT under the hood. Installation Standard Composer package installation: composer

Ricardo Čerljenko 34 Nov 21, 2022