REST API with auth using Laravel 8 and Sanctum

Overview

Laravel REST API with Sanctum

This is an example of a REST API using auth tokens with Laravel Sanctum

Usage

Change the .env.example to .env and add your database info

For SQLite, add

DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306

Create a database.sqlite file in the database directory

# Run the webserver on port 8000
php artisan serve

Routes

# Public

GET   /api/products
GET   /api/products/:id

POST   /api/login
@body: email, password

POST   /api/register
@body: name, email, password, password_confirmation


# Protected

POST   /api/products
@body: name, slug, description, price

PUT   /api/products/:id
@body: ?name, ?slug, ?description, ?price

DELETE  /api/products/:id

POST    /api/logout
You might also like...
Laravel Livewire UI, Auth, & CRUD starter kit.
Laravel Livewire UI, Auth, & CRUD starter kit.

Laravel Livewire Ui This package provides Laravel Livewire & Bootstrap UI, Auth, & CRUD scaffolding commands to make your development speeds blazing f

Fast and simple implementation of a REST API based on the Laravel Framework, Repository Pattern, Eloquent Resources, Translatability, and Swagger.

Laravel Headless What about? This allows a fast and simple implementation of a REST API based on the Laravel Framework, Repository Pattern, Eloquent R

Random Anime Pictures And Quotes Rest API, Toshino Kyoko.
Random Anime Pictures And Quotes Rest API, Toshino Kyoko.

Toshino Kyoko Toshinou Kyouko is the founder of the amusement club. Goes so wild that no one can keep up with her, but it's not as if anyone even trie

A suit of basic files samples to build a light-weight , short-handed & a strong Laravel REST API Applications.

Concerning Laravel. I found myself repeating a very basic form of code each time i begin a new REST API. in which the whole Interface's pieces are dev

Laravel API REST Skeleton Stack

Skeleton stack for API development. Based on Laravel 9 (stable) with Oauth2 API Authentication. Also uses Laravel's and 3'd party vendors.

User authentication REST API with Laravel (Register, Email verification, Login, Logout, Logged user data, Change password, Reset password)

User Authentication API with Laravel This project is a user authentication REST API application that I developed by using Laravel and MySql. Setup Fir

Proyecto Start-Basic sobre Login y crud de usuarios, mediante Api Rest, usando la plantilla AdminLte 3.1 y manejo de roles y permisos con spatie y autenticacion JWT
Proyecto Start-Basic sobre Login y crud de usuarios, mediante Api Rest, usando la plantilla AdminLte 3.1 y manejo de roles y permisos con spatie y autenticacion JWT

Proyecto Start-Basic sobre Login y crud de usuarios, mediante Api Rest, usando la plantilla AdminLte 3.1 y manejo de roles y permisos con spatie y autenticacion JWT

Builds nice, normalized and easy to consume REST JSON responses for Laravel powered APIs.

REST API Response Builder for Laravel Master branch: Development branch: Table of contents Introduction Why should I use it? Usage examples Features E

Execute Laravel Artisan commands via REST APIs and HTTP requests safely.

Artisan Api There might be some times you wanted to execute an Artisan command, but you did not have access to shell or SSH. Here we brought REST API

