You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A simple framework-agnostic JSON Web Token authentication solution.
6
6
7
-
## Installation
7
+
## 📄 License
8
+
9
+
Licensed under the [MIT license](https://opensource.org/licenses/MIT) and is free for private or commercial projects.
10
+
11
+
## 📥 Installation
8
12
9
13
```bash
10
14
composer require andrewdyer/jwt-auth
11
15
```
12
16
13
-
## Getting Started
17
+
## 🚀 Getting Started
14
18
15
-
### Define the JWT Subject
19
+
### 1. Define the JWT Subject
16
20
17
21
Create a class (e.g., `User`) that implements the `JWTSubject` interface. This class must provide a method `getJWTIdentifier` to return the user’s unique identifier.
18
22
@@ -32,7 +36,7 @@ class User implements JWTSubject
32
36
33
37
> **Note:** This example is simplified for demonstration purposes. In a real-world application, you would typically use a proper user model, such as one provided by your framework. Ensure the `getJWTIdentifier` method returns a unique user identifier appropriate for your system.
34
38
35
-
### Create an Authentication Provider
39
+
### 2. Create an Authentication Provider
36
40
37
41
Create an authentication provider class that implements `AuthProviderInterface`. This class will handle credential validation and user retrieval by ID.
38
42
@@ -66,7 +70,7 @@ class AuthProvider implements AuthProviderInterface
66
70
67
71
> **Note:** This example uses hardcoded credentials for demonstration purposes. In a real-world application, you should validate credentials securely by checking against a database and using hashed passwords (e.g., via libraries like `bcrypt` or `password_hash`). Ensure you follow best practices for secure authentication.
68
72
69
-
### Create a JWT Provider
73
+
### 3. Create a JWT Provider
70
74
71
75
Create a JWT provider class that implements `JWTProviderInterface`. This class should handle encoding and decoding JWT tokens.
72
76
@@ -91,7 +95,7 @@ class JWTProvider implements JWTProviderInterface
91
95
92
96
> **Note:** This examples used `base64_encode` and `base64_decode` for simplicity. For real-world usage, consider using a proper JWT library such as [firebase/php-jwt](https://github.com/firebase/php-jwt) for better security.
93
97
94
-
### Generate JWT Claims
98
+
### 4. Generate JWT Claims
95
99
96
100
The `ClaimsFactory` class helps create a JWT claims instance. The `build` method accepts an array of claims and returns an instance of `ClaimsInterface`.
> **Note:** This example uses hardcoded Unix timestamps for demonstration purposes. Consider using libraries like [nesbot/carbon](https://github.com/briannesbitt/carbon) or PHP's native `DateTime` class to generate timestamps dynamically. This helps improve readability and ensures accurate date handling.
112
116
113
-
### Initialize the JWT Authenticator
117
+
### 5. Initialize the JWT Authenticator
114
118
115
119
Create a new instance of the `JWTAuth` class. This requires an instance of `AuthProviderInterface`, `JWTProviderInterface`, and `ClaimsInterface`.
116
120
@@ -126,7 +130,7 @@ $jwtProvider = new JWTProvider();
126
130
$jwtAuth = new JWTAuth($authProvider, $jwtProvider, $claims);
127
131
```
128
132
129
-
## Usage
133
+
## 📖 Usage
130
134
131
135
### Attempt Authentication
132
136
@@ -154,7 +158,3 @@ if ($subject) {
154
158
echo "Invalid token";
155
159
}
156
160
```
157
-
158
-
## License
159
-
160
-
Licensed under MIT. Totally free for private or commercial projects.
0 commit comments