2019-12-20 16:32:31 +01:00
|
|
|
import log from 'loglevel'
|
|
|
|
import extension from 'extensionizer'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a middleware that intercepts `wallet_registerOnboarding` messages
|
2020-02-20 23:39:00 +01:00
|
|
|
* @param {{ location: string, registerOnboarding: Function }} opts - The middleware options
|
2019-12-20 16:32:31 +01:00
|
|
|
* @returns {(req: any, res: any, next: Function, end: Function) => void}
|
|
|
|
*/
|
2020-05-06 00:19:38 +02:00
|
|
|
export default function createOnboardingMiddleware ({ location, registerOnboarding }) {
|
2019-12-20 16:32:31 +01:00
|
|
|
return async function originMiddleware (req, res, next, end) {
|
|
|
|
try {
|
|
|
|
if (req.method !== 'wallet_registerOnboarding') {
|
|
|
|
next()
|
|
|
|
return
|
|
|
|
}
|
2020-02-20 23:39:00 +01:00
|
|
|
if (req.tabId && req.tabId !== extension.tabs.TAB_ID_NONE) {
|
|
|
|
await registerOnboarding(location, req.tabId)
|
2019-12-20 16:32:31 +01:00
|
|
|
} else {
|
|
|
|
log.debug(`'wallet_registerOnboarding' message from ${location} ignored due to missing tabId`)
|
|
|
|
}
|
|
|
|
res.result = true
|
|
|
|
end()
|
|
|
|
} catch (error) {
|
|
|
|
end(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|