Skip to content

Commit

Permalink
Login: Finish GUI and error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
stephendade committed Jan 4, 2025
1 parent 3151c6a commit 36ec7a3
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 216 deletions.
45 changes: 14 additions & 31 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,15 @@ app.post('/login', async (req, res) => {
token: token
})
} else {
res.status(401).send({
error: 'Invalid username or password'
})
res.status(401).send(JSON.stringify({error: 'Invalid username or password'}))
}
})
})

// List all users
app.get('/users', authenticateToken, (req, res) => {
userMgmt.getAllUsers().then((users) => {
res.send(users)
res.send(JSON.stringify({users: users}))
})
})

Expand All @@ -197,20 +195,17 @@ app.post('/updateUserPassword', authenticateToken, async (req, res) => {
const { username, password } = req.body;

if (!username || !password) {
return res.status(400).send({
error: 'Username and password are required'
})
//return res.status(400).send({
// error: 'Username and password are required'
//})
res.status(400).send(JSON.stringify({error: 'Username and password are required'}))
}

userMgmt.changePassword(username, password).then((success) => {
if (success) {
res.send({
error: 'User password updated successfully'
})
res.send(JSON.stringify({infoMessage: 'User password updated successfully'}))
} else {
res.status(500).send({
error: 'Error updating user password'
})
res.status(500).send(JSON.stringify({error: 'Error updating user password'}))
}
})
})
Expand All @@ -220,20 +215,14 @@ app.post('/createUser', authenticateToken, async (req, res) => {
const { username, password } = req.body

if (!username || !password) {
return res.status(400).send({
error: 'Username and password are required'
})
return res.status(400).send(JSON.stringify({error: 'Username and password are required'}))
}

userMgmt.addUser(username, password).then((success) => {
if (success) {
res.send({
error: 'User created successfully'
})
res.send(JSON.stringify({infoMessage: 'User created successfully'}))
} else {
res.status(500).send({
error: 'Error creating user'
})
res.status(500).send(JSON.stringify({error: 'Error creating user'}))
}
})
})
Expand All @@ -243,20 +232,14 @@ app.post('/deleteUser', authenticateToken, (req, res) => {
const { username } = req.body

if (!username) {
return res.status(400).send({
error: 'Username is required'
})
return res.status(400).send(JSON.stringify({error: 'Username is required'}))
}

userMgmt.deleteUser(username).then((success) => {
if (success) {
res.send({
error: 'User deleted successfully'
})
res.send(JSON.stringify({infoMessage: 'User deleted successfully'}))
} else {
res.status(500).send({
error: 'Error deleting user'
})
res.status(500).send(JSON.stringify({error: 'Error deleting user'}))
}
})
})
Expand Down
30 changes: 30 additions & 0 deletions server/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,34 @@ describe('Express server', function () {
})
})
})

// Test that unauthorized users cannot access the /users endpoint
describe('GET /users nonauth', function () {
it('should return a 401 status code', function (done) {
chai.request(app)
.get('/users')
.set('Accept', 'application/json')
.end(function (err, res) {
assert.equal(res.status, 401)
assert.equal(err, null)
done()
})
})
})

// Test that login works
describe('POST /login', function () {
it('should return a 200 status code', function (done) {
chai.request(app)
.post('/login')
.send({ username: 'admin', password: 'admin' })
.set('Accept', 'application/json')
.end(function (err, res) {
assert.equal(res.status, 200)
assert.equal(err, null)
done()
})
})
})

})
27 changes: 13 additions & 14 deletions src/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,22 @@ export default function loginPage() {
body: JSON.stringify({ username, password }),
});

const data = await response.json(); // Get the error response from the server

console.log('Login:', data);

// Check if login was successful
if (!response.ok) {
const errorData = await response.json(); // Get the error response from the server
throw new Error(errorData.message || 'Failed to login');
console.log('Login failed:', data);
setErrorMessage(data.error)
} else {
// If login is successful, process the data (e.g., save token)
localStorage.setItem('token', JSON.stringify(data));
window.location.reload();

// Clear any previous error message
setErrorMessage('');
}

// If login is successful, process the data (e.g., save token)
const data = await response.json();
console.log('Login successful:', data);
localStorage.setItem('token', JSON.stringify(data));
window.location.reload();

// Clear any previous error message
setErrorMessage('');

// Do something on successful login (e.g., redirect)
// Example: navigate to another page or store token in localStorage
} catch (error) {
// If an error occurred, set the error message
setErrorMessage(error.message);
Expand Down
Loading

0 comments on commit 36ec7a3

Please sign in to comment.