Skip to content

Commit 5511415

Browse files
committed
自原專案抽離
0 parents  commit 5511415

16 files changed

+628
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[*.{yml,yaml}]
15+
indent_size = 2

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor
2+
.env
3+
.php_cs.cache
4+
.phpunit.result.cache

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# A Laravel package example

composer.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "a2workspace/laravel-jwt",
3+
"description": "提供 Laravel JWT 認證功能",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Shishamou",
8+
"email": "shishatw225@gmail.com"
9+
}
10+
],
11+
"require": {
12+
"php": "^7.4|^8.0",
13+
"tymon/jwt-auth": "^1.0"
14+
},
15+
"require-dev": {
16+
"orchestra/testbench": "4.x"
17+
},
18+
"autoload": {
19+
"psr-4": {
20+
"A2Workspace\\LaravelJwt\\": "src/"
21+
}
22+
},
23+
"autoload-dev": {
24+
"psr-4": {
25+
"Tests\\": "tests/"
26+
}
27+
},
28+
"extra": {
29+
"laravel": {
30+
"providers": [
31+
"A2Workspace\\LaravelJwt\\ServiceProvider"
32+
]
33+
}
34+
}
35+
}

phpunit.xml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
bootstrap="vendor/autoload.php"
5+
backupGlobals="false"
6+
backupStaticAttributes="false"
7+
colors="true"
8+
verbose="true"
9+
convertErrorsToExceptions="true"
10+
convertNoticesToExceptions="true"
11+
convertWarningsToExceptions="true"
12+
processIsolation="false"
13+
stopOnFailure="false"
14+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
15+
>
16+
<testsuites>
17+
<testsuite name="Unit">
18+
<directory suffix="Test.php">./tests/Unit</directory>
19+
</testsuite>
20+
<testsuite name="Feature">
21+
<directory suffix="Test.php">./tests/Feature</directory>
22+
</testsuite>
23+
</testsuites>
24+
<php>
25+
<env name="DB_CONNECTION" value="testing"/>
26+
<env name="APP_KEY" value="base64:2fl+Ktvkfl+Fuz4Qp/A75G2RTiWVA/ZoKZvp6fiiM10="/>
27+
<env name="DB_CONNECTION" value="sqlite"/>
28+
<env name="DB_DATABASE" value=":memory:"/>
29+
</php>
30+
</phpunit>

src/AuthenticatesUsers.php

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
namespace A2Workspace\LaravelJwt;
4+
5+
use Tymon\JWTAuth\JWTGuard;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Support\Facades\Auth;
8+
use Tymon\JWTAuth\Exceptions\JWTException;
9+
10+
trait AuthenticatesUsers
11+
{
12+
/**
13+
* 處理登入請求
14+
*
15+
* @param \Illuminate\Http\Request $request
16+
* @return \Illuminate\Http\Response
17+
*/
18+
public function login(Request $request)
19+
{
20+
$credentials = $this->credentials($request);
21+
22+
if ($token = $this->guard()->attempt($credentials)) {
23+
return $this->respondWithToken($token);
24+
}
25+
26+
return response()->json(['error' => 'Unauthorized'], 401);
27+
}
28+
29+
/**
30+
* 自 Request 取得驗證所需的欄位
31+
*
32+
* @param \Illuminate\Http\Request $request
33+
* @return array
34+
*/
35+
protected function credentials(Request $request)
36+
{
37+
return $request->only($this->username(), 'password');
38+
}
39+
40+
/**
41+
* 回傳驗證守衛。
42+
*
43+
* @return \Tymon\JWTAuth\JWTGuard
44+
*/
45+
protected function guard(): JWTGuard
46+
{
47+
return Auth::guard('api');
48+
}
49+
50+
/**
51+
* 取得驗證使用者名稱的欄位
52+
*
53+
* @return string
54+
*/
55+
protected function username()
56+
{
57+
return 'username';
58+
}
59+
60+
/**
61+
* Get the token array structure.
62+
*
63+
* @param string $token
64+
*
65+
* @return \Illuminate\Http\JsonResponse
66+
*/
67+
protected function respondWithToken($token)
68+
{
69+
return response()->json([
70+
'access_token' => $token,
71+
'token_type' => 'bearer',
72+
'expires_in' => $this->guard()->factory()->getTTL() * 60
73+
]);
74+
}
75+
76+
/**
77+
* 處理登出請求
78+
*
79+
* @param \Illuminate\Http\Request $request
80+
* @return \Illuminate\Http\Response
81+
*/
82+
public function logout(Request $request)
83+
{
84+
$this->guard()->logout();
85+
86+
return response()->json(['message' => 'Successfully logged out']);
87+
}
88+
89+
/**
90+
* 刷新 token
91+
*
92+
* @return \Illuminate\Http\JsonResponse
93+
*/
94+
public function refresh()
95+
{
96+
try {
97+
return $this->respondWithToken($this->guard()->refresh());
98+
} catch (JWTException $e) {
99+
return response()->json(['error' => 'Invalid Access Token'], 401);
100+
}
101+
}
102+
103+
/**
104+
* 回傳當前使用者資訊
105+
*
106+
* @param \Illuminate\Http\Request $request
107+
* @return \Illuminate\Http\Response 回傳使用者資訊
108+
*/
109+
public function me(Request $request)
110+
{
111+
if ($this->guard()->check()) {
112+
return response()->json($this->guard()->user());
113+
} else {
114+
return response()->json(['error' => 'Invalid Access Token'], 401);
115+
}
116+
}
117+
}

