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
|
|
57
|
-
*
|
|
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
|
|
62
|
-
*
|
|
63
|
-
* @param string
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
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
|
|