Laravel 9

Laravel 9 Sanctum Authentication REST API

in this will teach about the Sanctum Authentication REST API with token step by step.
we have created the user register api and login api using Sanctum.

Install laravel

composer create-project laravel/laravel lms-app

after installed  the laravel .Open up the project into vs code editor.

Create the Database in Mysql. which name dblms

After that open the .env file and changes database name as dblms.

Install Laravel Sanctum

composer require laravel/sanctum

Publish Sanctum

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

After that create and tables and token running the migration command

php artisan migrate

After that your tables has been created.and model also created.

Create the Controller

 php artisan make:controller UserController

Inside the UserController paste this below code

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use Auth;

class UserController extends Controller
{

    public function register(Request $request)
    {
          $user = new User();
          $user->name = $request->name;
          $user->email = $request->email;
          $user->password = bcrypt($request->password);
          $user->save();

          $token = $user->createToken('API TOKEN');

          return response()->json([
            'message' => "User registered",
            'token' => $token->plainTextToken
        ],200);
    }

    public function login(Request $request)
    {
      if(!Auth::attempt($request->only('email','password')))
     {
          return response()->json([
            'message' => "Invalid Username or Password",
        ],401);
     }
      $user = User::where('email', $request->email)->first();
      $token = $user->createToken('API TOKEN');
        return response()->json([
          'message' => "User Logged in",
          'token' => $token->plainTextToken
      ],200);       
}
    
}

routes

Open in file api.php

<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});

Route::post("/register",[UserController::class,'register']);
Route::post("/login",[UserController::class,'login']);

i have attached the video link below. which will do this tutorials step by step.

 

admin

Recent Posts

Employee Working Hours Calculation System using C#.net

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…

1 week ago

Java Payroll System Calculate Employee Overtime

Act as a Java developer to create a program that calculates the gross wage for…

2 weeks ago

Employee Working Hours Calculation System using Java

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…

2 weeks ago

Laravel 11 School Management System

In this tutorial, we will teach you how to create a simple school management system…

2 weeks ago

How to Make Admin Panel Using React MUI

I have design the Admin Basic templete using React MUI Design Admin Dashboard and Login.Here…

4 weeks ago

Install Laravel Breeze for Authentication Using Laravel 11

In this tutorial ,i am to going teach the Laravel Breeze.Laravel Breeze provides a simple…

1 month ago