Skip to content

Commit 10b0578

Browse files
committed
update
1 parent 96e8cdd commit 10b0578

File tree

9 files changed

+803
-194
lines changed

9 files changed

+803
-194
lines changed

docs/authentication.md

Lines changed: 207 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,49 @@ title: Authentication
44
permalink: /docs/authentication/
55
---
66

7-
In the "Auth" class you find 3 functions that you can use anywhere.
7+
The "Auth" class provides user authentication, registration, and password management with support for password hashing and two-factor authentication (TOTP).
88

99
## Login
1010

1111
```
12-
Auth::login($username,$password)
12+
Auth::login(string $username, string $password, string $totp = ''): array
1313
```
1414

15-
Call this function to authenticate a user, example:
15+
Authenticate a user with username, password, and optional TOTP code. Verifies the username and password, checks TOTP if configured, regenerates the session, and stores user data in the session.
16+
17+
Returns user data on success, empty array on failure.
18+
19+
Example:
1620

1721
```
18-
if (Auth::login($username, $password)) {
22+
$user = Auth::login($username, $password);
23+
if ($user) {
1924
Router::redirect("admin");
2025
} else {
2126
$error = "Username/password not valid";
2227
}
2328
```
2429

30+
For two-factor authentication:
31+
32+
```
33+
$user = Auth::login($username, $password, $totpCode);
34+
if ($user) {
35+
Router::redirect("admin");
36+
} else {
37+
$error = "Invalid credentials or TOTP code";
38+
}
39+
```
40+
2541
## Logout
2642

2743
```
28-
Auth::logout()
44+
Auth::logout(): bool
2945
```
3046

31-
Call this function to de-authenticate a user, example:
47+
Log out the current user. Removes user data from the session and regenerates the session ID for security. Always returns true.
48+
49+
Example:
3250

