forked from codecodecoder78/RHDEV-BE-2-flask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AuthAPI.py
33 lines (26 loc) · 1.29 KB
/
AuthAPI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import json
import jwt
from flask import Flask, Blueprint, request,jsonify
from db import datab
from db import profile_auth
auth_api = Blueprint('auth_api',__name__,url_prefix='/auth')
@auth_api.route('/register/POST/<string:username>/<string:password>/', methods=['POST','GET'])
def register_user(username,password):
passwordhash = hash(password)
user_dict = {"username":username, "hashedPassword": passwordhash}
if user_dict["hashedPassword"] == -9223363242168321331:
return jsonify({"message":"failure", "status":"400"})
else:
profile_auth.append(user_dict)
return jsonify({"message":"success", "status":"200"})
@auth_api.route('/login/POST/<string:username>/<string:password>/', methods=['POST','GET'])
def user_login(username,password):
passwordhash = hash(password)
user_dict = {"username":username, "hashedPassword": passwordhash}
match = list(filter(lambda a:a["username"] == username and a["hashedPassword"] ==passwordhash, profile_auth))
try:
if match[0] == user_dict:
token = jwt.encode({"username":username, "hashedPassword": passwordhash}, "secret", algorithm="HS256")
return jsonify({"token":token, "message":"success", "status":"200"})
except:
return jsonify({"message":"failure", "status":"401"})