Created
June 15, 2022 05:38
-
-
Save twyle/558b90dc9ca994256827a231278e0858 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""This module contains the routes associated with the auth Blueprint.""" | |
from json import JSONDecodeError | |
from flask import Blueprint, jsonify, request | |
from flask_jwt_extended import ( | |
create_access_token, | |
create_refresh_token, | |
get_jwt_identity, | |
jwt_required, | |
) | |
from api.blueprints.auth.models import Admin | |
from ..extensions import app_logger | |
from .helpers import handle_create_admin, handle_get_admin, handle_update_admin | |
auth = Blueprint('default', __name__, template_folder='templates', | |
static_folder='static', url_prefix='/auth') | |
@auth.route('/register', methods=['POST']) | |
def register(): | |
"""Create a new Admin User.""" | |
try: | |
data = request.json | |
except JSONDecodeError as e: | |
print(e) | |
return str(e), 400 | |
else: | |
return handle_create_admin(data) | |
@auth.route('/login', methods=['POST']) | |
def login(): | |
"""Log in a registered Admin.""" | |
email = request.json['email'] | |
password = request.json['password'] | |
admin = Admin.query.filter_by(email=email).first() | |
if admin: | |
if password == admin.password: | |
access_token = create_access_token(admin.id) | |
refresh_token = create_refresh_token(admin.id) | |
admin_data = admin.get_admin() | |
admin_data['access token'] = access_token | |
admin_data['refresh token'] = refresh_token | |
return admin_data, 200 | |
return jsonify({'error': 'Wrong creds'}), 401 | |
@auth.route('/me', methods=['GET']) | |
@jwt_required() | |
def get_admin(): | |
"""Get admin details.""" | |
app_logger.info("Handling a GET request to '/auth/me' route.") | |
admin_id = get_jwt_identity() | |
app_logger.info(f"GET request succeessful. Returning user with id {admin_id}.") | |
return handle_get_admin(admin_id) | |
@auth.route('/me', methods=['PUT']) | |
@jwt_required() | |
def update(): | |
"""Update admin details.""" | |
try: | |
data = request.json | |
admin_id = get_jwt_identity() | |
except JSONDecodeError as e: | |
print(e) | |
return str(e), 400 | |
else: | |
return handle_update_admin(admin_id, data) | |
@auth.route('/me', methods=['DELETE']) | |
@jwt_required() | |
@jwt_required() | |
def delete(): | |
"""Delete admin details.""" | |
app_logger.info("Successfully deleted the admin data.") | |
return jsonify({'hello': 'from template api'}), 200 | |
@auth.route('/admins', methods=['GET']) | |
def get_all_admins(): | |
"""Get all admins.""" | |
admins = Admin.query.all(), 200 | |
app_logger.info("Successfully handled a GET request to the '/admins' route. Returning all the admins.") | |
return jsonify(admins), 200 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment