├── .gitignore ├── package.json ├── auth.js ├── db.js └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "week-7", 3 | "version": "1.0.0", 4 | "main": "index.js", 5 | "scripts": { 6 | "test": "echo \"Error: no test specified\" && exit 1" 7 | }, 8 | "keywords": [], 9 | "author": "", 10 | "license": "ISC", 11 | "description": "", 12 | "dependencies": { 13 | "express": "^4.21.0", 14 | "jsonwebtoken": "^9.0.2", 15 | "mongoose": "^8.6.2" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /auth.js: -------------------------------------------------------------------------------- 1 | const jwt = require("jsonwebtoken"); 2 | const JWT_SECRET = "s3cret"; 3 | 4 | function auth(req, res, next) { 5 | const token = req.headers.authorization; 6 | 7 | const response = jwt.verify(token, JWT_SECRET); 8 | 9 | if (response) { 10 | req.userId = response.id; 11 | next(); 12 | } else { 13 | res.status(403).json({ 14 | message: "Incorrect creds" 15 | }) 16 | } 17 | } 18 | 19 | module.exports = { 20 | auth, 21 | JWT_SECRET 22 | } 23 | -------------------------------------------------------------------------------- /db.js: -------------------------------------------------------------------------------- 1 | const mongoose = require("mongoose"); 2 | 3 | const Schema = mongoose.Schema; 4 | const ObjectId = Schema.ObjectId; 5 | 6 | const User = new Schema({ 7 | name: String, 8 | email: {type: String, unique: true}, 9 | password: String 10 | }); 11 | 12 | const Todo = new Schema({ 13 | userId: ObjectId, 14 | title: String, 15 | done: Boolean 16 | }); 17 | 18 | const UserModel = mongoose.model('users', User); 19 | const TodoModel = mongoose.model('todos', Todo); 20 | 21 | module.exports = { 22 | UserModel, 23 | TodoModel 24 | } -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const { UserModel, TodoModel } = require("./db"); 3 | const { auth, JWT_SECRET } = require("./auth"); 4 | const jwt = require("jsonwebtoken"); 5 | const mongoose = require("mongoose"); 6 | 7 | mongoose.connect("") 8 | 9 | const app = express(); 10 | app.use(express.json()); 11 | 12 | app.post("/signup", async function(req, res) { 13 | const email = req.body.email; 14 | const password = req.body.password; 15 | const name = req.body.name; 16 | 17 | await UserModel.create({ 18 | email: email, 19 | password: password, 20 | name: name 21 | }); 22 | 23 | res.json({ 24 | message: "You are signed up" 25 | }) 26 | }); 27 | 28 | 29 | app.post("/signin", async function(req, res) { 30 | const email = req.body.email; 31 | const password = req.body.password; 32 | 33 | const response = await UserModel.findOne({ 34 | email: email, 35 | password: password, 36 | }); 37 | 38 | if (response) { 39 | const token = jwt.sign({ 40 | id: response._id.toString() 41 | }, JWT_SECRET); 42 | 43 | res.json({ 44 | token 45 | }) 46 | } else { 47 | res.status(403).json({ 48 | message: "Incorrect creds" 49 | }) 50 | } 51 | }); 52 | 53 | 54 | app.post("/todo", auth, async function(req, res) { 55 | const userId = req.userId; 56 | const title = req.body.title; 57 | const done = req.body.done; 58 | 59 | await TodoModel.create({ 60 | userId, 61 | title, 62 | done 63 | }); 64 | 65 | res.json({ 66 | message: "Todo created" 67 | }) 68 | }); 69 | 70 | 71 | app.get("/todos", auth, async function(req, res) { 72 | const userId = req.userId; 73 | 74 | const todos = await TodoModel.find({ 75 | userId 76 | }); 77 | 78 | res.json({ 79 | todos 80 | }) 81 | }); 82 | 83 | app.listen(3000); --------------------------------------------------------------------------------