Implement login via rest_api

This commit is contained in:
Kosh 2023-10-15 12:33:48 +05:30
parent 12ba1760e7
commit 970278f25f
2 changed files with 90 additions and 2 deletions

47
backend/api-docs.txt Normal file
View File

@ -0,0 +1,47 @@
WARNING:-
This api is not yet capable of handling any exceptions that may arise when opened for the first time
_____________________
Login:-
URL:- /login
Method:- POST
Data Type:- Form Data
Data content:- {
user_name: "<user name>",
password: "<password>"
}
Response:-
200 OK:-
Data:- No
Note:- Assume login completed.
400 Bad Request:-
Content Type:- text/plain
Note:- user_name or password not given a Form Data.
Data:- What is missing will be mentioned.
403 Forbidden:-
Content Type:- text/plain
Note:- user_name or password is wrong, can display the response data to user.
Data:- What is wrong will be mentioned
Example:-
formData = new FormData();
formData.append('name', 'John');
formData.append('password', 'John123');
fetch(
"/login",
{
body: formData,
method: "post"
}
);
This will give a response status 200 with no data if login is done.
This will return a 403 if the user gave wrong name or password.
_____________________

View File

@ -1,5 +1,8 @@
from flask import Flask, send_file, abort, Response
from os import path
from flask import Flask, request, send_file, abort, Response
from werkzeug.datastructures import ImmutableMultiDict
from data_handler import DataHandler
app: Flask = Flask(__name__)
@ -15,6 +18,44 @@ def handle_get(url_path: str = "index.html") -> Response:
response: Response = send_file(requested_file_path)
response.status = 200
if path.isfile(requested_file_path):
print('a')
return send_file(requested_file_path)
abort(404)
class QueryHandler:
"""
This is the bridge between the frontend and DataHandler class
"""
__data_handler: DataHandler
@staticmethod
@app.post("/login")
def login() -> Response:
"""
Logs the user in
"""
data: ImmutableMultiDict[str, str] = request.form
if "user_name" not in data:
return Response(
"user_name not given",
400,
content_type="text/plain"
)
if "password" not in data:
return Response(
"password is not given",
400,
content_type="text/plain"
)
try:
QueryHandler.__data_handler = \
DataHandler(data["user_name"], data["password"])
except ValueError as value_error:
return Response(
str(value_error),
403,
content_type="text/plain"
)
return Response(status=200)