Hello
I can’t figure out how to do the same :
@callback(
Output('page-content', 'children'),
Input('url', 'pathname'),
prevent_initial_call=True,
)
def display_page(pathname):
if not flask.session.get('authenticated'):
return login-layout
elif pathname == '/dashboard':
return dash-layout
else:
return "404 Page Error! Please choose a valid URL"
I need to redirect to the authentication page there is a user who has not logged in.
My code:
app.py
from dash import html, register_page, Input, Output, State , no_update, callback, dcc, Dash, page_container
import os
import flask
from dotenv import load_dotenv, find_dotenv
assets_folder = os.path.join(os.getcwd(), 'assets')
app = Dash(__name__, use_pages=True, update_title=None, suppress_callback_exceptions=True, assets_folder=assets_folder)
app.layout = html.Div(
id='main-layout',
children=[
page_container
]
)
if __name__ == '__main__':
app.run(debug=True)
./pages/auth.py:
from dash import html, register_page, Input, Output, State , no_update, callback
import flask
import dash_mantine_components as dmc
from src.db.h_main import check_password
register_page(__name__, path='/auth')
layout = (
html.Div(children=[
html.Div('', className="bg"),
html.Div('', className="bg bg2"),
html.Div('', className="bg bg3"),
html.Div(
className="contentBG",
children=[
dmc.LoadingOverlay(
dmc.Center(
children=[
dmc.Card(
children=[
dmc.CardSection(
children=[
dmc.Text('Вход в дашборд', size='lg'),
dmc.Divider(
variant='dashed',
labelPosition='center',
mb='1.25rem'
),
dmc.Stack(
children=[
dmc.TextInput(label="Login:", id='log'),
dmc.PasswordInput(label="Password:", required=True, error=False, id='pass')
],
spacing='1rem',
id="loading-form",
),
dmc.Group(
[
# dcc.Link(dmc.Text("Don't have an account? Register", color="gray", size='sm'), href='/'),
dmc.Button(
"Login", id="load-button", variant="outline", fullWidth=True
),
],
grow=True,
mt='1.5rem',
noWrap=True,
spacing='apart'
)
],
inheritPadding=True
)
],
withBorder=True,
shadow='xl',
radius='lg',
p='1.5rem',
style={'width': '420px'}
)
]
),
radius='16px',
)
],
)
])
)
How do I make sure that the authentication tab always opens for the user if he is not logged in using register_page and page_container
Thank you for reading and taking your time