Skip to content

Instantly share code, notes, and snippets.

@taurgis
Created November 3, 2024 17:59
Show Gist options
  • Save taurgis/e6a60e14672df31a0e1d7ea311671f3c to your computer and use it in GitHub Desktop.
Save taurgis/e6a60e14672df31a0e1d7ea311671f3c to your computer and use it in GitHub Desktop.
ssr.js - Basic Authentication for the Composable Storefront
// ssr.js
// ...
const AUTH = {
username: 'storefront',
password: 'password'
}
function basicAuthMiddleware(req, res, next) {
const shouldSkipAuth =
req.path.startsWith('/proxy') ||
req.path.startsWith('/mobify') ||
req.path.startsWith('/callback')
if (shouldSkipAuth) {
return next()
}
const authorization = (req.get('authorization') || '').split(' ')[1] || ''
const [username, password] = Buffer.from(authorization, 'base64').toString().split(':')
const hasValidAuth = username == AUTH.username && password == AUTH.password
if (hasValidAuth) {
return next()
}
res.set('WWW-Authenticate', 'Basic realm="Storefront"')
res.status(401).send('Auth Required!')
}
// ...
const {handler} = runtime.createHandler(options, (app) => {
// ...
app.use(basicAuthMiddleware)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment