Example #1
0
    def post(self):
        data = parser.parse_args()
        # lookup by username
        current_user = UserModel.find_by_username(data['username'])
        if not current_user:
            return {
                "message": "User {} doesn't exist".format(data['username'])
            }

        user_logging = UserLoggingModel(user_id=current_user.id)
        try:
            user_logging.save_to_db()
        except Exception:
            pass

        if UserModel.verify_hash(data['password'], current_user.password):
            access_token = create_access_token(identity=data['username'])
            refresh_token = create_refresh_token(identity=data['username'])
            return {
                "message": "Logged in as {}".format(current_user.username),
                'access_token': access_token,
                'refresh_token': refresh_token
            }
        else:
            return {"message": "Wrong password"}
Example #2
0
    def post(self):
        data = UserRegister.parser.parse_args()
        item = UserModel.find_by_username(data["username"])
        if item:
            return {"message": "username already exists"}, 400

        user = UserModel(**data)
        user.save()
        return {"message": "user created successfully"}
Example #3
0
    def post(self):
        data = parser.parse_args()
        if UserModel.find_by_username(data['username']):
            return {
                "message": "User {} already exists".format(data['username'])
            }

        new_user = UserModel(username=data['username'],
                             password=UserModel.generate_hash(
                                 data['password']))
        try:
            new_user.save_to_db()
            access_token = create_access_token(identity=data['username'])
            refresh_token = create_refresh_token(identity=data['username'])
            return {
                "message": "User {} was created".format(data['username']),
                'access_token': access_token,
                'refresh_token': refresh_token
            }, 201
        except Exception:
            return {"message": "Something else went wrong while creating"}, 500
def authenticate(username, password):
    user = UserModel.find_by_username(username)
    if user and safe_str_cmp(user.password, password):
        return user
    return None