initial commit
This commit is contained in:
1
routes/__init__.py
Normal file
1
routes/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from routes.auth import auth_blueprint
|
||||
44
routes/auth.py
Normal file
44
routes/auth.py
Normal file
@@ -0,0 +1,44 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
from flask_jwt_extended import create_access_token, jwt_required, get_jwt_identity
|
||||
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, method='sha256')
|
||||
|
||||
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), 401
|
||||
password = data.get('password')
|
||||
if not check_password_hash(user.password_hash, password):
|
||||
return jsonify(ok=False), 401
|
||||
access_token = create_access_token(identity=user.id)
|
||||
return jsonify(access_token=access_token)
|
||||
|
||||
|
||||
@auth_blueprint.get('/protected')
|
||||
@jwt_required()
|
||||
def protected_endpoint():
|
||||
print(type(get_jwt_identity()))
|
||||
return 'test'
|
||||
Reference in New Issue
Block a user