From b522727a701d8b247e639b6bf368fa909f440fb4 Mon Sep 17 00:00:00 2001 From: Nobelf <13517042@std.stei.itb.ac.id> Date: Sun, 17 Apr 2022 22:31:38 +0700 Subject: [PATCH] add authentication --- app.py | 10 +++++++--- front.html | 5 ++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 09e470b..ae8055e 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,5 @@ from flask import Flask -from flask_socketio import SocketIO, send +from flask_socketio import SocketIO, send, disconnect from flask_sqlalchemy import SQLAlchemy from flask import request from difflib import get_close_matches @@ -28,8 +28,12 @@ question = QAPair.query.with_entities(QAPair.question).all() questions = [a.question for a in question] @socketio.on('connect') -def welcomeMessage(): - send("hello there", broadcast = False) +def welcomeMessage(auth): + if auth['token'] == "password123": + send("hello there", broadcast = False) + else: + disconnect() + @socketio.on('message') def handleMessage(msg): diff --git a/front.html b/front.html index 8ef5556..46e9ef4 100644 --- a/front.html +++ b/front.html @@ -7,7 +7,9 @@ <body> <script type="text/javascript"> $(document).ready(function() { - var socket = io.connect('http://127.0.0.1:5000'); + + let password = prompt("Please enter a password"); + var socket = io.connect('http://127.0.0.1:5000', { auth : { token : password } }); socket.on('message', function(msg) { $("#messages").append('<li>'+msg+'</li>') @@ -18,6 +20,7 @@ $("#messages").append('<li>'+$('#myMessage').val()+'</li>') $('#myMessage').val(''); }); + }); </script> -- GitLab