src/Commands/InstallCommand.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace A2Workspace\LaravelJwt\Commands;
4+
5+
use Illuminate\Console\Command;
6+
7+
class InstallCommand extends Command
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'laravel-jwt:install';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = '安裝設置 laravel-jwt';
22+
23+
/**
24+
* Execute the console command.
25+
*/
26+
public function handle()
27+
{
28+
$this->line('安裝 jwt-auth ...');
29+
30+
$this->call('vendor:publish', [
31+
'--provider' => 'Tymon\JWTAuth\Providers\LaravelServiceProvider',
32+
]);
33+
34+
$this->call('jwt:secret');
35+
}
36+
}

src/Controllers/AuthController.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace A2Workspace\LaravelJwt\Controllers;
4+
5+
use Illuminate\Routing\Controller;
6+
use A2Workspace\LaravelJwt\AuthenticatesUsers;
7+
8+
class AuthController extends Controller
9+
{
10+
use AuthenticatesUsers;
11+
}

src/HasApiTokens.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace A2Workspace\LaravelJwt;
4+
5+
use Tymon\JWTAuth\Facades\JWTAuth;
6+
7+
trait HasApiTokens
8+
{
9+
/**
10+
* Get the identifier that will be stored in the subject claim of the JWT.
11+
*
12+
* @return mixed
13+
*/
14+
public function getJWTIdentifier()
15+
{
16+
return $this->getKey();
17+
}
18+
19+
/**
20+
* Return a key value array, containing any custom claims to be added to the JWT.
21+
*
22+
* @return array
23+
*/
24+
public function getJWTCustomClaims()
25+
{
26+
return [];
27+
}
28+
29+
/**
30+
* Create a new token for current user.
31+
*
32+
* @return string
33+
*/
34+
public function createToken(): string
35+
{
36+
return JWTAuth::fromUser($this);
37+
}
38+
}

src/LaravelJwt.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace A2Workspace\LaravelJwt;
4+
5+
use Illuminate\Support\Facades\Route;
6+
use Illuminate\Contracts\Routing\Registrar as Router;
7+
8+
class LaravelJwt
9+
{
10+
/**
11+
* Binds the routes.
12+
*
13+
* @param array $options
14+
* @return void
15+
*/
16+
public static function routes(array $options = [])
17+
{
18+
$defaultOptions = [
19+
'prefix' => '/api/auth',
20+
'namespace' => '\A2Workspace\LaravelJwt\Controllers',
21+
'as' => 'jwt.',
22+
];
23+
24+
$options = array_merge($defaultOptions, $options);
25+
26+
Route::group($options, function ($router) {
27+
$router->post('/login', 'AuthController@login')->name('login');
28+
$router->post('/logout', 'AuthController@logout')->name('logout');
29+
$router->post('/refresh', 'AuthController@refresh')->name('refresh');
30+
$router->get('/user', 'AuthController@me')->name('user');
31+
});
32+
}
33+
}

0 commit comments

Comments
 (0)