Keep vault state and file transfers within their originating session
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { sessionGuard } from './sessionGuard.js';
|
||||
/**
|
||||
* Sunucu tarafı hesap servisleri: plan/kota, kullanıcı verisi, API anahtarları, belgeler.
|
||||
* (Önceden bunların hepsi localStorage'daydı veya sahteydi.)
|
||||
@@ -45,12 +46,14 @@ export const listDocuments = () => iste('/api/me/documents');
|
||||
export const deleteDocument = (id) => iste(`/api/me/documents?id=${encodeURIComponent(id)}`, { method: 'DELETE' });
|
||||
|
||||
export async function uploadDocument(file, kategori, ikn, ek = {}) {
|
||||
const current = sessionGuard(getToken);
|
||||
const icerikBase64 = await new Promise((resolve, reject) => {
|
||||
const r = new FileReader();
|
||||
r.onload = () => resolve(String(r.result).split(',')[1]);
|
||||
r.onerror = reject;
|
||||
r.readAsDataURL(file);
|
||||
});
|
||||
if (!current()) throw new Error('Oturum değişti; dosyayı yeniden seçin.');
|
||||
return iste('/api/me/documents', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ ad: file.name, mime: file.type, kategori, ikn, icerikBase64, ...ek }),
|
||||
@@ -59,6 +62,7 @@ export async function uploadDocument(file, kategori, ikn, ek = {}) {
|
||||
|
||||
/** Auth başlığı gerektiği için <a href> yerine blob ile indirilir. */
|
||||
export async function downloadDocument(id, ad) {
|
||||
const current = sessionGuard(getToken);
|
||||
const res = await fetch(`/api/me/documents/download?id=${encodeURIComponent(id)}`, {
|
||||
headers: { Authorization: `Bearer ${getToken()}` },
|
||||
});
|
||||
@@ -67,6 +71,7 @@ export async function downloadDocument(id, ad) {
|
||||
throw new Error(d.error || `İndirilemedi (HTTP ${res.status})`);
|
||||
}
|
||||
const blob = await res.blob();
|
||||
if (!current()) throw new Error('Oturum değişti; belgeyi yeniden açın.');
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
/** Snapshot of an authenticated operation; never continue it under another token. */
|
||||
export function sessionGuard(readToken) {
|
||||
const token = readToken();
|
||||
return () => Boolean(token) && readToken() === token;
|
||||
}
|
||||
Reference in New Issue
Block a user