Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alincode/laravel-jwt-sandbox
https://github.com/alincode/laravel-jwt-sandbox
Last synced: 13 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/alincode/laravel-jwt-sandbox
- Owner: alincode
- Created: 2017-09-24T07:56:33.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-09-24T08:02:35.000Z (over 7 years ago)
- Last Synced: 2024-11-06T07:27:14.093Z (2 months ago)
- Language: PHP
- Size: 165 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Laravel with JWT
### 安裝
```
composer create-project --prefer-dist laravel/laravel laravel-jwt-sandbox
composer require tymon/jwt-auth
```### 配置
**config/app.php**
```
// aliases'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class,
```**app/HTTP/Kernel.php**
```
// routeMiddleware'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class,
'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class,
``````
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider"
php artisan jwt:generate
```### 實作
**AuthenticateController**
```php
public function authenticate(Request $request)
{
// grab credentials from the request
$credentials = $request->only('email', 'password');try {
// attempt to verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return response()->json(['error' => 'could_not_create_token'], 500);
}$user = User::where('email', $request->email)->first();
$token = JWTAuth::fromUser($user);// all good so return the token
return response()->json([
'token' => $token
]);
}
```**route/api.php**
```php
Route::post('/login', 'AuthenticateController@authenticate');Route::middleware('jwt.auth')->post('/jwt/check', function (Request $request) {
return 'token is correct';
});
```**Http Header**
```
Authorization: Bearer TOKEN_STRING
```### 參考資料
* [Installation · tymondesigns/jwt-auth Wiki · GitHub](https://github.com/tymondesigns/jwt-auth/wiki/Installation)