diff --git a/users/authservice/auth-service.js b/users/authservice/auth-service.js index 85ca92ec..924dc512 100644 --- a/users/authservice/auth-service.js +++ b/users/authservice/auth-service.js @@ -28,22 +28,19 @@ app.post('/login', async (req, res) => { try { // Check if required fields are present in the request body try{ - validateRequiredFields(req, ['email', 'username', 'password']); + validateRequiredFields(req, ['username', 'password']); } catch(error){ res.status(400).json({ error : error.message }); return } - const email = req.body.email.toString(); + const email = req.body.username.toString(); const username = req.body.username.toString(); const password = req.body.password.toString(); - let user; - if(username) //Can log in with both - // Find the user by username in the database - user = await User.findOne({ username }) - else + let user = await User.findOne({ username }) + if(!user) //There is no user by that username we may have received an email user = await User.findOne({ email }) // Check if the user exists and verify the password @@ -51,7 +48,7 @@ app.post('/login', async (req, res) => { // 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, email: email}); + res.json({ token: token, username: user.username, email: user.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 28067d5a..7f258f5c 100644 --- a/users/authservice/auth-service.test.js +++ b/users/authservice/auth-service.test.js @@ -7,8 +7,7 @@ let mongoServer; let app; //test user -const user = { - email: 'nice@g.com', +let user = { username: 'testuser', password: 'testpassword', }; @@ -16,7 +15,7 @@ const user = { async function addUser(user){ const hashedPassword = await bcrypt.hash(user.password, 10); const newUser = new User({ - email: user.email, + email: "user@gmail.com", username: user.username, password: hashedPassword, createdAt: new Date() @@ -46,14 +45,21 @@ describe('Auth Service', () => { expect(response.body).toHaveProperty('username', 'testuser'); }); - it('Should show missing field user /login', async () => { + it('Should perform a login operation with email /login', async () => { + user.username = "user@gmail.com"; + const response = await request(app).post('/login').send(user); + expect(response.status).toBe(200); + expect(response.body).toHaveProperty('username', 'testuser'); + }); + + it('Should show missing field username /login', async () => { const response = await request(app).post('/login').send(); expect(response.status).toBe(400); - expect(response.body).toHaveProperty('error', 'Missing required field: email'); + expect(response.body).toHaveProperty('error', 'Missing required field: username'); }); it('Should show invalid credentials /login', async () => { - const user2 = {email:"nice@g.com" ,username:"Hello", password:"world"} + const user2 = {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/webapp/src/components/loginAndRegistration/AddUser.js b/webapp/src/components/loginAndRegistration/AddUser.js index 2324ecab..f5ee2557 100644 --- a/webapp/src/components/loginAndRegistration/AddUser.js +++ b/webapp/src/components/loginAndRegistration/AddUser.js @@ -11,6 +11,7 @@ const AddUser = () => { const navigate = useNavigate(); const apiUrl = (process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000') + "/adduser"; const { t } = useTranslation("global"); + const [email, setEmail] = useState(''); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [repeatPassword, setRepeatPassword] = useState(''); @@ -21,7 +22,7 @@ const AddUser = () => { try { //TODO: Add more validations if(password === repeatPassword){ //User put the same password - const response = await axios.post(apiUrl, { username, password }); + const response = await axios.post(apiUrl, { email, username, password }); console.log("Registered user: " + response.data.username); navigate('/login'); } @@ -40,6 +41,17 @@ const AddUser = () => {