Skip to content

Commit f2196c5

Browse files
committed
Better error message when connecting to insecure contexts.
1 parent 7bd5cd0 commit f2196c5

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

tools/diagnostics-app/src/library/powersync/TokenConnector.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class TokenConnector implements PowerSyncBackendConnector {
2222
}
2323

2424
async signIn(credentials: Credentials) {
25+
validateSecureContext(credentials.endpoint);
2526
try {
2627
localStorage.setItem('powersync_credentials', JSON.stringify(credentials));
2728
await connect();
@@ -39,3 +40,19 @@ export class TokenConnector implements PowerSyncBackendConnector {
3940
localStorage.removeItem('powersync_credentials');
4041
}
4142
}
43+
44+
function validateSecureContext(url: string) {
45+
if (!location.href.startsWith('https:')) {
46+
return;
47+
}
48+
const parsedUrl = new URL(url);
49+
const secure =
50+
parsedUrl.protocol === 'https:' ||
51+
parsedUrl.hostname === 'localhost' ||
52+
parsedUrl.hostname === '127.0.0.1' ||
53+
parsedUrl.hostname === '::1';
54+
if (!secure) {
55+
throw new Error(`Cannot connect to http endpoints from the hosted diagnostics app.
56+
Run either the PowerSync endpoint on http://localhost, or the diagnostics app on http://localhost.`);
57+
}
58+
}

0 commit comments

Comments
 (0)