Skip to content

Commit 2ae87fc

Browse files
committed
Refactor login functionality: Remove Remember Me feature and update tests for deep link support
1 parent 8224363 commit 2ae87fc

2 files changed

Lines changed: 105 additions & 296 deletions

File tree

front/index.php

Lines changed: 33 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
session_start();
1515

16-
const COOKIE_NAME = 'NetAlertX_SaveLogin';
1716
const DEFAULT_REDIRECT = '/devices.php';
1817

1918
/* =====================================================
@@ -42,9 +41,39 @@ function validate_local_path(?string $encoded): string {
4241
return $decoded;
4342
}
4443

44+
function extract_hash_from_path(string $path): array {
45+
/*
46+
Split a path into path and hash components.
47+
48+
For deep links encoded in the 'next' parameter like /devices.php#device-123,
49+
extract the hash fragment so it can be properly included in the redirect.
50+
51+
Args:
52+
path: Full path potentially with hash (e.g., "/devices.php#device-123")
53+
54+
Returns:
55+
Array with keys 'path' (without hash) and 'hash' (with # prefix, or empty string)
56+
*/
57+
$parts = explode('#', $path, 2);
58+
return [
59+
'path' => $parts[0],
60+
'hash' => !empty($parts[1]) ? '#' . $parts[1] : ''
61+
];
62+
}
63+
4564
function append_hash(string $url): string {
65+
// First check if the URL already has a hash from the deep link
66+
$parts = extract_hash_from_path($url);
67+
if (!empty($parts['hash'])) {
68+
return $parts['path'] . $parts['hash'];
69+
}
70+
71+
// Fall back to POST url_hash (for browser-captured hashes)
4672
if (!empty($_POST['url_hash'])) {
47-
return $url . preg_replace('/[^#a-zA-Z0-9_\-]/', '', $_POST['url_hash']);
73+
$sanitized = preg_replace('/[^#a-zA-Z0-9_\-]/', '', $_POST['url_hash']);
74+
if (str_starts_with($sanitized, '#')) {
75+
return $url . $sanitized;
76+
}
4877
}
4978
return $url;
5079
}
@@ -134,14 +163,6 @@ function call_api(string $endpoint, array $data = []): ?array {
134163
function logout_user(): void {
135164
$_SESSION = [];
136165
session_destroy();
137-
138-
setcookie(COOKIE_NAME,'',[
139-
'expires'=>time()-3600,
140-
'path'=>'/',
141-
'secure'=>is_https_request(),
142-
'httponly'=>true,
143-
'samesite'=>'Strict'
144-
]);
145166
}
146167

147168
/* =====================================================
@@ -173,28 +194,7 @@ function logout_user(): void {
173194

174195
login_user();
175196

176-
// Handle "Remember Me" if checked
177-
if (!empty($_POST['PWRemember'])) {
178-
// Generate random token (64-byte hex = 128 chars, use 64 chars)
179-
$token = bin2hex(random_bytes(32));
180-
181-
// Call API to save token hash to Parameters table
182-
$save_response = call_api('/auth/remember-me/save', [
183-
'token' => $token
184-
]);
185-
186-
// If API call successful, set persistent cookie
187-
if ($save_response && isset($save_response['success']) && $save_response['success']) {
188-
setcookie(COOKIE_NAME, $token, [
189-
'expires' => time() + 604800,
190-
'path' => '/',
191-
'secure' => is_https_request(),
192-
'httponly' => true,
193-
'samesite' => 'Strict'
194-
]);
195-
}
196-
}
197-
197+
// Redirect to target page, preserving deep link hash if present
198198
safe_redirect(append_hash($redirectTo));
199199
}
200200
}
@@ -203,20 +203,6 @@ function logout_user(): void {
203203
Remember Me Validation
204204
===================================================== */
205205

206-
if (!is_authenticated() && !empty($_COOKIE[COOKIE_NAME])) {
207-
208-
// Call API to validate token against stored hash
209-
$validate_response = call_api('/auth/validate-remember', [
210-
'token' => $_COOKIE[COOKIE_NAME]
211-
]);
212-
213-
// If API returns valid token, authenticate and redirect
214-
if ($validate_response && isset($validate_response['valid']) && $validate_response['valid'] === true) {
215-
login_user();
216-
safe_redirect(append_hash($redirectTo));
217-
}
218-
}
219-
220206
/* =====================================================
221207
Already Logged In
222208
===================================================== */
@@ -289,18 +275,7 @@ function logout_user(): void {
289275
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
290276
</div>
291277
<div class="row">
292-
<div class="col-xs-8">
293-
<div class="checkbox icheck">
294-
<label>
295-
<input type="checkbox" name="PWRemember">
296-
<div style="margin-left: 10px; display: inline-block; vertical-align: top;">
297-
<?= lang('Login_Remember');?><br><span style="font-size: smaller"><?= lang('Login_Remember_small');?></span>
298-
</div>
299-
</label>
300-
</div>
301-
</div>
302-
<!-- /.col -->
303-
<div class="col-xs-4" style="padding-top: 10px;">
278+
<div class="col-xs-12">
304279
<button type="submit" class="btn btn-primary btn-block btn-flat"><?= lang('Login_Submit');?></button>
305280
</div>
306281
<!-- /.col -->

0 commit comments

Comments
 (0)