3351
```
3452
Auth::logout();
@@ -38,64 +56,183 @@ Router::redirect("login");
3856
## Register
3957

4058
```
41-
Auth::register($username,$password)
59+
Auth::register(string $username, string $password): int
4260
```
4361

44-
Call this function to register a new user, example:
62+
Register a new user with username and password. Hashes the password using PASSWORD_DEFAULT algorithm and stores the user record with the current timestamp.
63+
64+
Returns the ID of the newly created user.
65+
66+
Example:
4567

4668
```
47-
if (Auth::register($username, $password)) {
69+
$userId = Auth::register($username, $password);
70+
if ($userId) {
4871
Auth::login($username, $password);
4972
Router::redirect("admin");
5073
} else {
5174
$error = "User can not be registered";
5275
}
5376
```
5477

78+
## Update
79+
80+
```
81+
Auth::update(string $username, string $password): int
82+
```
83+
84+
Update a user's password. Hashes the new password using PASSWORD_DEFAULT algorithm and updates the user's record.
85+
86+
Returns the number of rows affected (typically 1 on success, 0 if user not found).
87+
88+
Example:
89+
90+
```
91+
$result = Auth::update($username, $newPassword);
92+
if ($result) {
93+
$message = "Password updated successfully";
94+
} else {
95+
$error = "User not found";
96+
}
97+
```
98+
99+
## Update TOTP Secret
100+
101+
```
102+
Auth::updateTotpSecret(string $username, string $secret): int
103+
```
104+
105+
Update a user's TOTP secret for two-factor authentication. Sets or updates the TOTP secret (base32-encoded).
106+
107+
Returns the number of rows affected (typically 1 on success, 0 if user not found).
108+
109+
Example:
110+
111+
```
112+
$secret = Totp::generateSecret();
113+
$result = Auth::updateTotpSecret($username, $secret);
114+
if ($result) {
115+
$message = "TOTP enabled successfully";
116+
}
117+
```
118+
119+
## Exists
120+
121+
```
122+
Auth::exists(string $username): bool
123+
```
124+
125+
Check if a user exists. Queries the database to determine if a user with the given username exists.
126+
127+
Returns true if user exists, false otherwise.
128+
129+
Example:
130+
131+
```
132+
if (Auth::exists($username)) {
133+
$error = "Username already taken";
134+
} else {
135+
Auth::register($username, $password);
136+
}
137+
```
138+
55139
# Passwordless
56140

57-
In the "NoPassAuth" class you find 4 functions that you can use anywhere.
141+
The "NoPassAuth" class provides passwordless user authentication using time-based tokens, with support for remember-me functionality and optional TOTP two-factor authentication.
58142

59143
## Token
60144

61145
```
62-
NoPassAuth::token($username)
146+
NoPassAuth::token(string $username): string
63147
```
64148

65-
Call this function to retrieve a login token, example:
149+
Generate a token for the given username. Creates a JWT token containing the username and IP address, using the user's password hash as the secret.
150+
151+
Returns the generated token, or empty string if user not found.
152+
153+
Example:
66154

67155
```
68156
$token = NoPassAuth::token($username);
69157
if ($token) {
70-
mail($username,'token',Router::getBaseUrl().'login/'.$token);
158+
mail($username, 'Login Token', Router::getBaseUrl() . 'login/' . $token);
159+
$message = "Login token sent to your email";
71160
} else {
72161
$error = "Username not valid";
73162
}
74163
```
75164

165+
## Remember
166+
167+
```
168+
NoPassAuth::remember(): bool
169+
```
170+
171+
Attempt to restore a user session from a remember-me cookie. Checks for a valid remember-me cookie, verifies the token, and restores the user session if valid.
172+
173+
Returns true if session was restored, false otherwise.
174+
175+
Example:
176+
177+
```
178+
// Typically called at application startup
179+
if (NoPassAuth::remember()) {
180+
// User session restored from cookie
181+
Router::redirect("dashboard");
182+
}
183+
```
184+
76185
## Login
77186

78187
```
79-
NoPassAuth::login($token)
188+
NoPassAuth::login(string $token, bool $rememberMe = false, ?string $totp = null): array
80189
```
81190

82-
Call this function to authenticate a user, example:
191+
Authenticate a user with a token and optional TOTP code. Verifies the JWT token signature and claims, checks TOTP if configured, regenerates the session, and stores user data in the session.
192+
193+
Returns user data on success, empty array on failure.
194+
195+
Example:
83196

84197
```
85-
if (NoPassAuth::login($token)) {
198+
$user = NoPassAuth::login($token);
199+
if ($user) {
86200
Router::redirect("admin");
87201
} else {
88202
$error = "Token not valid";
89203
}
90204
```
91205

206+
With remember-me functionality:
207+
208+
```
209+
$user = NoPassAuth::login($token, true);
210+
if ($user) {
211+
// User logged in and remember-me cookie set
212+
Router::redirect("admin");
213+
}
214+
```
215+
216+
With two-factor authentication:
217+
218+
```
219+
$user = NoPassAuth::login($token, false, $totpCode);
220+
if ($user) {
221+
Router::redirect("admin");
222+
} else {
223+
$error = "Invalid token or TOTP code";
224+
}
225+
```
226+
92227
## Logout
93228

94229
```
95-
NoPassAuth::logout()
230+
NoPassAuth::logout(): bool
96231
```
97232

98-
Call this function to de-authenticate a user, example:
233+
Log out the current user. Clears all session variables except debugger data, regenerates the session ID, and removes the remember-me cookie. Always returns true.
234+
235+
Example:
99236

100237
```
101238
NoPassAuth::logout();
@@ -105,16 +242,64 @@ Router::redirect("login");
105242
## Register
106243

107244
```
108-
NoPassAuth::register($username)
245+
NoPassAuth::register(string $username): int
109246
```
110247

111-
Call this function to register a new user, example:
248+
Register a new user with the given username. Creates a new user record with a random hashed password.
249+
250+
Returns the ID of the newly created user.
251+
252+
Example:
112253

113254
```
114-
if (NoPassAuth::register($username)) {
255+
$userId = NoPassAuth::register($username);
256+
if ($userId) {
115257
$token = NoPassAuth::token($username);
116-
mail($username,'token',Router::getBaseUrl().'login/'.$token);
258+
mail($username, 'Welcome', Router::getBaseUrl() . 'login/' . $token);
259+
$message = "Registration successful, check your email";
117260
} else {
118261
$error = "User can not be registered";
119262
}
263+
```
264+
265+
## Update
266+
267+
```
268+
NoPassAuth::update(string $username): int
269+
```
270+
271+
Update the password for an existing user. Generates a new random hashed password for the user.
272+
273+
Returns the number of affected rows.
274+
275+
Example:
276+
277+
```
278+
$result = NoPassAuth::update($username);
279+
if ($result) {
280+
$token = NoPassAuth::token($username);
281+
mail($username, 'Password Reset', Router::getBaseUrl() . 'login/' . $token);
282+
$message = "Password reset, new login token sent";
283+
}
284+
```
285+
286+
## Update TOTP Secret
287+
288+
```
289+
NoPassAuth::updateTotpSecret(string $username, string $secret): int
290+
```
291+
292+
Update the TOTP secret for a user to enable two-factor authentication.
293+
294+
Returns the number of affected rows.
295+
296+
Example:
297+
298+
```
299+
$secret = Totp::generateSecret();
300+
$result = NoPassAuth::updateTotpSecret($username, $secret);
301+
if ($result) {
302+
$qrCode = Totp::getQrCodeUrl($username, $secret);
303+
$message = "TOTP enabled, scan QR code: " . $qrCode;
304+
}
120305
```

0 commit comments

Comments
 (0)