create-prisma-php-app 4.0.16 → 4.0.18

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.
@@ -53,33 +53,20 @@ class Auth
53
53
  }
54
54
 
55
55
  /**
56
- * Authenticates a user and generates a JWT (JSON Web Token) based on the specified user data
57
- * and token validity duration. The method first checks if the secret key is set, calculates
58
- * the token's expiration time, sets the necessary payload, and encodes it into a JWT.
59
- * If possible (HTTP headers not yet sent), it also sets cookies with the JWT for client-side storage.
56
+ * Authenticates a user and generates a JWT.
57
+ * Optionally redirects the user to a default or custom URL.
60
58
  *
61
- * @param mixed $data User data which can be a simple string or an instance of AuthRole.
62
- * If an instance of AuthRole is provided, its `value` property will be used as the role in the token.
63
- * @param string|null $tokenValidity Optional parameter specifying the duration the token is valid for (e.g., '10m', '1h').
64
- * If null, the default validity period set in the class property is used, which is 1 hour.
65
- * The format should be a number followed by a time unit ('s' for seconds, 'm' for minutes,
66
- * 'h' for hours, 'd' for days), and this is parsed to calculate the exact expiration time.
59
+ * @param mixed $data User data (string or AuthRole).
60
+ * @param string|null $tokenValidity Duration token is valid for (e.g., '1h'). Default is '1h'.
61
+ * @param bool|string $redirect
62
+ * - If `false` (default): No redirect occurs; returns the JWT.
63
+ * - If `true`: Redirects to `AuthConfig::DEFAULT_SIGNIN_REDIRECT`.
64
+ * - If `string`: Redirects to the specified URL (e.g., '/dashboard').
67
65
  *
68
66
  * @return string Returns the encoded JWT as a string.
69
- *
70
- * @throws InvalidArgumentException Thrown if the secret key is not set or if the duration format is invalid.
71
- *
72
- * Example:
73
- * $auth = Auth::getInstance();
74
- * $auth->setSecretKey('your_secret_key');
75
- * try {
76
- * $jwt = $auth->signIn('Admin', '1h');
77
- * echo "JWT: " . $jwt;
78
- * } catch (InvalidArgumentException $e) {
79
- * echo "Error: " . $e->getMessage();
80
- * }
67
+ * @throws InvalidArgumentException
81
68
  */
82
- public function signIn($data, ?string $tokenValidity = null): string
69
+ public function signIn($data, ?string $tokenValidity = null, bool|string $redirect = false): string
83
70
  {
84
71
  if (!$this->secretKey) {
85
72
  throw new InvalidArgumentException("Secret key is required for authentication.");
@@ -106,6 +93,12 @@ class Auth
106
93
  $this->setCookies($jwt, $expirationTime);
107
94
  }
108
95
 
96
+ if ($redirect === true) {
97
+ Request::redirect(AuthConfig::DEFAULT_SIGNIN_REDIRECT);
98
+ } elseif (is_string($redirect) && !empty($redirect)) {
99
+ Request::redirect($redirect);
100
+ }
101
+
109
102
  return $jwt;
110
103
  }
111
104
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-prisma-php-app",
3
- "version": "4.0.16",
3
+ "version": "4.0.18",
4
4
  "description": "Prisma-PHP: A Revolutionary Library Bridging PHP with Prisma ORM",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",