Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const USERNAME = 'USERNAME'
- const PASSWORD = 'PASSWORD'
- const denyAccess = (res) => {
- res.statusCode = 401;
- res.setHeader('WWW-Authenticate', 'Basic realm="Authorization
- Required');
- res.end('Unauthorized');
- }
- exports.authorizeAccess = functions.https.onRequest((req, res) => {
- if (typeof req.headers.authorization !== 'string') {
- denyAccess(res);
- return;
- }
- const base64Auth = req.headers.authorization.split(' ')[1];
- if (typeof base64Auth !== 'string' ) {
- denyAccess(res);
- return;
- }
- const [user, pass] = Buffer.from(base64Auth,
- 'base64').toString().split(':');
- if (user !== USERNAME || pass !== PASSWORD) {
- denyAccess(res);
- return;
- }
- const urlObject = url.parse(req.url);
- urlObject.pathname =
- `/${PASSWORD}${urlObject.pathname}`;
- const location = url.format(urlObject);
- res.writeHead(302, { location });
- res.end();
- });
- .
- ├── index.html
- ├── js
- | ├── main.js
- | └── main.js.map
- └── styles.css
- .
- └── PASSWORD
- ├── index.html
- ├── js
- | ├── main.js
- | └── main.js.map
- └── styles.css
- {
- ...
- "rewrites": {
- "source": "/"
- "function": "authorizeAccess"
- }
- ...
- }
Add Comment
Please, Sign In to add comment