Skip to content

Commit 4517bae

Browse files
committed
Rewrite exports as functions.
1 parent cf7d5ce commit 4517bae

1 file changed

Lines changed: 33 additions & 28 deletions

File tree

src/webid-oidc.js

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import type { webIdOidcSession } from './session'
1010
import type { AsyncStorage } from './storage'
1111
import { defaultStorage, getData, updateStorage } from './storage'
1212

13-
export const login = async (
13+
export async function login(
1414
idp: string,
1515
options: loginOptions
16-
): Promise<?null> => {
16+
): Promise<?null> {
1717
try {
1818
const rp = await getRegisteredRp(idp, options)
1919
await saveAppHashFragment(options.storage)
@@ -25,9 +25,9 @@ export const login = async (
2525
}
2626
}
2727

28-
export const currentSession = async (
28+
export async function currentSession(
2929
storage: AsyncStorage = defaultStorage()
30-
): Promise<?webIdOidcSession> => {
30+
): Promise<?webIdOidcSession> {
3131
try {
3232
const rp = await getStoredRp(storage)
3333
if (!rp) {
@@ -55,26 +55,29 @@ export const currentSession = async (
5555
}
5656
}
5757

58-
export const logout = (storage: AsyncStorage): Promise<void> =>
59-
getStoredRp(storage)
60-
.then(rp => (rp ? rp.logout() : undefined))
61-
.catch(err => {
58+
export async function logout(storage: AsyncStorage): Promise<void> {
59+
const rp = await getStoredRp(storage)
60+
if (rp) {
61+
try {
62+
rp.logout()
63+
} catch (err) {
6264
console.warn('Error logging out of the WebID-OIDC session')
6365
console.error(err)
64-
})
66+
}
67+
}
68+
}
6569

66-
export const getRegisteredRp = (
70+
export async function getRegisteredRp(
6771
idp: string,
6872
options: loginOptions
69-
): Promise<RelyingParty> =>
70-
getStoredRp(options.storage).then(rp => {
71-
if (rp && rp.provider.url === idp) {
72-
return rp
73-
}
74-
return registerRp(idp, options).then(rp =>
75-
storeRp(options.storage, idp, rp)
76-
)
77-
})
73+
): Promise<RelyingParty> {
74+
let rp = await getStoredRp(options.storage)
75+
if (!rp || rp.provider.url !== idp) {
76+
rp = await registerRp(idp, options)
77+
await storeRp(options.storage, idp, rp)
78+
}
79+
return rp
80+
}
7881

7982
async function getStoredRp(storage: AsyncStorage): Promise<?RelyingParty> {
8083
const data = await getData(storage)
@@ -99,10 +102,10 @@ async function storeRp(
99102
return rp
100103
}
101104

102-
const registerRp = (
105+
function registerRp(
103106
idp: string,
104107
{ storage, callbackUri }: loginOptions
105-
): Promise<RelyingParty> => {
108+
): Promise<RelyingParty> {
106109
const responseType = 'id_token token'
107110
const registration = {
108111
issuer: idp,
@@ -123,33 +126,35 @@ const registerRp = (
123126
return RelyingParty.register(idp, registration, options)
124127
}
125128

126-
const sendAuthRequest = async (
129+
async function sendAuthRequest(
127130
rp: RelyingParty,
128131
{ callbackUri, storage }: loginOptions
129-
): Promise<void> => {
132+
): Promise<void> {
130133
const data = await getData(storage)
131134
const url = await rp.createRequest({ redirect_uri: callbackUri }, data)
132135
await updateStorage(storage, () => data)
133136
return navigateTo(url)
134137
}
135138

136-
const saveAppHashFragment = (store: AsyncStorage): Promise<any> =>
137-
updateStorage(store, data => ({
139+
async function saveAppHashFragment(store: AsyncStorage): Promise<void> {
140+
await updateStorage(store, data => ({
138141
...data,
139142
appHashFragment: window.location.hash
140143
}))
144+
}
141145

142-
const restoreAppHashFragment = (store: AsyncStorage): Promise<any> =>
143-
updateStorage(store, data => {
146+
async function restoreAppHashFragment(store: AsyncStorage): Promise<void> {
147+
await updateStorage(store, data => {
144148
window.location.hash = data.appHashFragment
145149
delete data.appHashFragment
146150
return data
147151
})
152+
}
148153

149154
/**
150155
* Answers whether a HTTP response requires WebID-OIDC authentication.
151156
*/
152-
export const requiresAuth = (resp: Response): boolean => {
157+
export function requiresAuth(resp: Response): boolean {
153158
if (resp.status !== 401) {
154159
return false
155160
}

0 commit comments

Comments
 (0)