Menu

Create a RESTful API Authentication Using Laravel Passport

November 11th, 2017

  • *     * @param  \Illuminate\Http\Request  $request     * @param  $type     * @return function = [ ];        $error = false;        if($type == = = = = true;                $errors = == “create todo”){            $validator = = = = = true;                $errors = == “update todo”){            $validator = = = = = true;                $errors = [“error” = Display a listing of the resource.
  • *     * @param  \Illuminate\Http\Request  $request     * @return function index(Request $request)    {        return $this-prepareResult(true, $request-user()-todo()-get(), [],”All user todos”);    }      /**     * Display the specified resource.
  • *     * @param  \App\Todo  $todo     * @return function show(Request $request,Todo == $this-prepareResult(true, $todo, [],”All results $this-prepareResult(false, [], “unauthorized”,”You are not authenticated to view this Store a newly created resource in storage.
  • *     * @param  \Illuminate\Http\Request  $request     * @return function store(Request $request)    {        $error = todo”);        if ($error[‘error’]) {            return $this-prepareResult(false, [], $error[‘errors’],”Error in creating todo”);        } else {            $todo = $this-prepareResult(true, $todo, $error[‘errors’],”Todo Update the specified resource in storage.
  • *     * @param  \Illuminate\Http\Request  $request     * @param  \App\Todo  $todo     * @return function update(Request $request, Todo == = todo”);            if ($error[‘error’]) {                return $this-prepareResult(false, [], $error[‘errors’],”error in updating data”);            } else {                $todo = $this-prepareResult(true, $todo, $error[‘errors’],”updating $this-prepareResult(false, [], “unauthorized”,”You are not authenticated to edit this Remove the specified resource from storage.

Laravel Passport offers a great way of creating RESTful API. Check out this great tutorial on creating and testing Laravel Passport powered REST API.

In today’s article, I am going to create a REST API in Laravel using Laravel Passport.

To demonstrate the API, I am going to continue the creating the Laravel powered ToDo app. I will use Laravel Passport access token in it. The Github repo for the todoappwithlaravel has been updated to this Passport API version.

Let’s get started.

To install Laravel Passport, run the following command:

Once the installation finishes, add the following provider in the config/app.php file:

Now, run the following command to migrate Laravel Passport tables:

Once all the tables have been migrated, run the following command to install it completely:

method, add the following line:

At this point, Passport has been installed. Next, I will configure it in the Users model.

trait in it.

I will now create a controller which will handle REST API requests.

Now open the newly created file. Let’s start with creating the method for generating access tokens.

       $ validate = $ this -> validations ( $ request , “login” ) ;

       if ( $ validate [ “error” ] ) {

           return $ this -> prepareResult ( false , [ ] , $ validate [ ‘errors’ ] , “Error while validating user” ) ;

       $ user = User :: where ( “email” , $ request -> email ) -> first…

Create a RESTful API Authentication Using Laravel Passport