-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathpopup.js
61 lines (56 loc) · 1.64 KB
/
popup.js
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
// @flow
import type { loginOptions } from './solid-auth-client'
import { Server } from './ipc'
import type { Session } from './session'
import type { AsyncStorage } from './storage'
import { originOf } from './url-util'
export function openIdpPopup(popupUri: string): window {
const width = 650
const height = 400
const left = window.screenX + (window.innerWidth - width) / 2
const top = window.screenY + (window.innerHeight - height) / 2
const settings = `width=${width},height=${height},left=${left},top=${top}`
return window.open(popupUri, 'solid-auth-client', settings)
}
export function obtainSession(
store: AsyncStorage,
popup: window,
options: loginOptions
): Promise<?Session> {
return new Promise((resolve, reject) => {
const popupServer = new Server(
popup,
originOf(options.popupUri || ''),
popupHandler(store, options, (session: Session) => {
popupServer.stop()
resolve(session)
})
)
popupServer.start()
})
}
export function popupHandler(
store: AsyncStorage,
{ popupUri, callbackUri }: loginOptions,
foundSessionCb: (Session) => void
) {
return async (method: string, ...args: any[]) => {
switch (method) {
// Origin
case 'getAppOrigin':
return window.location.origin
// Storage
case 'storage/getItem':
return store.getItem(...args)
case 'storage/setItem':
return store.setItem(...args)
case 'storage/removeItem':
return store.removeItem(...args)
// Login
case 'getLoginOptions':
return { popupUri, callbackUri }
case 'foundSession':
foundSessionCb(...args)
}
}
}