Dah auth user name extraction!

let say ,

import dash_auth

# Keep this out of source code repository - save in a file or a database

VALID_USERNAME_PASSWORD_PAIRS = {
‘hello’: ‘world’,‘user2’:‘pass2’
}

external_stylesheets = [‘https://codepen.io/chriddyp/pen/bWLwgP.css’]

app = Dash(**name**, external_stylesheets=external_stylesheets)
auth = dash_auth.BasicAuth(
app,
VALID_USERNAME_PASSWORD_PAIRS
)

app.layout = html.Div([
html.H1(‘Welcome to the app’),
html.H3(‘You are successfully authorized’),
dcc.Dropdown([‘A’, ‘B’], ‘A’, id=‘dropdown’),
dcc.Graph(id=‘graph’)
], className=‘container’)

@app.callback(
Output(‘graph’, ‘figure’),
[Input(‘dropdown’, ‘value’)])
def update_graph(dropdown_value):
return {
‘layout’: {
‘title’: ‘Graph of {}’.format(dropdown_value),
‘margin’: {
‘l’: 20,
‘b’: 20,
‘r’: 10,
‘t’: 60
}
},
‘data’: [{‘x’: [1, 2, 3], ‘y’: [4, 1, 2]}]
}

if **name** == ‘**main**’:

It is possible to get the information inside the dash layout which user is logged in?
kindly share your ideas to get the user name inside the layout . I have to show that in web app

You may be able to use the logic within the BasicAuth implementation here: https://github.com/plotly/dash-auth/blob/eea39c691da3eb8fd547af118b5482a3d1403a06/dash_auth/basic_auth.py#LL15C17-L15C17

    ...
    def is_authorized(self):
        header = flask.request.headers.get('Authorization', None)
        if not header:
            return False
        username_password = base64.b64decode(header.split('Basic ')[1])
        username_password_utf8 = username_password.decode('utf-8')
        username, password = username_password_utf8.split(':', 1)
        return self._users.get(username) == password

So you could mirror this by doing something like:

def get_username():
    header = flask.request.headers.get('Authorization', None)
    if not header:
        return None
    username_password = base64.b64decode(header.split('Basic ')[1])
    username_password_utf8 = username_password.decode('utf-8')
    username, password = username_password_utf8.split(':', 1)
    return username

Thank you I’ll try it and inform here

@russellthehippo this is working perfectly Thank you for your consideration

1 Like

Hey glad it worked!