Loading...
More JavaScript Posts
const gcd = (...arr) => {const _gcd = (x, y) => (!y ? x : gcd(y, x % y));return [...arr].reduce((a, b) => _gcd(a, b));};gcd(8, 36); // 4gcd(...[12, 8, 32]); // 4
const getSearchTerm = delimiter => {let searchTerm = "";for (let i = 1; i < commands.length - 1; i++)searchTerm = searchTerm + commands[i] + delimiter;searchTerm += commands[commands.length - 1];return searchTerm;};
function Clock(props) {return (<div><h1>Hello, world!</h1><h2>It is {props.date.toLocaleTimeString()}.</h2></div>);}function tick() {ReactDOM.render(<Clock date={new Date()} />,document.getElementById('root'));}setInterval(tick, 1000);
function printHeap(heap, index, level) {if (index >= heap.length) {return;}console.log(" ".repeat(level) + heap[index]);printHeap(heap, 2 * index + 1, level + 1);printHeap(heap, 2 * index + 2, level + 1);}//You can call this function by passing in the heap array and the index of the root node, which is typically 0, and level = 0.let heap = [3, 8, 7, 15, 17, 30, 35, 2, 4, 5, 9];printHeap(heap,0,0)
router.post("/", async (req, res) => {const { email, password } = req.body;try {if (!email) return res.status(400).json({ msg: "An email is required" });if (!password)return res.status(400).json({ msg: "A password is required" });const user = await User.findOne({ email }).select("_id password");if (!user) return res.status(400).json({ msg: "Invalid credentials" });const match = await bcrypt.compare(password, user.password);if (!match) return res.status(400).json({ msg: "Invalid credentials" });const accessToken = genAccessToken({ id: user._id });const refreshToken = genRefreshToken({ id: user._id });res.cookie("token", refreshToken, {expires: new Date(Date.now() + 604800),httpOnly: true,});res.json({ accessToken });} catch (err) {console.log(err.message);res.status(500).json({ msg: "Error logging in user" });}});
const jwt = require("jsonwebtoken");const authToken = (req, res, next) => {const token = req.headers["x-auth-token"];try {req.user = jwt.verify(token, process.env.ACCESS_TOKEN_SECRET);next();} catch (err) {console.log(err.message);res.status(401).json({ msg: "Error authenticating token" });}};module.exports = authToken;