UNPKG

2.92 kBJavaScriptView Raw
1import * as cidLog from "./common/cid-log.js";
2import * as common from "./common/index.js";
3import * as base64 from "./common/base64.js";
4import * as did from "./did/index.js";
5import * as path from "./path.js";
6import * as crypto from "./crypto/index.js";
7import * as storage from "./storage/index.js";
8import * as ucan from "./ucan/store.js";
9import { USERNAME_STORAGE_KEY, VERSION } from "./common/index.js";
10import { setup } from "./setup/internal.js";
11// FUNCTIONS
12/**
13 * Retrieve the authenticated username.
14 */
15export async function authenticatedUsername() {
16 return common.authenticatedUsername();
17}
18/**
19 * Leave.
20 *
21 * Removes any trace of the user and redirects to the lobby.
22 */
23export async function leave({ withoutRedirect } = {}) {
24 await storage.removeItem(USERNAME_STORAGE_KEY);
25 await ucan.clearStorage();
26 await cidLog.clear();
27 await crypto.keystore.clear();
28 (globalThis.filesystems || []).forEach((f) => f.deactivate());
29 if (!withoutRedirect && globalThis.location) {
30 globalThis.location.href = setup.endpoints.lobby;
31 }
32}
33/**
34 * Redirects to a lobby.
35 *
36 * NOTE: Only works on the main thread, as it uses `window.location`.
37 *
38 * @param permissions The permissions from `initialise`.
39 * Pass `null` if working without permissions.
40 * @param redirectTo Specify the URL you want users to return to.
41 * Uses the current url by default.
42 */
43export async function redirectToLobby(permissions, redirectTo) {
44 const app = permissions?.app;
45 const fs = permissions?.fs;
46 const platform = permissions?.platform;
47 const raw = permissions?.raw;
48 const exchangeDid = await did.exchange();
49 const writeDid = await did.write();
50 const sharedRepo = !!document.body.querySelector("iframe#webnative-ipfs") && typeof SharedWorker === "function";
51 redirectTo = redirectTo || window.location.href;
52 // Compile params
53 const params = [
54 ["didExchange", exchangeDid],
55 ["didWrite", writeDid],
56 ["redirectTo", redirectTo],
57 ["sdk", VERSION.toString()],
58 ["sharedRepo", sharedRepo ? "t" : "f"]
59 ].concat(app ? [["appFolder", `${app.creator}/${app.name}`]] : [], fs?.private ? fs.private.map(p => ["privatePath", path.toPosix(p, { absolute: true })]) : [], fs?.public ? fs.public.map(p => ["publicPath", path.toPosix(p, { absolute: true })]) : [], raw ? [["raw", base64.urlEncode(JSON.stringify(raw))]] : []).concat((() => {
60 const apps = platform?.apps;
61 switch (typeof apps) {
62 case "string": return [["app", apps]];
63 case "object": return apps.map(a => ["app", a]);
64 default: return [];
65 }
66 })());
67 // And, go!
68 window.location.href = setup.endpoints.lobby + "?" +
69 params
70 .map(([k, v]) => encodeURIComponent(k) + "=" + encodeURIComponent(v))
71 .join("&");
72}
73//# sourceMappingURL=auth.js.map
\No newline at end of file