UNPKG

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