Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Better login system + view links as a real link
Browse files Browse the repository at this point in the history
  • Loading branch information
cinaryilmaz committed Jun 19, 2022
1 parent b3609ad commit 6ab53f7
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 14 deletions.
26 changes: 23 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,34 @@ const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
const session = require('express-session');

app.set('view engine', 'ejs');
app.use(express.urlencoded({ extended: false }));
app.use(session({
secret: 'put-a-really-secret-key-here',
resave: false,
saveUninitialized: true
}));

app.get('/', (req, res) => {
res.render('index');
res.redirect('/login');
});

app.get('/login', (req, res) => {
res.render('login');
});

app.post('/login', (req, res) => {
req.session.username = req.body.username;
res.redirect('/chat');
});

app.get('/chat/:username', (req, res) => {
res.render('chat', { username: req.params.username });
app.get('/chat', (req, res) => {
if (req.session.username == null) {
res.redirect('/login');
}
res.render('chat', { username: req.session.username });
});

app.use(express.static('static'))
Expand Down
94 changes: 94 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"dependencies": {
"ejs": "^3.1.8",
"express": "^4.18.1",
"express-session": "^1.17.3",
"mongodb": "^4.7.0",
"socket.io": "^4.5.1"
}
Expand Down
9 changes: 0 additions & 9 deletions static/script/enter.js

This file was deleted.

1 change: 1 addition & 0 deletions static/script/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function gotMessage(msg, id) {
username.textContent = id;
username.className = "username";
message.textContent = msg;
message.innerHTML = message.innerHTML.replace(/(\b(https?|ftp|file):\/\/([-A-Z0-9+&@#%?=~_|!:,.;]*)[-A-Z0-9+&@#%?\/=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, "<a href='$1' target='_blank'>$1</a>");
message.className = "message";
row.appendChild(username);
row.appendChild(message);
Expand Down
4 changes: 2 additions & 2 deletions views/index.ejs → views/login.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
</tr>
</tbody>
</table>
<form id="form" action="">
<input id="input" autocomplete="off" /><button>Enter</button>
<form id="form" method="post">
<input id="input" autocomplete="off" name="username"/><button>Enter</button>
</form>
</body>

Expand Down

0 comments on commit 6ab53f7

Please sign in to comment.