Skip to content

Commit 18faa50

Browse files
committed
fix message order, account editing and deletion
1 parent 50f56f3 commit 18faa50

File tree

3 files changed

+8
-12
lines changed

3 files changed

+8
-12
lines changed

controller/auth.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,7 @@ const editAccount = async (req, res) => {
140140

141141
const session = req.cookies.session // Get session from browser cookies
142142

143-
const id = await getIdFromSession(session) // Get user ID from session
144-
145-
const { username: oldUsername, password: oldHash } = await getUserDetails(id) // Get current details about user from database
143+
const { id, username: oldUsername, password: oldHash } = await getUserDetails(session) // Get current details about user from database
146144

147145
// Check if password given as verification is correct
148146
const verifyPasswordCorrect = await bcrypt.compare(verifyPassword, oldHash)
@@ -157,7 +155,7 @@ const editAccount = async (req, res) => {
157155
const password = req.body.password /// Get plaintext password
158156

159157
// Check if new username is not already taken by another account
160-
const usernameTaken = await accDb.get("SELECT * FROM users WHERE username = ?", username)
158+
const usernameTaken = await db.get("SELECT * FROM users WHERE username = ?", username)
161159

162160
if (usernameTaken && oldUsername !== username) { // Username is already in use and it's not the user's current username
163161
res.status(409).end("Username already in use.")
@@ -189,9 +187,7 @@ const deleteAccount = async (req, res) => {
189187

190188
const session = req.cookies.session // Get session from cookie
191189

192-
const id = await getIdFromSession(session) // Get ID from session
193-
194-
const { password: hash } = await getUserDetails(id) // Get account password as bcrypt hash
190+
const { id, password: hash } = await getUserDetails(session) // Get account password as bcrypt hash
195191

196192
// Check that password for verification is correct
197193
const verifyPasswordCorrect = await bcrypt.compare(verifyPassword, hash)

handler/chat.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const getChatLog = async (socket) => {
2424
driver: Database
2525
})
2626

27-
const messages = await db.all("SELECT * FROM chatlog ORDER BY id ASC LIMIT 300") // Obtain all information about the latest 300 messages
27+
const messages = await db.all("SELECT * FROM chatlog ORDER BY id DESC LIMIT 300") // Obtain all information about the latest 300 messages
2828
await db.close()
2929

3030
socket.emit('receive chatlog', messages) // Send the messages to the new client

public/scripts/home.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,18 @@ socket.on('receive message', message => {
4444

4545
// Handling the receiving of chatlog for new connections
4646
socket.on('receive chatlog', messages => {
47-
messages.forEach(message => { // Go through each message of chatlog
48-
const { username, content } = message
47+
for (let i = messages.length-1; i > 0; i--) { // Go through each message of chatlog from last (oldest) to first (newest)
48+
const { id, username, content } = messages[i]
4949

5050
// Add message to chatlog
5151
let html = $('.home__container__chat-view__chatlog').html()
5252
html += `
5353
<div class="home__container__chat-view__chatlog__message">
54-
<strong>${username}:</strong> ${content}
54+
<strong>${id}. ${username}:</strong> ${content}
5555
</div>
5656
`
5757
$('.home__container__chat-view__chatlog').html(html)
58-
})
58+
}
5959
})
6060

6161
// Handling the receiving of users list for new connections

0 commit comments

Comments
 (0)