def authenticate_user():
    try:
        data = request.get_json()
        if data.get('email'):
            current_user = User.find_by_email(data['email'])
        elif data.get('username'):
            current_user = User.find_by_username(data['username'])
        if not current_user:
            return response_with(resp.SERVER_ERROR_404)
        if current_user and not current_user.isVerified:
            return response_with(resp.BAD_REQUEST_400)
        if User.verify_hash(data['password'], current_user.password):
            access_token = create_access_token(identity=current_user.username)
            return response_with(resp.SUCCESS_200,
                                 value={
                                     'message':
                                     'Logged in as {} '.format(
                                         current_user.username),
                                     "access_token":
                                     access_token
                                 })
        else:
            return response_with(resp.UNAUTHORIZED_401)
    except Exception as e:
        print(e)
        return response_with(resp.INVALID_INPUT_422)
def authenticate_user(db, email: str, password: str):
    user = get_user(db, email)
    if not user:
        return False
    if not User.verify_hash(password, user.password):
        return False
    return user
示例#3
0
def authenticate_user():
    try:
        data = request.get_json()
        if data.get('email'):
            current_user = User.find_by_email(data['email'])
        elif data.get('username'):
            current_user = User.find_by_username(data['username'])
        if not current_user:
            return response_with(resp.SERVER_ERROR_404)

        # Comment from this line if you don't want use email.
        if current_user and not current_user.isVerified:
            return jsonify(message='User is not verified'), 403
        #Comment to this line if you don't want use email.

        if User.verify_hash(data['password'], current_user.password):
            access_token = create_access_token(
                identity=current_user.id)  #,   expires_delta = False)
            return response_with(resp.SUCCESS_200, \
                                 value={'message': 'Logged in as {}'.format(current_user.username), \
                                        "access_token": access_token})
        else:
            return response_with(resp.UNAUTHORIZED_401)
    except Exception as e:
        print(e)
        return response_with(resp.INVALID_INPUT_422)
示例#4
0
def authenticate_user():
    try:
        data = request.get_json()
        if data.get('email'):
            current_user = User.find_by_email(data.get('email'))
        elif data.get('username'):
            current_user = User.find_by_username(data.get('username'))
        if not current_user:
            return response_with(resp.SERVER_ERROR_404)
        if current_user and not current_user.is_verified:
            return response_with(resp.BAD_REQUEST_400)
        if User.verify_hash(data.get('password'), current_user.password):
            access_token = create_access_token(identity=data.get('username'))
            user_schema = UserSchema()
            user = user_schema.dump(current_user)
            return response_with(resp.SUCCESS_200,
                                 value={
                                     'message':
                                     'Logged in as {}'.format(
                                         current_user.username),
                                     'access_token':
                                     access_token,
                                     'user':
                                     user
                                 })
        else:
            return response_with(resp.UNAUTHORIZED_401,
                                 value={'message': 'Password is wrong'})
    except Exception as e:
        print(e)
        return response_with(resp.INVALID_INPUT_422)
示例#5
0
def autehticate_user():
    # try :
    data = request.get_json()
    current_user = User.query.filter_by(username=data['username']).first()
    if not current_user :
        return response_with(resp.SERVER_ERROR_404)
    if User.verify_hash(data['password'],current_user.password):
        access_token = create_access_token(identity = data['username'])
        return response_with(resp.SUCCESS_201, value={'message': 'Logged in as {}'.format(current_user.username), "access_token": access_token})
    else:
        return response_with(resp.UNAUTHORIZED_401)
示例#6
0
def authenticate_user():
    try:
        data = request.get_json()
        current_user = User.find_by_username(data['username'])
        if not current_user:
            return response_with(resp.INVALID_INPUT_422)
        if User.verify_hash(data['password'], current_user.password):
            access_token = create_access_token(identity=data['username'])
            return response_with(resp.SUCCESS_200,
                                 value={
                                     'message': 'Successfully logged',
                                     'access_toke': access_token
                                 })
        else:
            return response_with(resp.UNAUTHORIZED_401)
    except Exception as e:
        return response_with(resp.INVALID_INPUT_422)
def authenticate_user():
    try:
        data = request.get_json()
        current_user = User.find_by_username(data['username'])
        if not current_user:
            return response_with(resp.SERVER_ERROR_404)
        if User.verify_hash(data['password'], current_user.password):
            access_token = create_access_token(identity=data['username'])
            return response_with(resp.SUCCESS_201,
                                 value={
                                     'message':
                                     'Logged in as {}'.format(
                                         current_user.username),
                                     "access_token":
                                     access_token
                                 })
        else:
            return response_with(resp.UNAUTHORIZED_401)
    except Exception as e:
        print e
        return response_with(resp.INVALID_INPUT_422)
示例#8
0
def authenticate_user():
    """
    User Login
    ---
    parameters:
        - in: body
          name: body
          schema:
            id: UserLogin
            required:
                - password
                - username
            properties:
                username:
                    type: string
                    description: username of the user
                    default: "test1"
                password:
                    type: string
                    description: Password of the user
                    default: "123456"
            responses:
                200:
                    description: User successfully logged In
                    schema:
                    id: UserLoggedIn
                    properties:
                        code:
                        type: string
                        message:
                        type: string
                        value:
                        schema:
                            id: UserToken
                            properties:
                                access_token:
                                    type: string
                                code:
                                    type: string
                                message:
                                    type: string
                401:
                    description: Invalid input arguments
                    schema:
                        id: invalidInput
                        properties:
                            code:
                                type: string
                            message:
                                type: string
    """

    try:
        data = request.get_json()
        if data.get("email"):
            current_user = User.find_by_email(data["email"])
        elif data.get("username"):
            current_user = User.find_by_username(data["username"])
        if not current_user:
            return response_with(resp.SERVER_ERROR_404)
        if current_user and not current_user.isVerified:
            return response_with(resp.BAD_REQUEST_400)
        if User.verify_hash(data["password"], current_user.password):
            access_token = create_access_token(identity=current_user.username)
            return response_with(
                resp.SUCCESS_200,
                value={
                    "message": "Logged in as admin",
                    "access_token": access_token
                },
            )
        else:
            return response_with(resp.UNAUTHORIZED_401)
    except Exception as e:
        print(e)
        return response_with(resp.INVALID_INPUT_422)