Skip to content
logo1

Appfinz Tech Blogs

Javascript, Laravel, Jquery, Vue Js, Node Js Blogs

Primary Menu logo1

Appfinz Tech Blogs

  • PHP
  • Laravel
  • React
  • React Native
  • Payment Gateway
  • Javascript
  • Jquery
  • Shopify
  • WordPress
  • Angular JS
  • Vue JS
  • Website Trends
  • SEO
  • Node JS
  • Home
  • PHP
  • User Login with API Using Laravel 11
  • PHP
  • Laravel

User Login with API Using Laravel 11

1 year ago
User Login with API Using Laravel 11

Hello Artisans, making User Login with API Using Laravel 11 is very easy, In this blog, i will complete explain you that how you can implement and create a User Login with API Using Laravel 11, This in detailed Laravel API for User Creations involves setting up authentication, creating the necessary routes, controllers, and request handling.

Step 1: Install Laravel Sanctum

Laravel Sanctum provides a lightweight authentication system for SPAs (single page applications), mobile applications, and simple, token-based APIs.

First, install Sanctum via Composer:

composer require laravel/sanctum

Step 2: Publish the Sanctum Configuration

Publish the Sanctum configuration file using the following command:

php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"

Configure Database First and Then we will do migrations:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password

Run the Sanctum migrations:

php artisan migrate

Read Also:

Differences between PUT and PATCH? in laravel

Paypal Payment Gateway Integration in Laravel 11

Step 3: Configure Sanctum Middleware

Add Sanctum’s middleware to your api middleware group within your app/Http/Kernel.php file:

'api' => [
    \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
    'throttle:api',
    \Illuminate\Routing\Middleware\SubstituteBindings::class,
],

Step 4: Update User Model

Ensure your User model uses the HasApiTokens trait provided by Sanctum:

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;

    // Other model properties and methods
}

Step 5: Create Auth Controller

Create an AuthController to handle user registration, login, and logout:

php artisan make:controller AuthController

In AuthController.php, implement the following methods:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;

class AuthController extends Controller
{
    public function register(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:8|confirmed',
        ]);

        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]);

        return response()->json(['message' => 'User registered successfully']);
    }

    public function login(Request $request)
    {
        $request->validate([
            'email' => 'required|string|email',
            'password' => 'required|string',
        ]);

        $user = User::where('email', $request->email)->first();

        if (! $user || ! Hash::check($request->password, $user->password)) {
            throw ValidationException::withMessages([
                'email' => ['The provided credentials are incorrect.'],
            ]);
        }

        $token = $user->createToken('auth_token')->plainTextToken;

        return response()->json(['access_token' => $token, 'token_type' => 'Bearer']);
    }

    public function logout(Request $request)
    {
        $request->user()->tokens()->delete();

        return response()->json(['message' => 'Logged out successfully']);
    }
}

Step 6: Define Routes

Add routes for user registration, login, and logout in routes/api.php:

use App\Http\Controllers\AuthController;

Route::post('register', [AuthController::class, 'register']);
Route::post('login', [AuthController::class, 'login']);

Route::middleware('auth:sanctum')->group(function () {
    Route::post('logout', [AuthController::class, 'logout']);
});

Step 7: Test Your API

You can use tools like Postman or cURL to test your API endpoints.

  • POST /api/register: Register a new user.
  • POST /api/login: Login a user.
  • POST /api/logout: Logout a user (requires authentication).

In this blog, we have covered about API generation in Laravel, User Login API generation in Laravel

If you’re planning to revamp your online presence, don’t miss exploring our website designing services and custom website development solutions tailored to your business needs.

About Post Author

9e9449b06621124a84dc2b65c64536676c3fbc88d5e6f9ad9d60cbd72b9a1e20?s=150&d=identicon&r=g

Kishan Kumar || Founder @ Appfinz

administrator

𝗦𝗵𝗼𝗽𝗶𝗳𝘆 𝗣𝗹𝘂𝘀 𝗘𝘅𝗽𝗲𝗿𝘁 | Digital Marketing Consultant (Organic & Paid Outreach) | Built 𝗔𝗽𝗽𝗳𝗶𝗻𝘇 𝗧𝗲𝗰𝗵𝗻𝗼𝗹𝗼𝗴𝗶𝗲𝘀 | Working with Graphics, Website & Performance Marketing Enthusiasts.
Ask for 𝐖𝐞𝐛𝐬𝐢𝐭𝐞 𝐃𝐞𝐯𝐞𝐥𝐨𝐩𝐦𝐞𝐧𝐭 | 𝐒𝐨𝐜𝐢𝐚𝐥 𝐌𝐞𝐝𝐢𝐚 𝐎𝐮𝐭𝐫𝐞𝐚𝐜𝐡 | 𝐆𝐨𝐨𝐠𝐥𝐞 𝐂𝐚𝐦𝐩𝐚𝐢𝐠𝐧𝐬 | 𝐋𝐚𝐫𝐚𝐯𝐞𝐥 & 𝐒𝐡𝐨𝐩𝐢𝐟𝐲 𝐃𝐞𝐯𝐞𝐥𝐨𝐩𝐦𝐞𝐧𝐭

See author's posts

Post Views: 34
Tags: API for mobile apps, api in laravel, user login, user login with api

Continue Reading

Previous What’s the differences between PUT and PATCH? in laravel
Next How to change Password in Laravel Using Tinker Method

Please Support us

If our blogs help you to solve your problem, you can help us running this blogs by paying for a small coffee brew.

Google Pay/Phonepe/Paytm Number: +91 8340725097

If my blogs help you to find your answer and query, then help us with above mentioned links

React JS

  • React
  • React Native

Difference Between Ionic React Vs React Native

5 years ago
  • React
  • React Native

What is the difference between React Native and React?

6 years ago

Laravel

  • Laravel
  • PHP

Building a Simple AJAX CRUD Application in Laravel: Add, Edit, Delete, and List

12 months ago
  • Laravel

Laravel Custom Authentication | Registration, Login, Logout Process | Laravel login with session

12 months ago
  • Laravel

How to change Password in Laravel Using Tinker Method

1 year ago
  • PHP
  • Laravel

User Login with API Using Laravel 11

1 year ago
  • Laravel

What’s the differences between PUT and PATCH? in laravel

1 year ago

Recent Blogs

  • Social Media

The upsurge of niche social media platforms and what that means for your brand

5 years ago
  • SEO

Best Scientific Equipment Manufacturers in India

4 days ago
  • PHP

Why Every Entrepreneur Needs a Digital Business Card in 2025

4 days ago
  • Shopify

Shopify Company Guide 2025: Success for Business Owners

3 weeks ago
  • Shopify

Shopify Ecommerce Website Design Guide for 2025

4 weeks ago
  • Website Trends

Best Small Business Website Ideas for India & US in 2025

1 month ago

Appfinz Technologies

Appfinz Technologies has been in the industry for more than 12 years, holding a core motive to make a change in the world by providing solutions perfectly crafted with intelligence and creativity. the company is gradually yet constantly spreading its legs across the world leaving the rat race for the rest and striving to become better than the best!

Our services

  • Shopify Development Company
  • SEO Services In India
  • SEO Packages
  • Digital Marketing Packages
  • Social Media Marketing

Categories

Angular JS Artificial Intelligence Ionic Framework Javascript Jquery Laravel Marketing Node JS Payment Gateway PHP Python React React Native SEO Shopify Social Media VPN Vue JS Website Trends Wordpress
  • Home
  • Instagram
  • Facebook
  • Twitter
  • Linkedin
  • Github
© Appfinz Technologies | All rights reserved.