-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai-auth.js
More file actions
113 lines (93 loc) · 3.58 KB
/
Copy pathai-auth.js
File metadata and controls
113 lines (93 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// ai-auth.js
let isAuthenticated = false;
let currentResolver = null; // Stores the promise resolver function
/**
* Attaches event listeners to the AI Auth Modal buttons.
*/
function setupAiAuthModalListeners() {
const modal = document.getElementById('aiAuthModal');
const input = document.getElementById('aiPasswordInput');
const confirmBtn = document.getElementById('aiAuthConfirm');
const cancelBtn = document.getElementById('aiAuthCancel');
if (!modal || !input || !confirmBtn || !cancelBtn) return;
// Handle Enter keypress for quick submission
input.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
handleUnlockAttempt();
}
});
confirmBtn.addEventListener('click', handleUnlockAttempt);
cancelBtn.addEventListener('click', () => {
modal.style.display = 'none';
if (currentResolver) {
currentResolver(false); // Resolve with failure on cancel
}
currentResolver = null;
});
}
/**
* Handles the user's attempt to enter the password.
*/
async function handleUnlockAttempt() {
const modal = document.getElementById('aiAuthModal');
const input = document.getElementById('aiPasswordInput');
const password = input.value.trim();
// Reset descriptive message to default
const message = modal.querySelector('.modal-description');
try {
const res = await fetch('/.netlify/functions/verify-ai-password', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ password })
});
if (!res.ok) {
throw new Error('Invalid password');
}
isAuthenticated = true;
if (window.customAlert) {
window.customAlert("Success!", "AI Generator Unlocked for this session!");
} else {
alert("AI Generator Unlocked for this session!");
}
modal.style.display = 'none';
if (currentResolver) {
currentResolver(true); // Resolve with success
}
} catch (_err) {
input.value = '';
input.focus();
// Indicate failure visually
if (message) {
message.innerHTML =
'Incorrect password. Try again! <span style="color:' + window.getComputedStyle(document.body).getPropertyValue('--danger') + ';">(Hint: Check the case!)</span>';
}
}
}
/**
* Checks if the user has authenticated in the current session.
* If not, shows the stylish modal prompt and waits for input.
* @returns {Promise<boolean>} Resolves to true if authentication succeeds or is already active.
*/
export function authenticateAiAccess() {
if (isAuthenticated) {
return Promise.resolve(true);
}
const modal = document.getElementById('aiAuthModal');
const input = document.getElementById('aiPasswordInput');
const originalMessage = modal.querySelector('.modal-description');
if (!modal || !input) {
// Fallback or error state
return Promise.resolve(false);
}
// Reset modal state
input.value = '';
originalMessage.textContent = "Please enter the one-time password to unlock AI generation features for this session.";
modal.style.display = 'flex';
input.focus();
// Return a Promise that resolves when the user clicks 'Unlock' or 'Cancel'
return new Promise((resolve) => {
currentResolver = resolve;
});
}
// Global initialization to attach listeners once the DOM is ready
document.addEventListener("DOMContentLoaded", setupAiAuthModalListeners);