Comments
  • Error when trying to use POST/PUT commands

    Error when trying to use POST/PUT commands

    When I try to use these requests: POST /api/products or PUT /api/products/:id I am getting the following error: Symfony\Component\Routing\Exception\RouteNotFoundException: Route [login] not defined. in file /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php on line 429

    #1 /Applications/MAMP/htdocs/laravel-sanctum-api/app/Http/Middleware/Authenticate.php(18): route('login')
    #2 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(83): App\Http\Middleware\Authenticate->redirectTo(Object(Illuminate\Http\Request))
    #3 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(68): Illuminate\Auth\Middleware\Authenticate->unauthenticated(Object(Illuminate\Http\Request), Array)
    #4 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(42): Illuminate\Auth\Middleware\Authenticate->authenticate(Object(Illuminate\Http\Request), Array)
    #5 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Auth\Middleware\Authenticate->handle(Object(Illuminate\Http\Request), Object(Closure), 'sanctum')
    #6 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/sanctum/src/Http/Middleware/EnsureFrontendRequestsAreStateful.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #7 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful->Laravel\Sanctum\Http\Middleware\{closure}(Object(Illuminate\Http\Request))
    #8 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #9 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/sanctum/src/Http/Middleware/EnsureFrontendRequestsAreStateful.php(34): Illuminate\Pipeline\Pipeline->then(Object(Closure))
    #10 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful->handle(Object(Illuminate\Http\Request), Object(Closure))
    #11 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #12 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Routing/Router.php(697): Illuminate\Pipeline\Pipeline->then(Object(Closure))
    #13 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Routing/Router.php(672): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
    #14 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Routing/Router.php(636): Illuminate\Routing\Router->runRoute(Object(Illuminate\Http\Request), Object(Illuminate\Routing\Route))
    #15 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Routing/Router.php(625): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
    #16 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(166): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
    #17 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(128): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
    #18 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #19 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
    #20 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(21): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #21 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
    #22 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #23 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
    #24 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(86): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #25 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance->handle(Object(Illuminate\Http\Request), Object(Closure))
    #26 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/fruitcake/laravel-cors/src/HandleCors.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #27 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fruitcake\Cors\HandleCors->handle(Object(Illuminate\Http\Request), Object(Closure))
    #28 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #29 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(167): Fideloper\Proxy\TrustProxies->handle(Object(Illuminate\Http\Request), Object(Closure))
    #30 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(103): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
    #31 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(141): Illuminate\Pipeline\Pipeline->then(Object(Closure))
    #32 /Applications/MAMP/htdocs/laravel-sanctum-api/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(110): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
    #33 /Applications/MAMP/htdocs/laravel-sanctum-api/public/index.php(52): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
    #34 /Applications/MAMP/htdocs/laravel-sanctum-api/server.php(21): require_once('/Applications/M...')
    #35 {main}
    

    I am using the token which I have got via login. For example in header token 2|LIpDVDG2Ry9rJMlevY6wAsQYiWxl1Tm9MdSc****

    Please help.

    opened by Ildar5 1
Owner
Brad Traversy
Full stack web developer and online instructor, specializiing in mostly JS, but also write Python, PHP and some other stuff.
Brad Traversy
Laravel Sanctum support for Laravel Lighthouse

Lighthouse Sanctum Add Laravel Sanctum support to Lighthouse Requirements Installation Usage Login Logout Register Email Verification Forgot Password

Daniël de Wit 43 Dec 21, 2022
Laravel Sanctum, Websockets & Vue [SPA]

Laravel Auth [SPA] 2021-07-15_22-24-33.mp4 composer require laravel/ui php artisan ui:auth Laravel Sanctum composer require laravel/sanctum php art

Kritish Dhaubanjar 6 Jun 1, 2022
A Laravel 8 Project Implement with GraphQL With Sanctum APIs Authentications Which utilized in Any Frontend or Any Mobile Application Programs.

A Laravel 8 Project Implement with GraphQL With Sanctum APIs Authentications Which utilized in Any Frontend or Any Mobile Application Programs.

Vikas Ukani 3 Jan 6, 2022
Authentication for SPA (Laravel Sanctum + Vue.js)

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

Damir 0 Dec 30, 2021
Laravel UI, Auth, & CRUD scaffolding package using Bootstrap & Livewire.

bastinald/ux Laravel UI, Auth, & CRUD scaffolding package using Bootstrap & Livewire. Features Automatic migrations Automatic routing Automatic passwo

null 33 Nov 26, 2022
Message box application written in Laravel - simple inbox design using auth UI.

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

null 0 Dec 25, 2021
A Laravel starter kit with auth scaffolding using Livewire & Bootstrap.

Zephyr This package is a Laravel starter kit with auth scaffolding using Livewire & Bootstrap. It was created for people who prefer using Livewire & B

null 23 Aug 26, 2022
Backend application using Laravel 9.x REST APIs for games topup from digiflazz.com and payment gateway using xendit.co

TOPUP - Laravel 9.x REST API Documentation is still on progress. For now, you can fork this postman collection Installation Clone this project git clo

Muhammad Athhar Kautsar 46 Dec 17, 2022
The fastest way to make a powerful JSON:API compatible Rest API with Laravel.

The first fully customizable Laravel JSON:API builder. "CRUD" and protect your resources with 0 (zero) extra line of code. Installation You can instal

BinarCode 288 Aug 8, 2022
Lumen rest api demo with Dingo/Api, JWT, CORS, PHPUNIT

lumen-api-demo 这是一个比较完整用 lumen 5.7 写的的 REST API 例子。使用了 dingo/api ,jwt 实现登录,功能上很简单,登录,注册,发帖,评论,单元测试(正在补充)。 lumen5.x 请看对应的分支 有需要随时联系我 lumen/laravel/rest

Yu Li 859 Oct 25, 2022