Skip to content

Commit

Permalink
feat: add webAuthOpener method
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed May 2, 2024
1 parent df44705 commit c0bb22f
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const isValidUrl = u => {
}

const webAuth = async (opener, opts, body) => {
const abortController = new AbortController()
try {
const res = await fetch('/-/v1/login', {
...opts,
Expand All @@ -70,23 +69,33 @@ const webAuth = async (opener, opts, body) => {
throw new WebLoginInvalidResponse('POST', res, content)
}

return await webAuthOpener(opener, loginUrl, doneUrl, opts)
} catch (er) {
if ((er.statusCode >= 400 && er.statusCode <= 499) || er.statusCode === 500) {
throw new WebLoginNotSupported('POST', {
status: er.statusCode,
headers: { raw: () => er.headers },
}, er.body)
}
throw er
}
}

const webAuthOpener = async (opener, loginUrl, doneUrl, opts) => {
const abortController = new AbortController()
try {
log.verbose('web auth', 'opening url pair')
return await Promise.all([
const [, authResult] = await Promise.all([
opener(loginUrl, { signal: abortController.signal }),
webAuthCheckLogin(doneUrl, { ...opts, cache: false }).then((r) => {
log.verbose('web auth', 'done-check finished')
abortController.abort()
return r
}),
]).then(([, authResult]) => authResult)
])
return authResult
} catch (er) {
abortController.abort()
if ((er.statusCode >= 400 && er.statusCode <= 499) || er.statusCode === 500) {
throw new WebLoginNotSupported('POST', {
status: er.statusCode,
headers: { raw: () => er.headers },
}, er.body)
}
throw er
}
}
Expand Down Expand Up @@ -262,4 +271,5 @@ module.exports = {
removeToken,
createToken,
webAuthCheckLogin,
webAuthOpener,
}

0 comments on commit c0bb22f

Please sign in to comment.