From 46ca5377f49c7858b41e418678c7e450a3fc69fb Mon Sep 17 00:00:00 2001 From: Mister-Mario Date: Mon, 15 Apr 2024 00:25:49 +0200 Subject: [PATCH] Added email to users at the backend --- users/authservice/auth-model.js | 1 + users/authservice/auth-service.js | 14 +++++++++----- users/authservice/auth-service.test.js | 6 ++++-- users/userservice/user-model.js | 4 ++++ users/userservice/user-service.js | 13 ++++++++++--- users/userservice/user-service.test.js | 17 +++++++++++++++-- 6 files changed, 43 insertions(+), 12 deletions(-) diff --git a/users/authservice/auth-model.js b/users/authservice/auth-model.js index 7763b51e..5cdbaf69 100644 --- a/users/authservice/auth-model.js +++ b/users/authservice/auth-model.js @@ -1,6 +1,7 @@ const mongoose = require('mongoose'); const userSchema = new mongoose.Schema({ + email: String, username: String, password: String, createdAt: Date, diff --git a/users/authservice/auth-service.js b/users/authservice/auth-service.js index 0bdbf096..60c425b4 100644 --- a/users/authservice/auth-service.js +++ b/users/authservice/auth-service.js @@ -28,24 +28,28 @@ app.post('/login', async (req, res) => { try { // Check if required fields are present in the request body try{ - validateRequiredFields(req, ['username', 'password']); + validateRequiredFields(req, ['email', 'username', 'password']); } catch(error){ res.status(400).json({ error : error.message }); return } - const { username, password } = req.body; + const { email, username, password } = req.body; - // Find the user by username in the database - const user = await User.findOne({ username }); + let user; + if(username) //Can log in with both + // Find the user by username in the database + user = await User.findOne({ username }) + else + user = await User.findOne({ email }) // Check if the user exists and verify the password if (user && await bcrypt.compare(password, user.password)) { // Generate a JWT token const token = jwt.sign({ userId: user._id }, (process.env.JWT_KEY??'my-key'), { expiresIn: '1h' }); // Respond with the token and user information - res.json({ token: token, username: username}); + res.json({ token: token, username: username, email: email}); } else { res.status(400).json({ error: 'Invalid credentials' }); } diff --git a/users/authservice/auth-service.test.js b/users/authservice/auth-service.test.js index f4a5a82e..28067d5a 100644 --- a/users/authservice/auth-service.test.js +++ b/users/authservice/auth-service.test.js @@ -8,6 +8,7 @@ let app; //test user const user = { + email: 'nice@g.com', username: 'testuser', password: 'testpassword', }; @@ -15,6 +16,7 @@ const user = { async function addUser(user){ const hashedPassword = await bcrypt.hash(user.password, 10); const newUser = new User({ + email: user.email, username: user.username, password: hashedPassword, createdAt: new Date() @@ -47,11 +49,11 @@ describe('Auth Service', () => { it('Should show missing field user /login', async () => { const response = await request(app).post('/login').send(); expect(response.status).toBe(400); - expect(response.body).toHaveProperty('error', 'Missing required field: username'); + expect(response.body).toHaveProperty('error', 'Missing required field: email'); }); it('Should show invalid credentials /login', async () => { - const user2 = {username:"Hello", password:"world"} + const user2 = {email:"nice@g.com" ,username:"Hello", password:"world"} const response = await request(app).post('/login').send(user2); expect(response.status).toBe(400); expect(response.body).toHaveProperty('error', 'Invalid credentials'); diff --git a/users/userservice/user-model.js b/users/userservice/user-model.js index 71d81b5f..e6643ff2 100644 --- a/users/userservice/user-model.js +++ b/users/userservice/user-model.js @@ -1,6 +1,10 @@ const mongoose = require('mongoose'); const userSchema = new mongoose.Schema({ + email: { + type: String, + required: true, + }, username: { type: String, required: true, diff --git a/users/userservice/user-service.js b/users/userservice/user-service.js index 69899c29..6278ce18 100644 --- a/users/userservice/user-service.js +++ b/users/userservice/user-service.js @@ -30,7 +30,7 @@ app.post('/adduser', async (req, res) => { try { // Check if required fields are present in the request body try{ - validateRequiredFields(req, ['username', 'password']); + validateRequiredFields(req, ['email', 'username', 'password']); } catch(error){ res.status(400).json({ error : error.message }); @@ -38,15 +38,22 @@ app.post('/adduser', async (req, res) => { } //Check there is not a user with the same name - const user = await User.findOne({username: req.body.username}); + const userUsername = await User.findOne({username: req.body.username}); - if(user) + //Check there is not a user with the same name + const userEmail = await User.findOne({email: req.body.email}); + + if(userUsername) return res.status(400).json({error : "Username already in use"}) + if(userEmail) + return res.status(400).json({error : "Email already in use"}) + // Encrypt the password before saving it const hashedPassword = await bcrypt.hash(req.body.password, 10); const newUser = new User({ + email: req.body.email, username: req.body.username, password: hashedPassword, }); diff --git a/users/userservice/user-service.test.js b/users/userservice/user-service.test.js index 7b9e1b4f..a691d432 100644 --- a/users/userservice/user-service.test.js +++ b/users/userservice/user-service.test.js @@ -1,5 +1,6 @@ const request = require('supertest'); const { MongoMemoryServer } = require('mongodb-memory-server'); +const { email } = require('asciidoctor-emoji/dist/node/twemoji-map'); let mongoServer; let app; @@ -21,6 +22,7 @@ afterAll(async () => { describe('User Service', () => { it('should add a new user on POST /adduser', async () => { const newUser = { + email: 'Nice@g.com', username: 'testuser', password: 'testpassword' }; @@ -33,11 +35,12 @@ describe('User Service', () => { it('Should show missing field user /adduser', async () => { const response = await request(app).post('/adduser').send(); expect(response.status).toBe(400); - expect(response.body).toHaveProperty('error', 'Missing required field: username'); + expect(response.body).toHaveProperty('error', 'Missing required field: email'); }); it('Should not register user /adduser', async () => { const newUser = { + email: 'Nice2@g.com', username: 'testuser', password: 'testpassword' }; @@ -47,6 +50,16 @@ describe('User Service', () => { expect(response.body).toHaveProperty('error', 'Username already in use'); }); - + it('Should not register user /adduser', async () => { + const newUser = { + email: 'Nice@g.com', + username: 'testuser2', + password: 'testpassword' + }; + + const response = await request(app).post('/adduser').send(newUser); + expect(response.status).toBe(400); + expect(response.body).toHaveProperty('error', 'Email already in use'); + }); });