Flask-Login在Dash应用中表现不如预期。

huangapple go评论79阅读模式
英文:

Flask-login not working as expected with Dash App

问题

以下是您要翻译的内容:

"我正在构建一个多页面的 Dash 应用,需要登录功能。因此,我选择使用 Flask Login,因为基本的 Dash 认证不符合我的需求。基本上,我根据 if current_user.is_authenticated is False: 来保护我的页面布局,经过一些调试,我可以看到我的登录页面上的登录函数正在工作,"

"现在我有这个函数,它位于我的 index.py 文件中,用于呈现不同的页面布局。"

"但是当我登录并运行第一个函数时,路径仍然对我的已登录用户进行了阻止。有人知道我错在哪里吗?"

"先感谢了。"

英文:

So I am building a multi-page dash app and I need login functionality. Therefore I am going with Flask login as basic dash auth is not what I am looking for. Basically, I am protecting my pages based on. if current_user.is_authenticated is False: don't render the page layouts, after some debugging I can see that my login function on my login page is working,

def load_user(user_id):
    return User.get_id


@login_manager.user_loader
def load_user(user_id):
    return User.get_id


@ app.callback(
    Output('hiddendiv', 'children'),
    [Input('login_button', 'n_clicks')],
    [State('email_input', 'value'),
     State('password_input', 'value')])
def successful(n_clicks, email, password):
    redirect = dcc.Location(pathname="/dashboard", id="someid_doesnt_matter")
    redirect2 = dcc.Location(pathname="/admin", id="someid_doesnt_matter")
    if n_clicks > 0:
        try:
            user = session.query(User).filter(User.email == email).first()
            if user is not None and check_password_hash(user.password, password):
                login_user(user, duration=60*60*24*7)
                print('logged in')
                if current_user.is_admin:
                    print('admin')
                    return redirect2
                return redirect
        except:
            return 'Invalid Login'

Now I have this function which renders the different page layouts, located in my index.py file.

              [Input('url', 'pathname')])
def render_page_ui(pathname):
    if current_user.is_authenticated is False:
        return login.layout, print('not logged in')

    else:
        if pathname == '/':
            return dashboard.layout

        elif pathname == '/dashboard':
            return dashboard.layout

        elif pathname == '/admin':
            return admin.layout

        elif pathname == '/database_admin':
            return database_admin.layout

        else:
            return 'Page not found'

But when I log in and run the first function the paths are still blocked to my now logged-in user. Does anyone have any idea where I am going wrong?

Thanks in advance,

答案1

得分: 1

以下是翻译好的部分:

"After some tinkering and thanks to https://github.com/RafaelMiquelino/dash-flask-login/blob/master/app.py I got it to work.

Changed my render_page_ui function to

 @app.callback(Output('pageContent', 'children'),
              Output('login-required', 'is_open'),
              [Input('url', 'pathname'),
               ])
def render_page_ui(pathname):
    if pathname == '/':
        return login.layout, True

    elif pathname == '/login':
        return login.layout, True

    elif pathname == '/dashboard':
        if current_user.is_authenticated:
            print('logged in!')
            return dashboard.layout, False
        else:
            print('not logged in')
            return login.layout, True
    elif pathname == '/admin':

        if current_user.is_authenticated:
            return admin.layout, False
        else:
            return login.layout, True

    elif pathname == '/database_admin':
        if current_user is_authenticated:
            return database_admin.layout, False
        else:
            return login.layout, True
    else:
        return '404'

I guess the problem for me was that I was checking the current_user.is_authenticated immediately upon the app loading.

I also changed the user_loader to

@login_manager.user_loader
def load_user(user_id):
    return session.query(User).get(user_id)
英文:

After some tinkering and thanks to https://github.com/RafaelMiquelino/dash-flask-login/blob/master/app.py I got it to work.

Changed my render_page_ui function to

 @app.callback(Output('pageContent', 'children'),
              Output('login-required', 'is_open'),
              [Input('url', 'pathname'),
               ])
def render_page_ui(pathname):
    if pathname == '/':
        return login.layout, True

    elif pathname == '/login':
        return login.layout, True

    elif pathname == '/dashboard':
        if current_user.is_authenticated:
            print('logged in!')
            return dashboard.layout, False
        else:
            print('not logged in')
            return login.layout, True
    elif pathname == '/admin':

        if current_user.is_authenticated:
            return admin.layout, False
        else:
            return login.layout, True

    elif pathname == '/database_admin':
        if current_user.is_authenticated:
            return database_admin.layout, False
        else:
            return login.layout, True
    else:
        return '404'

I guess the problem for me was that I was checking the current_user.is_authenticated immediately upon the app loading.

I also changed the user_loader to

@login_manager.user_loader
def load_user(user_id):
    return session.query(User).get(user_id)

huangapple
  • 本文由 发表于 2023年2月9日 00:50:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定