GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — EcommerceWebApp/text.text at main · RabbitDaCoder/EcommerceWebApp Expand file tree/ Copy pathtext.textMore file actions 191 lines (146 loc) · 4.68 KB / Copy pathtext.textFile metadata and controls191 lines (146 loc) · 4.68 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 //the error class class CustomError extends Error { constructor(message, statusCode) { super(message); this.statusCode = statusCode; // Ensure the stack trace is captured Error.captureStackTrace(this, this.constructor); } } export default CustomError; // how to use the custom error import CustomError from "../utils/CustomError.js"; export const signup = async (req, res, next) => { const { email, password, name } = req.body; try { // Check if user exists const userExists = await User.findOne({ email }); if (userExists) { throw new CustomError("User already exists", 400); } const user = await User.create({ email, name, password }); // Authenticate user const { accessToken, refreshToken } = generateTokens(user._id); await storeRefreshToken(user._id, refreshToken); setCookies(res, accessToken, refreshToken); res.status(201).json({ user: { _id: user._id, name: user.name, email: user.email, role: user.role, }, }); } catch (error) { next(error); // Pass the error to the global error handler } }; // how to create an error middleware const errorMiddleware = (err, req, res, next) => { console.error(err.stack); // Log error to the console const statusCode = err.statusCode || 500; // Default to 500 if no status code is set const message = err.message || "Internal Server Error"; res.status(statusCode).json({ success: false, message: message, }); }; export default errorMiddleware; // how to use this middleware iin server.js import express from "express"; import dotenv from "dotenv"; import authRoutes from "./routes/auth.routes.js"; import errorMiddleware from "./middlewares/errorMiddleware.js"; // Import error middleware dotenv.config(); const app = express(); const PORT = process.env.PORT || 5000; app.use(express.json()); // Routes app.use("/api/auth", authRoutes); // 404 Middleware (Must be before error middleware) app.use((req, res, next) => { res.status(404).json({ success: false, message: "Route not found" }); }); // Global Error Handler (MUST be the last middleware) app.use(errorMiddleware); app.listen(PORT, () => { console.log(`Server running on http://localhost:${PORT}`); }); // how to implement remember me in login import jwt from "jsonwebtoken"; import dotenv from "dotenv"; import bcrypt from "bcrypt"; import User from "../models/user.model.js"; // Your user model dotenv.config(); export const login = async (req, res) => { try { const { email, password, rememberMe } = req.body; // Get user input const user = await User.findOne({ email }); if (!user) return res.status(404).json({ message: "User not found" }); const isMatch = await bcrypt.compare(password, user.password); if (!isMatch) return res.status(400).json({ message: "Invalid credentials" }); // Generate JWT token const token = jwt.sign({ id: user._id }, process.env.JWT_SECRET, { expiresIn: rememberMe ? "30d" : "1h", // Longer expiry if "Remember Me" is checked }); // Set token in cookies res.cookie("token", token, { httpOnly: true, secure: process.env.NODE_ENV === "production", sameSite: "Strict", maxAge: rememberMe ? 30 * 24 * 60 * 60 * 1000 : null, // 30 days or session-based }); res.status(200).json({ message: "Login successful" }); } catch (error) { res.status(500).json({ message: "Server error", error }); } }; // the frontend import { useState } from "react"; import axios from "axios"; const Login = () => { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [rememberMe, setRememberMe] = useState(false); const handleLogin = async (e) => { e.preventDefault(); try { const res = await axios.post("http://localhost:5000/api/auth/login", { email, password, rememberMe, }, { withCredentials: true }); // Allows cookies to be stored alert("Login successful!"); } catch (err) { console.error(err.response.data.message); } }; return ( <form onSubmit={handleLogin}> <input type="email" placeholder="Email" value={email} onChange={(e) => setEmail(e.target.value)} required /> <input type="password" placeholder="Password" value={password} onChange={(e) => setPassword(e.target.value)} required /> <label> <input type="checkbox" checked={rememberMe} onChange={(e) => setRememberMe(e.target.checked)} /> Remember Me </label> <button type="submit">Login</button> </form> ); }; export default Login; |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |