Skip to content

Instantly share code, notes, and snippets.

@cosmic-byte
Created March 29, 2018 12:14
Show Gist options
  • Select an option

  • Save cosmic-byte/af4ac2467401041d6bbbcfc0c1ffb62a to your computer and use it in GitHub Desktop.

Select an option

Save cosmic-byte/af4ac2467401041d6bbbcfc0c1ffb62a to your computer and use it in GitHub Desktop.

Revisions

  1. cosmic-byte created this gist Mar 29, 2018.
    58 changes: 58 additions & 0 deletions auth_helper.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    from app.main.model.user import User
    from ..service.blacklist_service import save_token


    class Auth:

    @staticmethod
    def login_user(data):
    try:
    # fetch the user data
    user = User.query.filter_by(email=data.get('email')).first()
    if user and user.check_password(data.get('password')):
    auth_token = user.encode_auth_token(user.id)
    if auth_token:
    response_object = {
    'status': 'success',
    'message': 'Successfully logged in.',
    'Authorization': auth_token.decode()
    }
    return response_object, 200
    else:
    response_object = {
    'status': 'fail',
    'message': 'email or password does not match.'
    }
    return response_object, 401

    except Exception as e:
    print(e)
    response_object = {
    'status': 'fail',
    'message': 'Try again'
    }
    return response_object, 500

    @staticmethod
    def logout_user(data):
    if data:
    auth_token = data.split(" ")[1]
    else:
    auth_token = ''
    if auth_token:
    resp = User.decode_auth_token(auth_token)
    if not isinstance(resp, str):
    # mark the token as blacklisted
    return save_token(token=auth_token)
    else:
    response_object = {
    'status': 'fail',
    'message': resp
    }
    return response_object, 401
    else:
    response_object = {
    'status': 'fail',
    'message': 'Provide a valid auth token.'
    }
    return response_object, 403