{"id":18449919,"url":"https://github.com/alincode/laravel-jwt-sandbox","last_synced_at":"2025-04-18T00:21:04.175Z","repository":{"id":81410992,"uuid":"104628223","full_name":"alincode/laravel-jwt-sandbox","owner":"alincode","description":null,"archived":false,"fork":false,"pushed_at":"2017-09-24T08:02:35.000Z","size":169,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-15T23:00:02.253Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alincode.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-09-24T07:56:33.000Z","updated_at":"2017-09-24T07:56:54.000Z","dependencies_parsed_at":null,"dependency_job_id":"d72e49d8-a2ff-476c-8b4b-a943a134ee10","html_url":"https://github.com/alincode/laravel-jwt-sandbox","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alincode%2Flaravel-jwt-sandbox","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alincode%2Flaravel-jwt-sandbox/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alincode%2Flaravel-jwt-sandbox/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alincode%2Flaravel-jwt-sandbox/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alincode","download_url":"https://codeload.github.com/alincode/laravel-jwt-sandbox/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249167442,"owners_count":21223505,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T07:22:40.612Z","updated_at":"2025-04-15T23:00:06.016Z","avatar_url":"https://github.com/alincode.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel with JWT\n\n### 安裝\n\n```\ncomposer create-project --prefer-dist laravel/laravel laravel-jwt-sandbox\ncomposer require tymon/jwt-auth\n```\n\n### 配置\n\n**config/app.php**\n\n```\n// aliases\n\n'JWTFactory' =\u003e Tymon\\JWTAuth\\Facades\\JWTFactory::class,\n```\n\n**app/HTTP/Kernel.php**\n\n```\n//  routeMiddleware\n\n'jwt.auth' =\u003e \\Tymon\\JWTAuth\\Middleware\\GetUserFromToken::class,\n'jwt.refresh' =\u003e \\Tymon\\JWTAuth\\Middleware\\RefreshToken::class,\n```\n\n```\nphp artisan vendor:publish --provider=\"Tymon\\JWTAuth\\Providers\\JWTAuthServiceProvider\"\nphp artisan jwt:generate\n```\n\n### 實作\n\n**AuthenticateController**\n\n```php\npublic function authenticate(Request $request)\n{\n        // grab credentials from the request\n    $credentials = $request-\u003eonly('email', 'password');\n\n    try {\n        // attempt to verify the credentials and create a token for the user\n        if (! $token = JWTAuth::attempt($credentials)) {\n            return response()-\u003ejson(['error' =\u003e 'invalid_credentials'], 401);\n        }\n    } catch (JWTException $e) {\n        // something went wrong whilst attempting to encode the token\n        return response()-\u003ejson(['error' =\u003e 'could_not_create_token'], 500);\n    }\n\n    $user = User::where('email', $request-\u003eemail)-\u003efirst();\n    $token = JWTAuth::fromUser($user);\n\n    // all good so return the token\n    return response()-\u003ejson([\n        'token' =\u003e $token\n    ]);\n}\n```\n\n**route/api.php**\n\n```php\nRoute::post('/login', 'AuthenticateController@authenticate');\n\nRoute::middleware('jwt.auth')-\u003epost('/jwt/check', function (Request $request) {\n    return 'token is correct';\n});\n```\n\n**Http Header**\n\n```\nAuthorization: Bearer TOKEN_STRING\n```\n\n### 參考資料\n\n* [Installation · tymondesigns/jwt-auth Wiki · GitHub](https://github.com/tymondesigns/jwt-auth/wiki/Installation)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falincode%2Flaravel-jwt-sandbox","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falincode%2Flaravel-jwt-sandbox","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falincode%2Flaravel-jwt-sandbox/lists"}