Categories

Thursday, March 28, 2024
#919814419350 therichposts@gmail.com
Laravel 7PhpRest Api

Laravel 7 Custom Access Token API Authentication

laravel 7 create custom access token

Hello to all, welcome to therichpost.com. In this post, I will tell you, Laravel 7 Custom Access Token API Authentication.

Post Working:

In this post, I am creating custom access token during auth login and auth registration. I am doing this in Laravel 7.

Here is the working image from Passport:

laravel 7 create custom access token
laravel 7 create custom access token
  1. Here is the link from where you can get the laravel 7 setup and Laravel 7 auth:
    Laravel 7 setup and Laravel 7 Auth:

2. We need to create our API’S Routes with below code in routes/api.php:

Route::post('login', 'API\AuthController@login');
Route::post('register', 'API\AuthController@register');

 

3. We need to create API Folder Into our app\Http\Controllers:

api folder

4. In app\Http\Controllers\API Folder, we need to create new file named AuthController.php file and add below code into this file:

<?php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request; 
use App\Http\Controllers\Controller; 
use Illuminate\Support\Facades\Auth; 
use App\User; 
use Validator;
use Illuminate\Support\Str;
class AuthController extends Controller 
{
  /** 
   * Login API 
   * 
   * @return \Illuminate\Http\Response 
   */ 
   private $apiToken;
   public function __construct()
    {
    $this->apiToken = uniqid(base64_encode(Str::random(40)));
    }
  public function login(Request $request){ 
    if(Auth::attempt(['email' => $request->email, 'password' => $request->password])){ 
      $user = Auth::user(); 
     
    $success['token'] = $this->apiToken;
    $success['name'] =  $user->name;
      return response()->json([
        'status' => 'success',
        'data' => $success
      ]); 
    } else { 
      return response()->json([
        'status' => 'error',
        'data' => 'Unauthorized Access'
      ]); 
    } 
  }
    
  /** 
   * Register API 
   * 
   * @return \Illuminate\Http\Response 
   */ 
  public function register(Request $request) 
  { 
    $validator = Validator::make($request->all(), [ 
      'name' => 'required', 
      'email' => 'required|email', 
      'password' => 'required', 
      'c_password' => 'required|same:password', 
    ]);
    if ($validator->fails()) { 
      return response()->json(['error'=>$validator->errors()]);
    }
    $postArray = $request->all(); 
    $postArray['password'] = bcrypt($postArray['password']); 
    $user = User::create($postArray); 
    
    $success['token'] = $this->apiToken;	
    $success['name'] =  $user->name;
    return response()->json([
      'status' => 'success',
      'data' => $success,
    ]); 
  }
}

 

5. Here you can check the working login example in Postman:

laravel 7 create custom access token
laravel 7 create custom access token

If you have any kind of query then please do comment below.

Jassa

Thank you.

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

5 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.