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