Loading...
More JavaScript Posts
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" });}});
import { createSlice } from "@reduxjs/toolkit";const alert = createSlice({name: "alert",initialState: {msg: "",status: "",},reducers: {set_alert: (state, action) => {return {...state,msg: action.payload.msg,status: action.payload.status,};},clear_alert: (state, action) => {return {...state,msg: "",status: "",};},},});export default alert.reducer;const { set_alert, clear_alert } = alert.actions;export const setAlert = (msg, status) => dispatch => {dispatch(set_alert({ msg, status }));setTimeout(() => dispatch(clear_alert()), 4000);};export const clearAlert = () => dispatch => {dispatch(clear_alert());};
const dragAndDropDiv = editor.container;dragAndDropDiv.addEventListener('dragover', function(e) {e.stopPropagation();e.preventDefault();e.dataTransfer.dropEffect = 'copy';});dragAndDropDiv.addEventListener("drop",function(e){// Prevent default behavior (Prevent file from being opened)e.stopPropagation();e.preventDefault();const files = e.dataTransfer.items; // Array of all filesconsole.assert(files.length >= 1);if (files[0].kind === 'file') {var file = e.dataTransfer.items[0].getAsFile();const fileSize = file.size;const fileName = file.name;const fileMimeType = file.type;reader = new FileReader();reader.onloadend = function(){editor.setValue(reader.result);}reader.readAsText(file);}else{//Maybe handle if text is droppedconsole.log(files[0].kind);}});
const unzipWith = (arr, fn) =>arr.reduce((acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc),Array.from({length: Math.max(...arr.map(x => x.length))}).map(x => [])).map(val => fn(...val));unzipWith([[1, 10, 100],[2, 20, 200],],(...args) => args.reduce((acc, v) => acc + v, 0));// [3, 30, 300]
// `arr` is an arrayconst clone = arr => arr.slice(0);// Orconst clone = arr => [...arr];// Orconst clone = arr => Array.from(arr);// Orconst clone = arr => arr.map(x => x);// Orconst clone = arr => JSON.parse(JSON.stringify(arr));// Orconst clone = arr => arr.concat([]);
const primes = num => {let arr = Array.from({ length: num - 1 }).map((x, i) => i + 2),sqroot = Math.floor(Math.sqrt(num)),numsTillSqroot = Array.from({ length: sqroot - 1 }).map((x, i) => i + 2);numsTillSqroot.forEach(x => (arr = arr.filter(y => y % x !== 0 || y === x)));return arr;};primes(10); // [2, 3, 5, 7]