Skip to content

Commit

Permalink
Added email to users at the backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Mario committed Apr 14, 2024
1 parent 62d7d76 commit 46ca537
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 12 deletions.
1 change: 1 addition & 0 deletions users/authservice/auth-model.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
email: String,
username: String,
password: String,
createdAt: Date,
Expand Down
14 changes: 9 additions & 5 deletions users/authservice/auth-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
}
Expand Down
6 changes: 4 additions & 2 deletions users/authservice/auth-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ let app;

//test user
const user = {
email: '[email protected]',
username: 'testuser',
password: 'testpassword',
};

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()
Expand Down Expand Up @@ -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:"[email protected]" ,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');
Expand Down
4 changes: 4 additions & 0 deletions users/userservice/user-model.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
email: {
type: String,
required: true,
},
username: {
type: String,
required: true,
Expand Down
13 changes: 10 additions & 3 deletions users/userservice/user-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,30 @@ 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 });
return
}

//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,
});
Expand Down
17 changes: 15 additions & 2 deletions users/userservice/user-service.test.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,6 +22,7 @@ afterAll(async () => {
describe('User Service', () => {
it('should add a new user on POST /adduser', async () => {
const newUser = {
email: '[email protected]',
username: 'testuser',
password: 'testpassword'
};
Expand All @@ -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: '[email protected]',
username: 'testuser',
password: 'testpassword'
};
Expand All @@ -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: '[email protected]',
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');
});

});

0 comments on commit 46ca537

Please sign in to comment.