Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 6 additions & 6 deletions data.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[
{
{
"_id": "682bab8c12155b00101732ce",
"message": "Berlin baby",
"hearts": 37,
"createdAt": "2025-05-19T22:07:08.999Z",
"__v": 0
},
{
"_id": "682e53cc4fddf50010bbe739",
"_id": "682e53cc4fddf50010bbe739",
"message": "My family!",
"hearts": 0,
"createdAt": "2025-05-22T22:29:32.232Z",
Expand All @@ -25,7 +25,7 @@
"message": "Newly washed bedlinen, kids that sleeps through the night.. FINGERS CROSSED 🤞🏼\n",
"hearts": 6,
"createdAt": "2025-05-21T21:42:23.862Z",
"__v": 0
"__v": 0
},
{
"_id": "682e45804fddf50010bbe736",
Expand Down Expand Up @@ -53,7 +53,7 @@
"message": "A god joke: \nWhy did the scarecrow win an award?\nBecause he was outstanding in his field!",
"hearts": 12,
"createdAt": "2025-05-20T20:54:51.082Z",
"__v": 0
"__v": 0
},
{
"_id": "682cebbe17487d0010a298b5",
Expand All @@ -74,7 +74,7 @@
"message": "Summer is coming...",
"hearts": 2,
"createdAt": "2025-05-20T15:03:22.379Z",
"__v": 0
"__v": 0
},
{
"_id": "682c706c951f7a0017130024",
Expand Down Expand Up @@ -118,4 +118,4 @@
"createdAt": "2025-05-19T22:07:08.999Z",
"__v": 0
}
]
]
26 changes: 26 additions & 0 deletions middleware/authMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { User } from "../models/User.js";

export const authenticateUser = async (req, res, next) => {
try {
const user = await User.findOne({
accessToken: req.header("Authorization").replace("Bearer ", ""),
});

if (user) {
req.user = user;
next();
} else {
res.status(401).json({
success: false,
message: "Unauthorized: Invalid or missing access token",
loggedOut: true,
});
}
} catch (error) {
res.status(500).json({
success: false,
message: "Internal server error",
error: error.message,
});
}
};
23 changes: 23 additions & 0 deletions models/Thoughts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import mongoose, { Schema } from "mongoose";

// Mongoose schema and model for Thought
const thoughtsSchema = new mongoose.Schema({
message: {
type: String,
required: [true, "Message is required"],
trim: true,
minlength: [5, "Message must be at least 5 characters"],
maxlength: [140, "Message cannot be longer than 140 characters"],
},
hearts: {
type: Number,
default: 0,
min: [0, "Hearts cannot be negative"],
},
createdAt: {
type: Date,
default: Date.now,
},
});

export const Thoughts = mongoose.model("Thoughts", thoughtsSchema);
31 changes: 31 additions & 0 deletions models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import mongoose, { Schema } from "mongoose";
import crypto from "crypto";

const UserSchema = new Schema(
{
username: {
type: String,
required: true,
unique: true,
trim: true,
},
email: {
type: String,
required: true,
unique: true,
trim: true,
lowercase: true,
},
password: {
type: String,
required: true,
},
accessToken: {
type: String,
default: () => crypto.randomBytes(128).toString("hex"),
},
},
{ timestamps: true },
);

export const User = mongoose.model("User", UserSchema);
29 changes: 22 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,33 @@
"name": "project-api",
"version": "1.0.0",
"description": "Project API",
"homepage": "https://github.com/SaraEnderborg/js-project-api#readme",
"bugs": {
"url": "https://github.com/SaraEnderborg/js-project-api/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/SaraEnderborg/js-project-api.git"
},
"license": "ISC",
"author": "",
"type": "module",
"main": "server.js",
"scripts": {
"start": "babel-node server.js",
"dev": "nodemon server.js --exec babel-node"
},
"author": "",
"license": "ISC",
"dependencies": {
"@babel/core": "^7.17.9",
"@babel/node": "^7.16.8",
"@babel/preset-env": "^7.16.11",
"@babel/core": "^7.28.6",
"@babel/node": "^7.28.6",
"@babel/preset-env": "^7.28.6",
"bcrypt": "^6.0.0",
"cors": "^2.8.5",
"express": "^4.17.3",
"nodemon": "^3.0.1"
"dotenv": "^17.2.3",
"express": "^4.22.1",
"express-list-endpoints": "^7.1.1",
"mongodb": "^7.0.0",
"mongoose": "^9.1.5",
"nodemon": "^3.1.11"
}
}
Loading