Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call firebase/php-jwt class in Laravel 5

Tags:

php

laravel-5

Currently I want to implement firebase/php-jwt into laravel 5

I've run the command composer require firebase/php-jwt under my laravel 5 project, and I get

"require": {
    ...
    "firebase/php-jwt": "^3.0"
},

in package.json

from config/app.php, I need inject this library, but don't know how to do.


Solution:

use tymondesigns/jwt-auth instead,
and follow JSON Web Token Tutorial: An Example in Laravel and AngularJS

Eventhough your front-end not using angular, it doesn't matter

like image 440
atom2ueki Avatar asked Dec 15 '15 05:12

atom2ueki


2 Answers

After installing it via composer then

Add these two lines to your app.php

'J42\LaravelFirebase\LaravelFirebaseServiceProvider',

and

'Firebase'        => 'J42\LaravelFirebase\LaravelFirebaseFacade'

Also don't forget to add the credentials in your config/database.php

Tip 1 :

You shall with the basic requests like Firebase::get('/my/path');

Here's the good resource to have a start with

Tip 2 : Here is the beautiful resource that you shall refer.

like image 189
Sulthan Allaudeen Avatar answered Oct 02 '22 15:10

Sulthan Allaudeen


It's is resolved in version 5 as follow: Assuming you have it from composer

    "require": {
        ...
        "firebase/php-jwt": "^5.0",
    },

Then you just call this in your class:

use Firebase\JWT\JWT;

...
protected function generateJwt(){
        if (config('jwt.secret')) {
            $now_seconds = time();
            $payload = array(
                    "iss" => ...,
                    "sub" => ...,
                    "aud" => ...,
                    "iat" => $now_seconds,
                    "exp" => $now_seconds + 60*60,  // Maximum expiration time is one hour
            );

            $this->jwt = JWT::encode($payload, config('jwt.secret'), "HS256");
        }

        Log::error("Missing JWT_SECRET in .env");
    }
like image 26
4givN Avatar answered Oct 02 '22 16:10

4givN