My multi-page callback, that based on location picks right layout and places it in children of html.Div(id='page') first checks if user is authenticated, if not, it returns login layout by default.
and match pathname of location to url, if it matches pick layout. You could also set URL’s as dictionary keys as I am not doing it for different reasons.
The callback is then:
@app.callback(
Output('page', 'children'),
[Input('location', 'pathname')])
def display_content(pathname: str):
if pathname is None:
return html.Div()
if current_user.is_authenticated:
matched = [c for c in pages.keys()
if re.fullmatch(pages[c]['url'], pathname)]
if matched:
page_content = pages[matched[0]]['layout']
else:
page_content = [
html.H1("Not found"),
html.P("Requested URL '{}' was not found".format(pathname))
]
content = html.Div(page_content)
else:
content = pages['login']['layout']
return content
You can check if user is authenticated and has required role for selected url. Allowed roles could be kept in the dict as a list.