Exemplo n.º 1
0
def login_route():
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')

        if not UserService.authenticate(username, password):
            flash('Invalid user key or password. Please try again.', 'danger')
            current_app.logger.warn(
                "Invalid credentials attempted for username '{0}'.".format(
                    username))

            return redirect(url_for('auth.login_route'))

        # Find or create user
        user = UserService.find_by(username=username, fetch_one=True)
        if user:
            UserService.update(user.id, authenticated=True)
        else:
            user = UserService.create(username=username, authenticated=True)

        # Log in user in Flask
        login_user(user)

        # Go to next page if defined
        next_page = session.pop('next') or request.args.get('next')
        if next_page:
            return redirect(next_page)

        return redirect(url_for('auth.login_route'))
    else:
        if current_user.is_authenticated:
            return redirect(url_for('user.me_route'))
        else:
            return render_template('pages/auth/authenticate.html')
class UserController(Resource):
    def __init__(self, *args, **kwargs):
        self.user_service = UserService()

    def get(self):
        users = self.user_service.fetch()
        if not users:
            return {'data': [], 'message': 'No se encontraron usuarios'}, 404
        return {'data': users, 'message': 'success'}, 200

    def post(self):
        body = request.get_json()

        data = self.user_service.create(**body)
        if not data:
            return {'message': 'Ocurrio un error al insertar'}, 404
        return {'data': data, 'message': 'Success'}, 200