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
  • Laravel
  • PHP

User Login with API Using Laravel 11

10 months ago
User Login with API Using Laravel 11

Loading

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

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

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?

5 years ago

Laravel

  • Laravel
  • PHP

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

10 months ago
  • Laravel

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

10 months ago
  • Laravel

How to change Password in Laravel Using Tinker Method

10 months ago
  • Laravel
  • PHP

User Login with API Using Laravel 11

10 months ago
  • Laravel

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

10 months ago

Recent Blogs

  • Social Media

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

4 years ago
  • PHP

WordPress Hooks and Filter | Complete Overview of WordPress hooks

3 weeks ago
  • Marketing
  • SEO

What are the 7 Ps of Marketing?

8 months ago
  • PHP

What is Fetch PHP? How to use it inside PHP

8 months ago
  • Laravel
  • PHP

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

10 months ago
  • Laravel

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

10 months 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.