@HarishOsthe The part of the code where the storing happens is:
#import dependencies
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc
external_scripts = [
{'src': 'https://cdn.paddle.com/paddle/paddle.js'}
]
app = dash.Dash(__name__, suppress_callback_exceptions=True, external_stylesheets=[dbc.themes.LITERA], external_scripts=external_scripts,
meta_tags=[{'name': 'viewport',
'content': 'width=device-width, initial-scale=1.0'}])
server = app.server
global email #to be used as a variable else in the script
# Input layout, log in form:
email_log_in = dbc.FormGroup(
[
dbc.Label("Email", html_for="email"),
dbc.Input(type="email", id="email-log-in", placeholder="Enter email")
]
)
password_log_in = dbc.FormGroup(
[
dbc.Label("Password", html_for="password"),
dbc.Input(type="password", id="password-log-in", placeholder="Enter password")
]
)
log_in_form = dbc.Form([
email_log_in,
password_log_in,
dbc.Button("Log In", id="btn-log-in", color="secondary", block=True, n_clicks=0),
html.Br(),
html.Div(id="log-in-error")
])
app.layout = html.Div([
html.Div(id='page-content', className='content'),
dcc.Store(id='email', storage_type='memory'),
html.Div(id='dummy_div'),
dcc.Location(id='url', refresh=False),
])
# callback, getting user from Firebase Authentication
@app.callback(
[Output("email", "data"),
Output("log-in-error", "children"),
Output('url_log_in', 'pathname')],
Input("btn-log-in", "n_clicks"),
[State("email-log-in", "value"),
State("password-log-in", "value")]
)
def log_in(n_clicks, inp_email, password):
if n_clicks>0:
import requests
import json
import datetime
if inp_email == None:
return ['', dbc.Alert([html.Abbr("\u26A0"), ' ', 'Please enter your email address'], color='warning', dismissable=True), None]
if password == None:
return ['', dbc.Alert('Please enter your password', color='warning', dismissable=True), None]
payload = {"email":inp_email,
"password":password,
"returnSecureToken": True}
apikey = 'API_firebase_authentication'
r = requests.post('https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key={}'.format(apikey),data=payload)
print(f'imp_email: {inp_email}')
print(type(inp_email))
user_rec = json.loads(r.content)
if r.ok:
# return the email as a global variable
global email
email = str(inp_email or '')
print(f'email: {email}')
print(type(inp_email))
# Get the ID token sent by the client
id_token = user_rec['idToken']
# Set session expiration to 1 days.
expires_in = datetime.timedelta(days=1)
try:
# Create the session cookie. This will also verify the ID token in the process.
# The session cookie will have the same claims as the ID token.
session_cookie = auth.create_session_cookie(id_token, expires_in=expires_in)
response = jsonify({'status': 'success'})
# Set cookie policy for session cookie.
expires = datetime.datetime.now() + expires_in
response.set_cookie(
'session', session_cookie, expires=expires, httponly=True, secure=True)
except Exception:
return [inp_email, None , '/']
return [inp_email, dbc.Alert([html.Abbr("\u2713"), ' ', f'Sucessfully logged in {inp_email}, ', html.A("refresh page \u21BB", href="/", className="alert-link")], color='success'), '/']
elif r.status_code == 400:
if user_rec['error']['message'] == 'EMAIL_NOT_FOUND':
e = f'{inp_email} is not in our database, please sign up first'
elif user_rec['error']['message'] == 'INVALID_PASSWORD':
e = 'Your password is not valid, please enter the correct password'
else:
e = 'Something went wrong, plase retry'
return ['', dbc.Alert([html.Abbr("\u26A0"), ' ', e], color='warning', dismissable=True), None]
return['', '', None]
The results of the print statements are:
imp_email: test@tester.nl
<class 'str'>
email: test@tester.nl
<class 'str'>