Skip to content

Commit

Permalink
Merge pull request #3 from dylibso/remove-session-code
Browse files Browse the repository at this point in the history
Remove the session code as its is not needed
  • Loading branch information
nilslice authored Aug 15, 2024
2 parents c7c56cd + 81a10f1 commit 1a8505e
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 46 deletions.
10 changes: 0 additions & 10 deletions client/mount.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,5 @@ import { createRoot } from 'react-dom/client'
import { createElement } from 'react'
import App from './App.jsx'

fetch("/login", {
method: "POST",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
nick: "me"
})
}).then(console.log)

const root = createRoot(document.getElementById('root'))
root.render(createElement(App))
38 changes: 2 additions & 36 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,44 +69,10 @@ export async function main(dev) {
return reply.html()
})

const SESSIONS = new Map();
const USERS = new Map();

server.post('/login', (req, reply) => {
const { nick } = req.body
if (nick === undefined) {
reply.status(403).send({ success: false, reason: 'No nick provided' })
return
}
if (USERS.has(nick)) {
reply.status(403).send({ success: false, reason: 'Cannot login as existing user' })
return
}
let sid;
do {
sid = crypto.randomBytes(16).toString('hex')
} while (SESSIONS.has(sid));
USERS.set(nick, sid)
SESSIONS.set(sid, nick)
reply.header('Set-Cookie', 'sid=' + sid).status(201).send({ success: true })
})

server.post('/messages', async (req, reply) => {
let [key, value] = req.headers.cookie.split("=")
if (key !== 'sid') {
reply.status(403).send({ success: false, reason: 'Cannot send message without being logged in' })
return
}
const sessionUser = SESSIONS.get(value)
if (sessionUser === undefined) {
reply.status(403).send({ success: false, reason: 'Invalid session id' })
return
}
// we're just assuming that they are who they say they are
// that's okay for this demo
let { nick, body } = req.body;
if (sessionUser !== nick) {
reply.status(403).send({ success: false, reason: 'Tried to send a message as another user' })
return
}

// remove trailing spaces
const endre = new RegExp('(&nbsp;)*\\s*(\\<br\\>)*$');
Expand Down

0 comments on commit 1a8505e

Please sign in to comment.