sandip kakade
sandip kakade

Reputation: 1356

How to authenticate RESTful API in Laravel 5.2?

I created RESTful API in Laravel 5.2. I need to authenticate everytime a user accesses AbcController. I created my routes as below:

Route::group(['prefix' => 'user'], function () {
   Route::post('login', 'Auth\AuthController@authenticate');
   Route::post('register', 'UserController@userRegister');
   Route::post('register', 'UserController@userRegister');
});

Route::group(['prefix' => 'abc'], function () {
   Route::get('abclists', 'AbcController@movieLists');
   Route::get('upcoming', 'AbcController@upcomingMovieLists');
});

When user registers for the first time or logs-in, at that time I create random 64 bit char string for session token. This session token is stored in a different table. Now I am confused how to check if a user is logged in or not in AbcController. I am not using any library for this. Please suggest how to reslove this problem.

Upvotes: 1

Views: 295

Answers (1)

TwilightStar
TwilightStar

Reputation: 41

The easiest way to do this would be to use middleware. In the handle() method you can make the check against the token stored in the table.

<?php

namespace App\Http\Middleware;

class AgeMiddleware
{

    public function handle($request)
    {
        if(TOKEN == $request->get('token')
        return $next($request);
    }

}

You can look here for more info: Laravel Docs

Upvotes: 1

Related Questions