Created
November 3, 2024 17:59
-
-
Save taurgis/e6a60e14672df31a0e1d7ea311671f3c to your computer and use it in GitHub Desktop.
ssr.js - Basic Authentication for the Composable Storefront
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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