Skip to content

Commit c78d8a5

Browse files
Epic2 user auth (#1766)
2 parents 10bcfcd + 1757434 commit c78d8a5

File tree

7 files changed

+258
-66
lines changed

7 files changed

+258
-66
lines changed

home/views.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class ServiceWorkerView(TemplateView):
2121
def get_manifest(request):
2222
language = translation.get_language()
2323
manifest = get_object_or_404(ManifestSettings, language=language)
24+
print(manifest.icon_96_96.title, "+++++++++++++++++++++++")
2425
response = {
2526
"name": manifest.name,
2627
"short_name": manifest.short_name,
@@ -34,17 +35,17 @@ def get_manifest(request):
3435
"icons": [
3536
{
3637
"src": f"{manifest.icon_96_96.file.url}",
37-
"type": f"image/{manifest.icon_96_96.title.split('.')[1]}",
38+
"type": f"image/{manifest.icon_96_96.file.name.split('.')[-1]}",
3839
"sizes": f"{manifest.icon_96_96.height}x{manifest.icon_96_96.width}",
3940
},
4041
{
4142
"src": f"{manifest.icon_512_512.file.url}",
42-
"type": f"image/{manifest.icon_512_512.title.split('.')[1]}",
43+
"type": f"image/{manifest.icon_512_512.file.name.split('.')[-1]}",
4344
"sizes": f"{manifest.icon_512_512.height}x{manifest.icon_512_512.width}",
4445
},
4546
{
4647
"src": f"{manifest.icon_192_192.file.url}",
47-
"type": f"image/{manifest.icon_192_192.title.split('.')[1]}",
48+
"type": f"image/{manifest.icon_192_192.file.name.split('.')[-1]}",
4849
"sizes": f"{manifest.icon_192_192.height}x{manifest.icon_192_192.width}",
4950
"purpose": "any maskable",
5051
},

iogt/settings/base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -592,13 +592,15 @@
592592
# Azure AD B2C setup ends
593593
print("CURRENT_DOMAIN", CURRENT_DOMAIN)
594594
print("AZURE_AD_TENANT_ID", AZURE_AD_TENANT_ID)
595-
595+
print("AZURE_CLIENT_ID", os.getenv('AZURE_AD_CLIENT_ID'))
596596
# Mailjet setup for sending emails
597597
MAILJET_API_KEY = os.getenv('MAILJET_API_KEY')
598598
MAILJET_API_SECRET = os.getenv('MAILJET_API_SECRET')
599599
MAILJET_FROM_EMAIL = os.getenv('MAILJET_FROM_EMAIL')
600600
MAILJET_FROM_NAME = os.getenv('MAILJET_FROM_NAME')
601601

602+
print("MAILJET_API_KEY", MAILJET_API_KEY)
603+
602604
# Secure session and CSRF cookies
603605
SESSION_COOKIE_SECURE = True
604606
SESSION_COOKIE_HTTPONLY = True

iogt/static/js/idb.js

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// idb.js - IndexedDB Helper
2+
const DB_NAME = "offline-forms";
3+
const STORE_NAME = "requests";
4+
5+
function openDB() {
6+
return new Promise((resolve, reject) => {
7+
const request = indexedDB.open(DB_NAME, 1);
8+
9+
request.onupgradeneeded = event => {
10+
const db = event.target.result;
11+
if (!db.objectStoreNames.contains(STORE_NAME)) {
12+
console.log("🔧 Creating object store:", STORE_NAME);
13+
db.createObjectStore(STORE_NAME, { keyPath: "id", autoIncrement: true });
14+
}
15+
};
16+
17+
request.onsuccess = event => {
18+
console.log("✅ IndexedDB Opened Successfully");
19+
resolve(event.target.result);
20+
};
21+
22+
request.onerror = event => {
23+
console.error("❌ IndexedDB Error:", event.target.error);
24+
reject(event.target.error);
25+
};
26+
});
27+
}
28+
29+
// ✅ Properly save request with correct body handling
30+
async function saveRequest(request) {
31+
console.log("💾 Saving request to IndexedDB:", request.url);
32+
33+
const db = await openDB();
34+
35+
// ✅ Extract body properly
36+
let body;
37+
try {
38+
body = await request.clone().json();
39+
} catch {
40+
body = await request.clone().text();
41+
}
42+
43+
const headersObj = {};
44+
for (let [key, value] of request.headers.entries()) {
45+
headersObj[key] = value;
46+
}
47+
48+
return new Promise((resolve, reject) => {
49+
const tx = db.transaction(STORE_NAME, "readwrite");
50+
const store = tx.objectStore(STORE_NAME);
51+
52+
const requestData = {
53+
url: request.url,
54+
method: request.method,
55+
body: body,
56+
headers: headersObj,
57+
};
58+
59+
const addRequest = store.add(requestData);
60+
61+
addRequest.onsuccess = () => {
62+
console.log("✅ Request successfully stored in IndexedDB!");
63+
resolve();
64+
};
65+
66+
addRequest.onerror = (event) => {
67+
console.error("❌ Error storing request:", event.target.error);
68+
reject(event.target.error);
69+
};
70+
71+
tx.oncomplete = () => console.log("✅ Transaction completed successfully!");
72+
tx.onerror = (event) => console.error("❌ Transaction failed:", event.target.error);
73+
});
74+
}
75+
76+
// ✅ Retrieve all stored requests
77+
async function getAllRequests() {
78+
console.log("📂 Retrieving all stored requests...");
79+
80+
const db = await openDB();
81+
82+
return new Promise((resolve, reject) => {
83+
const tx = db.transaction(STORE_NAME, "readonly");
84+
const store = tx.objectStore(STORE_NAME);
85+
const req = store.getAll();
86+
87+
req.onsuccess = () => {
88+
console.log("📦 Stored Requests:", req.result);
89+
resolve(req.result);
90+
};
91+
92+
req.onerror = () => {
93+
console.error("❌ Error retrieving requests:", req.error);
94+
reject(req.error);
95+
};
96+
});
97+
}
98+
99+
// ✅ Delete request after successful sync
100+
async function deleteRequest(id) {
101+
console.log("🗑️ Deleting request with ID:", id);
102+
103+
const db = await openDB();
104+
105+
return new Promise((resolve, reject) => {
106+
const tx = db.transaction(STORE_NAME, "readwrite");
107+
const store = tx.objectStore(STORE_NAME);
108+
const req = store.delete(id);
109+
110+
req.onsuccess = () => {
111+
console.log("✅ Successfully deleted request from IndexedDB!");
112+
resolve();
113+
};
114+
115+
req.onerror = (event) => {
116+
console.error("❌ Error deleting request:", event.target.error);
117+
reject(event.target.error);
118+
};
119+
});
120+
}

iogt/static/js/iogt.js

+28-5
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,37 @@ $(document).ready(() => {
9797
});
9898

9999
const download = pageId => {
100+
console.log("Starting download for page:", pageId);
101+
100102
fetch(`/page-tree/${pageId}/`)
101-
.then(resp => resp.json())
103+
.then(resp => {
104+
if (!resp.ok) {
105+
throw new Error(`Failed to fetch URLs for caching. Status: ${resp.status}`);
106+
}
107+
return resp.json();
108+
})
102109
.then(urls => {
103-
caches.open('iogt')
104-
.then(cache => {
105-
cache.addAll(urls);
106-
});
110+
if (!urls.length) {
111+
throw new Error("No URLs received for caching.");
112+
}
113+
114+
return caches.open('iogt').then(cache => {
115+
console.log(urls)
116+
return cache.addAll(urls)
117+
.then(() => {
118+
console.log("Content cached successfully!");
119+
alert("Content is now available offline!");
120+
})
121+
.catch(error => {
122+
console.error("Caching failed:", error);
123+
alert("Failed to cache content.");
124+
});
125+
});
107126
})
127+
.catch(error => {
128+
console.error("Download error:", error);
129+
alert("Download failed. Please try again.");
130+
});
108131
};
109132

110133
const getItem = (key, defaultValue) => {

iogt/static/js/sw-init.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
const registerSW = async () => {
2-
if ('serviceWorker' in navigator) {
3-
const registration = await navigator.serviceWorker.register(serviceWorkerURL, {scope: '/'});
4-
const isPushNotificationRegistered = getItem('isPushNotificationRegistered', false);
5-
if (!isPushNotificationRegistered) {
6-
if (isAuthenticated && pushNotification) {
7-
registerPushNotification(registration);
2+
if ('serviceWorker' in navigator && 'SyncManager' in window) {
3+
try {
4+
const registration = await navigator.serviceWorker.register(serviceWorkerURL, {scope: '/'});
5+
6+
// Register background sync event
7+
const syncTags = await registration.sync.getTags();
8+
if (!syncTags.includes('sync-forms')) {
9+
await registration.sync.register('sync-forms');
10+
console.log("✅ Background sync registered for offline forms!");
11+
}
12+
13+
const isPushNotificationRegistered = getItem('isPushNotificationRegistered', false);
14+
if (!isPushNotificationRegistered) {
15+
if (isAuthenticated && pushNotification) {
16+
registerPushNotification(registration);
17+
}
818
}
19+
} catch (error) {
20+
console.error("❌ Service worker registration failed:", error);
921
}
1022
}
1123
};

iogt/templates/base.html

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
{% else %}
104104
<script src="{% static 'js/iogt-no-jquery.js' %}"></script>
105105
{% endif %}
106+
<script src="{% static 'js/idb.js' %}"></script>
106107
<script src="{% static 'js/sw-init.js' %}"></script>
107108
<noscript>
108109
<style type="text/css">

0 commit comments

Comments
 (0)