38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
from flask import Blueprint, request, jsonify
|
|
from flask_jwt_extended import create_access_token
|
|
from werkzeug.security import generate_password_hash, check_password_hash
|
|
|
|
from database import User, db
|
|
|
|
auth_blueprint = Blueprint('auth', __name__)
|
|
|
|
|
|
@auth_blueprint.post('/register')
|
|
def register_endpoint():
|
|
data = request.json
|
|
login = data.get('login')
|
|
password = data.get('password')
|
|
password_hash = generate_password_hash(password)
|
|
|
|
new_user = User(login=login,
|
|
password_hash=password_hash)
|
|
db.session.add(new_user)
|
|
db.session.commit()
|
|
|
|
return jsonify(ok=True)
|
|
|
|
|
|
@auth_blueprint.post('/login')
|
|
def login_endpoint():
|
|
data = request.json
|
|
print(data)
|
|
login = data.get('login')
|
|
user = User.query.filter_by(login=login).first()
|
|
if not user:
|
|
return jsonify(ok=False, accessToken=''), 401
|
|
password = data.get('password')
|
|
if not check_password_hash(user.password_hash, password):
|
|
return jsonify(ok=False, accessToken=''), 401
|
|
access_token = create_access_token(identity=user.id)
|
|
return jsonify(ok=True, accessToken=access_token)
|