Skip to content

Commit 3016eb6

Browse files
ask user for auth if no token (#1389)
1 parent 402c407 commit 3016eb6

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/puter-js/src/modules/Workers.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ export class WorkersHandler {
77
}
88

99
async create(workerName, filePath) {
10+
if (!puter.authToken && puter.env === 'web') {
11+
try {
12+
await puter.ui.authenticateWithPuter();
13+
} catch (e) {
14+
// if authentication fails, throw an error
15+
throw 'Authentication failed.';
16+
}
17+
}
18+
1019
workerName = workerName.toLocaleLowerCase(); // just incase
1120
let currentWorkers = await puter.kv.get("user-workers");
1221
if (!currentWorkers) {
@@ -26,18 +35,45 @@ export class WorkersHandler {
2635
}
2736

2837
async exec(...args) {
38+
if (!puter.authToken && puter.env === 'web') {
39+
try {
40+
await puter.ui.authenticateWithPuter();
41+
} catch (e) {
42+
// if authentication fails, throw an error
43+
throw 'Authentication failed.';
44+
}
45+
}
46+
2947
const req = new Request(...args);
3048
if (!req.headers.get("puter-auth")) {
31-
req.headers.set("puter-auth", this.authToken);
49+
req.headers.set("puter-auth", puter.authToken);
3250
}
3351
return fetch(req);
3452
}
3553

3654
async list() {
55+
if (!puter.authToken && puter.env === 'web') {
56+
try {
57+
await puter.ui.authenticateWithPuter();
58+
} catch (e) {
59+
// if authentication fails, throw an error
60+
throw 'Authentication failed.';
61+
}
62+
}
63+
3764
return await puter.kv.get("user-workers");
3865
}
3966

4067
async get(workerName) {
68+
if (!puter.authToken && puter.env === 'web') {
69+
try {
70+
await puter.ui.authenticateWithPuter();
71+
} catch (e) {
72+
// if authentication fails, throw an error
73+
throw 'Authentication failed.';
74+
}
75+
}
76+
4177
workerName = workerName.toLocaleLowerCase(); // just incase
4278
try {
4379
return (await puter.kv.get("user-workers"))[workerName].url;
@@ -47,6 +83,15 @@ export class WorkersHandler {
4783
}
4884

4985
async delete(workerName) {
86+
if (!puter.authToken && puter.env === 'web') {
87+
try {
88+
await puter.ui.authenticateWithPuter();
89+
} catch (e) {
90+
// if authentication fails, throw an error
91+
throw 'Authentication failed.';
92+
}
93+
}
94+
5095
workerName = workerName.toLocaleLowerCase(); // just incase
5196
const driverCall = await puter.drivers.call("workers", "worker-service", "destroy", { authorization: puter.authToken, workerName });
5297

0 commit comments

Comments
 (0)