dry0106
dry0106
2015-12-15 05:23

如何在Laravel 5中调用firebase / php-jwt类

已采纳

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongnang8192 dongnang8192 6年前

    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.

    点赞 评论 复制链接分享
  • dpikoto468637 dpikoto468637 2年前

    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");
        }
    
    点赞 评论 复制链接分享

相关推荐