Home > Enterprise >  Using Flask-HTTPAuth when serving a static folder
Using Flask-HTTPAuth when serving a static folder

Time:11-26

I'm using Flask to serve a static folder:

from flask import Flask, send_from_directory
from flask_httpauth import HTTPBasicAuth

app = Flask(__name__,
            static_url_path='',
            static_folder='html_files')
...

@app.route('/')
@auth.login_required
def send_html_files():
    return send_from_directory('html_files', 'main.html')

I used the first example in Flask-HTTPAuth docs in order to add basic authentication to my website. Just a regular username and password is enough for me.

The problem is that the authentication dialog is not showing when the user go directly to http://localhost:5000/a/b/c (it works on http://localhost:5000/)

What is the proper way of doing this? On the other hand, what is the quick and dirty way?

CodePudding user response:

@app.route('/') matches your root path only.

Try something like this to match every path:

@app.route('/<path:filename>')
@auth.login_required
def send_html_files(filename):  
    return send_from_directory('html_files', filename)
  • Related