Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b4b9c99
installed dependencies
alex91-html Jun 4, 2025
dd0fcb9
getting
alex91-html Jun 4, 2025
cf51668
getting data by id problem solved
alex91-html Jun 4, 2025
412a63b
trying to fix crash
alex91-html Jun 4, 2025
b01328d
trying to fix crash
alex91-html Jun 4, 2025
218ed2d
added to do md
alex91-html Jun 4, 2025
1de4fa5
intalled mongoose
alex91-html Jun 6, 2025
f91e126
mongo URL'
alex91-html Jun 6, 2025
f72a39f
mongoose.connect(mongoUrl)
alex91-html Jun 6, 2025
c1d33de
Schema added
alex91-html Jun 6, 2025
8dcfc30
added model
alex91-html Jun 6, 2025
222d15d
updating mongoose
alex91-html Jun 6, 2025
9b45099
deployed
alex91-html Jun 9, 2025
cd5aa31
updated to do list
alex91-html Jun 12, 2025
33ef28a
fixing problem api
alex91-html Jun 12, 2025
102ddea
trying to debugg
alex91-html Jun 13, 2025
0e5ee01
fixed bugs but liking and posting still not working
alex91-html Jun 16, 2025
c5a218e
fixes posting new thought, have to fix edit, save and cancel and liking
alex91-html Jun 16, 2025
961ffee
changes to the front end
alex91-html Jun 16, 2025
5623121
added authentication
alex91-html Jun 16, 2025
0c7ba1f
added folders: endpoints, middleware, models
alex91-html Jun 16, 2025
f680318
moved schema
alex91-html Jun 16, 2025
bf4c068
testing with hout userID
alex91-html Jun 18, 2025
0e4be3d
debugging
alex91-html Jun 18, 2025
394b5ef
debugged
alex91-html Jun 19, 2025
e10fc7e
updated to do list
alex91-html Jun 24, 2025
35d33d2
implementing user.js with user model
alex91-html Jun 24, 2025
cd0507c
installaed bcrypt
alex91-html Jun 24, 2025
f36a797
final
alex91-html Jun 24, 2025
b7c0b25
removed email
alex91-html Jun 24, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 0 additions & 121 deletions data.json

This file was deleted.

22 changes: 22 additions & 0 deletions middleware/authMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { User } from "../models/user.js";

export const authenticateUser = async (req, res, next) => {
try {
const accessToken = req.header("Authorization");
const user = await User.findOne({ accessToken });
if (user) {
req.user = user;
next();
} else {
res.status(401).json({
message: "Authentication missing or invalid.",
loggedOut: true
});
}
} catch (error) {
res.status(500).json({
message: "Internal server error",
error: error.message
});
}
};
27 changes: 27 additions & 0 deletions models/Thought.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import mongoose, { Schema } from "mongoose";

const thoughtSchema = new mongoose.Schema({
message: {
type: String,
required: true,
minLength: 5,
maxLength: 140,
},
hearts: {
type: Number,
default: 0,
min: 0,
},
createdAt: {
type: Date,
default: Date.now,
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
required: true
}
});

export const Thought = mongoose.model("Thought", thoughtSchema);

24 changes: 24 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import mongoose from "mongoose";

const userSchema = new mongoose.Schema({
username: {
type: String,
required: true,
unique: true,
trim: true,
minlength: 3,
maxlength: 10
},
password: {
type: String,
required: true,
minlength: 6
},
accessToken: {
type: String,
default: null
}
});

export const User = mongoose.model("User", userSchema);

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@
"@babel/core": "^7.17.9",
"@babel/node": "^7.16.8",
"@babel/preset-env": "^7.16.11",
"bcrypt": "^6.0.0",
"cors": "^2.8.5",
"dotenv": "^16.5.0",
"express": "^4.17.3",
"nodemon": "^3.0.1"
"express-list-endpoints": "^7.1.1",
"jsonwebtoken": "^9.0.2",
"mongodb": "^6.17.0",
"mongoose": "^8.15.1",
"nodemon": "^3.1.10"
}
}
}
56 changes: 56 additions & 0 deletions routes/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import express from "express";
import bcrypt from "bcrypt";
import crypto from "crypto";
import { User } from "../models/user.js";

const router = express.Router();

// Helper to generate a random access token
const generateAccessToken = () => crypto.randomBytes(32).toString("hex");

// Register endpoint
router.post("/register", async (req, res) => {
const { username, password } = req.body;
if (!username || !password) {
return res.status(400).json({ error: "Username and password are required." });
}
try {
const existingUser = await User.findOne({ username });
if (existingUser) {
return res.status(400).json({ error: "Username already exists." });
}
const hashedPassword = await bcrypt.hash(password, 10);
const accessToken = generateAccessToken();
const user = new User({ username, password: hashedPassword, accessToken });
await user.save();
res.status(201).json({ message: "User registered successfully.", accessToken, username: user.username });
} catch (error) {
res.status(500).json({ error: "Failed to register user.", details: error.message });
}
});

// Login endpoint
router.post("/login", async (req, res) => {
const { username, password } = req.body;
if (!username || !password) {
return res.status(400).json({ error: "Username and password are required." });
}
try {
const user = await User.findOne({ username });
if (!user) {
return res.status(401).json({ error: "Invalid username or password." });
}
const passwordMatch = await bcrypt.compare(password, user.password);
if (!passwordMatch) {
return res.status(401).json({ error: "Invalid username or password." });
}
// Generate a new accessToken on login
user.accessToken = generateAccessToken();
await user.save();
res.status(200).json({ accessToken: user.accessToken, username: user.username });
} catch (error) {
res.status(500).json({ error: "Failed to log in.", details: error.message });
}
});

export default router;
Loading