create-prisma-php-app 1.16.9 → 1.16.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,12 +27,12 @@ class Auth
27
27
  }
28
28
 
29
29
  /**
30
- * Authenticates a user and generates a JWT (JSON Web Token) based on the specified user role
30
+ * Authenticates a user and generates a JWT (JSON Web Token) based on the specified user data
31
31
  * and token validity duration. The method first checks if the secret key is set, calculates
32
32
  * the token's expiration time, sets the necessary payload, and encodes it into a JWT.
33
33
  * If possible (HTTP headers not yet sent), it also sets cookies with the JWT for client-side storage.
34
34
  *
35
- * @param mixed $role A role identifier which can be a simple string or an instance of AuthRole.
35
+ * @param mixed $data User data which can be a simple string or an instance of AuthRole.
36
36
  * If an instance of AuthRole is provided, its `value` property will be used as the role in the token.
37
37
  * @param string|null $tokenValidity Optional parameter specifying the duration the token is valid for (e.g., '10m', '1h').
38
38
  * If null, the default validity period set in the class property is used.
@@ -53,7 +53,7 @@ class Auth
53
53
  * echo "Error: " . $e->getMessage();
54
54
  * }
55
55
  */
56
- public function authenticate($role, string $tokenValidity = null): string
56
+ public function authenticate($data, string $tokenValidity = null): string
57
57
  {
58
58
  if (!$this->secretKey) {
59
59
  throw new \InvalidArgumentException("Secret key is required for authentication.");
@@ -61,12 +61,12 @@ class Auth
61
61
 
62
62
  $expirationTime = $this->calculateExpirationTime($tokenValidity ?? $this->defaultTokenValidity);
63
63
 
64
- if ($role instanceof AuthRole) {
65
- $role = $role->value;
64
+ if ($data instanceof AuthRole) {
65
+ $data = $data->value;
66
66
  }
67
67
 
68
68
  $payload = [
69
- self::PAYLOAD_NAME => $role,
69
+ self::PAYLOAD_NAME => $data,
70
70
  'exp' => $expirationTime,
71
71
  ];
72
72
 
@@ -83,6 +83,12 @@ class Auth
83
83
  return $jwt;
84
84
  }
85
85
 
86
+ /**
87
+ * Checks if the user is authenticated based on the presence of the payload in the session.
88
+ * Returns true if the user is authenticated, false otherwise.
89
+ *
90
+ * @return bool Returns true if the user is authenticated, false otherwise.
91
+ */
86
92
  public function isAuthenticated(): bool
87
93
  {
88
94
  return isset($_SESSION[self::PAYLOAD]);
@@ -119,6 +125,13 @@ class Auth
119
125
  throw new \InvalidArgumentException("Invalid duration format: {$duration}");
120
126
  }
121
127
 
128
+ /**
129
+ * Verifies the JWT token and returns the decoded payload if the token is valid.
130
+ * If the token is invalid, an exception is thrown.
131
+ *
132
+ * @param string $jwt The JWT token to verify.
133
+ * @return object Returns the decoded payload if the token is valid.
134
+ */
122
135
  public function verifyToken(string $jwt)
123
136
  {
124
137
  try {
@@ -128,6 +141,21 @@ class Auth
128
141
  }
129
142
  }
130
143
 
144
+ /**
145
+ * Refreshes the JWT token by updating the expiration time and encoding the new payload into a JWT.
146
+ * If the token validity duration is not specified, the default token validity period is used.
147
+ * If possible (HTTP headers not yet sent), it also sets cookies with the new JWT for client-side storage.
148
+ *
149
+ * @param string $jwt The JWT token to refresh.
150
+ * @param string|null $tokenValidity Optional parameter specifying the duration the token is valid for (e.g., '10m', '1h').
151
+ * If null, the default validity period set in the class property is used.
152
+ * The format should be a number followed by a time unit ('s' for seconds, 'm' for minutes,
153
+ * 'h' for hours, 'd' for days), and this is parsed to calculate the exact expiration time.
154
+ *
155
+ * @return string Returns the refreshed JWT as a string.
156
+ *
157
+ * @throws InvalidArgumentException Thrown if the token is invalid.
158
+ */
131
159
  public function refreshToken(string $jwt, string $tokenValidity = null): string
132
160
  {
133
161
  $decodedToken = $this->verifyToken($jwt);
@@ -162,6 +190,18 @@ class Auth
162
190
  }
163
191
  }
164
192
 
193
+ /**
194
+ * Logs out the user by unsetting the session payload and deleting the authentication cookie.
195
+ * If a redirect URL is provided, the user is redirected to that URL after logging out.
196
+ *
197
+ * @param string|null $redirect Optional parameter specifying the URL to redirect to after logging out.
198
+ *
199
+ * Example:
200
+ * $auth = new Authentication();
201
+ * $auth->logout('/login');
202
+ *
203
+ * @return void
204
+ */
165
205
  public function logout(string $redirect = null)
166
206
  {
167
207
  if (isset($_COOKIE[self::COOKIE_NAME])) {
@@ -178,6 +218,12 @@ class Auth
178
218
  }
179
219
  }
180
220
 
221
+ /**
222
+ * Returns the role of the authenticated user based on the payload stored in the session.
223
+ * If the user is not authenticated, null is returned.
224
+ *
225
+ * @return string|null Returns the role of the authenticated user or null if the user is not authenticated.
226
+ */
181
227
  public function getPayload()
182
228
  {
183
229
  if (isset($_SESSION[self::PAYLOAD])) {
@@ -223,6 +269,18 @@ class Auth
223
269
  return null;
224
270
  }
225
271
 
272
+ /**
273
+ * Authenticates a user using OAuth providers such as Google or GitHub.
274
+ * The method first checks if the request is a GET request and if the route is a sign-in route.
275
+ * It then processes the authentication code received from the provider and retrieves the user's data.
276
+ * The user data is saved to the database, and the user is authenticated using the authenticate method.
277
+ *
278
+ * @param mixed ...$providers An array of provider objects such as GoogleProvider or GithubProvider.
279
+ *
280
+ * Example:
281
+ * $auth = new Auth();
282
+ * $auth->authProviders(new GoogleProvider('client_id', 'client_secret', 'redirect_uri'));
283
+ */
226
284
  public function authProviders(...$providers)
227
285
  {
228
286
  global $isGet, $dynamicRouteParams;
@@ -7,16 +7,10 @@
7
7
  <meta name="description" content="<?php echo htmlspecialchars($metadata['description']); ?>">
8
8
  <title><?php echo htmlspecialchars($metadata['title']); ?></title>
9
9
  <link rel="shortcut icon" href="<?php echo $baseUrl; ?>favicon.ico" type="image/x-icon">
10
- <script>
11
- var baseUrl = '<?php echo $baseUrl; ?>';
12
- var pathname = '<?php echo $pathname; ?>';
13
- </script>
14
10
  </head>
15
11
 
16
12
  <body>
17
- <!-- Additional HTML content can go here. -->
18
13
  <?php echo $content; ?>
19
- <!-- Additional HTML content can go here. -->
20
14
  </body>
21
15
 
22
16
  </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-prisma-php-app",
3
- "version": "1.16.9",
3
+ "version": "1.16.11",
4
4
  "description": "Prisma-PHP: A Revolutionary Library Bridging PHP with Prisma ORM",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",