Rewrite this code to JavaScript. Write only code, without Markdown: const jwt =…
- User
User: Rewrite this code to JavaScript. Write only code, without Markdown: const jwt = require('jsonwebtoken'); const jwtConfig = require('../config/jwtConfig');
const basicAuth = (req, res, next) => { const authHeader = req.headers['authorization']; if (!authHeader) { return res.status(401).json({ error: 'No credentials provided' }); }
codeconst credentials = Buffer.from(authHeader.split(' ')[1], 'base64').toString('ascii'); const [username, password] = credentials.split(':'); // Credenciais fixas para exemplo (substitua por validação real) if (username === 'admin' && password === 'password123') { req.user = { username }; next(); } else { res.status(401).json({ error: 'Invalid credentials' }); }};
const jwtAuth = (req, res, next) => { const token = req.headers['authorization']?.split(' ')[1]; if (!token) { return res.status(401).json({ error: 'No token provided' }); }
codejwt.verify(token, jwtConfig.secret, (err, decoded) => { if (err) { return res.status(401).json({ error: 'Invalid token' }); } req.user = decoded; next(); });};
module.exports = { basicAuth, jwtAuth }; User: Translate code Assistant:
- Assistant
const jwt = require('jsonwebtoken'); const jwtConfig = require('../config/jwtConfig');
const basicAuth = (req, res, next) => { const authHeader = req.headers['authorization']; if (!authHeader) { return res.status(401).json({ error: 'No credentials provided' }); }
codeconst credentials = Buffer.from(authHeader.split(' ')[1], 'base64').toString('ascii'); const [username, password] = credentials.split(':'); // Fixed credentials for example (replace with real validation) if (username === 'admin' && password === 'password123') { req.user = { username }; next(); } else { res.status(401).json({ error: 'Invalid credentials' }); }};
const jwtAuth = (req, res, next) => { const token = req.headers['authorization']?.split(' ')[1]; if (!token) { return res.status(401).json({ error: 'No token provided' }); }
code