2022-03-14 16:10:00 +00:00
|
|
|
import datetime
|
2022-03-14 06:32:16 +00:00
|
|
|
import os
|
|
|
|
|
2022-03-14 16:10:00 +00:00
|
|
|
import jwt
|
2022-03-14 06:32:16 +00:00
|
|
|
from flask import Blueprint, jsonify, request
|
|
|
|
|
2022-03-14 16:36:38 +00:00
|
|
|
from db import db
|
|
|
|
from helper_functions import check_password, hash_password, get_token, extract_token_data
|
|
|
|
from models import User
|
2022-03-14 06:32:16 +00:00
|
|
|
|
2022-03-14 16:10:00 +00:00
|
|
|
users_blueprint = Blueprint('users', __name__, url_prefix='/api')
|
2022-03-14 06:32:16 +00:00
|
|
|
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
|
|
|
|
|
|
|
|
|
2022-03-14 16:10:00 +00:00
|
|
|
@users_blueprint.route('/users', methods=['GET'])
|
2022-03-14 06:32:16 +00:00
|
|
|
def users():
|
|
|
|
res = []
|
|
|
|
for i in User.query.all():
|
|
|
|
res.append(i.as_dict())
|
|
|
|
|
2022-03-14 06:51:24 +00:00
|
|
|
return jsonify({"status": 200, "data": res})
|
2022-03-14 06:32:16 +00:00
|
|
|
|
|
|
|
|
2022-03-14 16:10:00 +00:00
|
|
|
@users_blueprint.route('/login', methods=['POST'])
|
2022-03-14 06:32:16 +00:00
|
|
|
def login():
|
|
|
|
request_data = request.get_json()
|
|
|
|
username = request_data['username']
|
|
|
|
password = request_data['password']
|
|
|
|
|
2022-03-14 06:57:51 +00:00
|
|
|
user = db.session.query(User).filter_by(username=username).first()
|
2022-03-14 06:32:16 +00:00
|
|
|
if check_password(user.password, password):
|
2022-03-14 16:10:00 +00:00
|
|
|
token = jwt.encode({'username': user.username, 'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=45)}, os.getenv('SECRET_KEY'), "HS256")
|
|
|
|
|
|
|
|
return jsonify({"status": 200, "text": "Successfully logged in", "data": token})
|
2022-03-14 06:32:16 +00:00
|
|
|
else:
|
2022-03-14 06:51:24 +00:00
|
|
|
return jsonify({"status": 500, "text": "Unable to login"})
|
2022-03-14 06:32:16 +00:00
|
|
|
|
|
|
|
|
2022-03-14 16:10:00 +00:00
|
|
|
@users_blueprint.route('/logout', methods=['GET'])
|
2022-03-14 06:32:16 +00:00
|
|
|
def logout():
|
|
|
|
# TODO
|
2022-03-14 06:51:24 +00:00
|
|
|
return jsonify({"status": 200, "text": "Successfully logged out"})
|
2022-03-14 06:32:16 +00:00
|
|
|
|
|
|
|
|
2022-03-14 16:10:00 +00:00
|
|
|
@users_blueprint.route('/register', methods=['POST'])
|
2022-03-14 06:32:16 +00:00
|
|
|
def register():
|
|
|
|
request_data = request.get_json()
|
|
|
|
username = request_data['username']
|
|
|
|
password = request_data['password']
|
|
|
|
|
2022-03-14 06:57:51 +00:00
|
|
|
user = db.session.query(User).filter_by(username=username).first()
|
|
|
|
if user is None:
|
2022-03-14 06:51:24 +00:00
|
|
|
# Username doesn't exist yet
|
|
|
|
user = User(
|
|
|
|
username=username,
|
|
|
|
password=hash_password(password),
|
|
|
|
admin=False
|
|
|
|
)
|
|
|
|
db.session.add(user)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return jsonify({"status": 200, "text": "Successfully registered user", "data": user.as_dict()})
|
|
|
|
else:
|
|
|
|
return jsonify({"status": 500, "text": "Username already exist"})
|