Skip to main content

Send mail with nodemailer

0 likes • Nov 18, 2022 • 1 view
JavaScript
Loading...

More JavaScript Posts

Convert data to yyyy mm dd format

0 likes • Nov 19, 2022 • 1 view
JavaScript
// `date` is a `Date` object
const formatYmd = date => date.toISOString().slice(0, 10);
// Example
formatYmd(new Date()); // 2020-05-06

Express Login Endpoint

CHS
0 likes • Mar 11, 2021 • 4 views
JavaScript
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" });
}
});

Reverse a string

0 likes • Nov 19, 2022 • 1 view
JavaScript
const reverse = str => str.split('').reverse().join('');
reverse('hello world');
// Result: 'dlrow olleh'

React Short Circuit Evaluation

CHS
0 likes • Feb 3, 2021 • 0 views
JavaScript
import React, { useState } from 'react' import Welcome from '../components/Welcome' function About() { const [showWelcome, setShowWelcome] = useState(false) return ( <div> {showWelcome ? <Welcome /> : null} </div> <div> {showWelcome && <Welcome /> } </div> ) } export default App

Mongoose DB Connection

CHS
0 likes • Mar 11, 2021 • 0 views
JavaScript
require("dotenv").config();
const mongoose = require("mongoose");
const db = process.env.MONGO_URI;
const connectDB = async () => {
try {
await mongoose.connect(db, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
useCreateIndex: true
});
console.log("MongoDB Connected");
} catch (err) {
console.error(err.message);
}
};
module.exports = connectDB;

localStorage Cookie Consent

0 likes • Oct 9, 2023 • 135 views
JavaScript
import React, { useState, useEffect } from 'react';
import Link from 'next/link';
export default function CookieBanner() {
// Initialize with a default value (false if not previously set in localStorage)
const [cookieConsent, setCookieConsent] = useState(() =>
localStorage.getItem('cookieConsent') === 'true' ? true : false
);
useEffect(() => {
const newCookieConsent = cookieConsent ? 'granted' : 'denied';
window.gtag('consent', 'update', {
analytics_storage: newCookieConsent
});
localStorage.setItem('cookieConsent', String(cookieConsent));
}, [cookieConsent]);
return !cookieConsent && (
<div>
<div>
<p>
We use cookies to enhance the user experience.{' '}
<Link href='/privacy/'>View privacy policy</Link>
</p>
</div>
<div>
<button type="button" onClick={() => setCookieConsent(false)}>Decline</button>
<button type="button" onClick={() => setCookieConsent(true)}>Allow</button>
</div>
</div>
)
}