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
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).
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.
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:
32
50
33
51
```
34
52
Auth::logout();
@@ -38,64 +56,183 @@ Router::redirect("login");
38
56
## Register
39
57
40
58
```
41
-
Auth::register($username,$password)
59
+
Auth::register(string $username, string $password): int
42
60
```
43
61
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:
45
67
46
68
```
47
-
if (Auth::register($username, $password)) {
69
+
$userId = Auth::register($username, $password);
70
+
if ($userId) {
48
71
Auth::login($username, $password);
49
72
Router::redirect("admin");
50
73
} else {
51
74
$error = "User can not be registered";
52
75
}
53
76
```
54
77
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).
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
+
55
139
# Passwordless
56
140
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.
58
142
59
143
## Token
60
144
61
145
```
62
-
NoPassAuth::token($username)
146
+
NoPassAuth::token(string $username): string
63
147
```
64
148
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.
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.
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.
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:
99
236
100
237
```
101
238
NoPassAuth::logout();
@@ -105,16 +242,64 @@ Router::redirect("login");
105
242
## Register
106
243
107
244
```
108
-
NoPassAuth::register($username)
245
+
NoPassAuth::register(string $username): int
109
246
```
110
247
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.
0 commit comments