Dash app login (with flask) not working when I dockerized it

Hello!

I Dockerized my dash app, the app launch well, but when I log in it’s like if the log in didn’t work.
The Username and password are well reconized because it displays the usual error message if login don’t match with database.

But when I log in that drive 0.1 sec on the right page and drive me back on the login page.
That’s the usual process if someone is not log in so I guess changing page disconnect me.

To log in I use the flask module with @login_manager.user_loader, the decorator is well activated and return usual logs.

I don’t get this error on my not Dockerized app

I’m new to Docker so I guess there’s a mistake in my Dockerfile
There it is :

FROM python:3.9.16

EXPOSE 8080
COPY requirements.txt requirements.txt
COPY dash_prototype/ .
RUN pip install -r requirements.txt

CMD [ “gunicorn”, “–workers=5”, “–threads=1”, “-b 0.0.0.0:8080”, “app:server”]

If one of you know how I can resolve this I would be very gratefull!

Hello @Tnecnivs,

It sounds like your cookie might not be saving? I know if I go onto a local server and have https cookies on on the server. If not accessing it in https then the cookie never saves on the client and just continues logging in.

Hello @jinnyzor ! Thanks for the answer,

I’m not familiar with cookies so I don’t really get how to manage the problem.
You mean I should add something that spend cookies on https?

Have you some idea about how I could repair that?

Many thanks

Do you have your code for the log in flow?

For sure, here it is

config

server.config.update(
SECRET_KEY=os.urandom(12),
)

Setup LoginManager pour le serveur

login_manager = LoginManager()
login_manager.init_app(server)
login_manager.login_view = ‘/login’

Creation de la classe User avec UserMixin pour pouvoir se login avec

class Users(UserMixin, Users):
pass

@login_manager.user_loader
def load_user(user_id):
“”"Connexion flask

Args:
    user_id (flask): id_user

Returns:
    session: charge l'utilisateur
"""
session = create_session()
request = session.query(Users).filter(
    Users.username == "admin"
).first()
print("request under")
print(request.username)
print("request above")
return session.query(Users).get(int(user_id))

@callback(
Output(‘url_login’, ‘pathname’)
, [Input(‘login-button’, ‘n_clicks’)]
, [State(‘uname-box’, ‘value’), State(‘pwd-box’, ‘value’)]
,prevent_initial_call = True)
def successful(n_clicks, input1, input2):
“”"Check matching username/password in database

Args:
    n_clicks (dbc.Button): Bouton de connexion déclanchant le callback
    input1 (string): username renseigné
    input2 (string): mot de passe renseigné

Returns:
    pathname: envoie à la page d'acceuil si le login est bon
"""
print("entrée dans connexion")
session = create_session()
is_there_user = len(session.query(Users).all())
if is_there_user == 0:
    hashed_password = generate_password_hash(input2, method='sha256')
    create_user(session, input1, "mailtochange@mail.com", hashed_password, "admin")
user = session.query(Users).filter_by(username=input1).first()
if user:
    if check_password_hash(user.password, input2):
        login_user(user)
        return '/'
    else:
        pass
else:
    pass

In successfull() we well go to "return’/’

And this works on your local just fine?

What about the rest of the application settings? Are there any cookie settings going into the flask server?

Something like this will not allow you access unless through https:

app.config['SESSION_COOKIE_SAMESITE'] = 'Lax'
app.config['SESSION_COOKIE_SECURE'] = 'True'

Yes, on local there’s no problem, I recreate exactly the same environnements, btw I forgot to mention it, but it seems I don’t get exatly the same logs for the local and the container app, like if callback don’t activate in the same way

I have the same answer (until the connexion) but not the same order beetween local and caontainer

No I don’t have any cookies settings, and don’t have more settings I guess, should I? (that’s not a huge app)

Hmm, what happens if you change the port to port 80 or 443?

Hi @jinnyzor thanks for your time

I already used port 80 no? I tried 443 that don’t work either, by curiosity, what could change the port we use?

Ok thanks for the help @jinnyzor , that was a problem with the thread in the dockerfile, I just delete it and it works, thanks again for all the help, and have a nice day!

1 Like