Home>
I want to manage user login status from session cookie using firebase authentication and flame.
It seems that set_cookie is not working properly, but I don't know how to solve it.
Referenced site: https://firebase.google.com/docs/auth/admin/manage-cookies
Applicable source codefirebase.auth (). onAuthStateChanged (function (user) {
if (user) {
user.getIdToken (). then (function (data) {
var tokenData = {idToken: data}
$.ajax ({
type: 'POST',
url: '/ sessionLogin',
data: JSON.stringify (tokenData),
contentType: 'application/json',
success: function () {
console.log ("done")
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert ('error');
console.log ("XMLHttpRequest:" + XMLHttpRequest.status);
console.log ("textStatus:" + textStatus);
console.log ("errorThrown:" + errorThrown.message);
}
})
});
}
});
@ app.route ('/ sessionLogin', methods = ['POST'])
def session_login ():
id_token = request.json ['idToken']
decoded_token = auth.verify_id_token (id_token)
uid = decoded_token ['uid']
expires_in = datetime.timedelta (days = 1)
# 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)
print ("response", response)
return response
@ app.route ('/ mypage', methods = ['GET', 'POST'])
def mypage ():
session_cookie = request.cookies.get ('session')
print ("session", session_cookie)
if not session_cookie:
# Session cookie is unavailable.Force user to login.
print ("No session")
return redirect ('/')
# Verify the session cookie.In this case an additional check is added to detect
# if the user's Firebase session was revoked, user deleted/disabled, etc.
try:
decoded_claims = auth.verify_session_cookie (session_cookie, check_revoked = True)
return serve_content_for_user (decoded_claims)
except auth.InvalidSessionCookieError:
# Session cookie is invalid, expired or revoked.Force user to login.
return redirect ('/')
this_year = check_year ()
return render_template ('mypage.html', this_year = this_year)
Error message
response<Response 26 bytes [200 OK]>
Session None
It was confirmed that the uid was correctly taken from the idToken taken from/sessionLogin.
We also confirmed that expires_in was generated properly.
However, the response.set_cookie (....) part isn't working, or request.cookies.get ('session') will be None.
If i am new to programming and don't know about cookie and firebase, please tell us if you need additional information.
If anyone can understand, please let me know.
-
Answer # 1
Related articles
- python - regular expression: sub () cannot replace the matched part
- python - all commands cannot be used with discord bot
- python - cannot deploy to heroku environment error
- python - psycopg2 cannot be read by pycharm
- python - variables cannot be defined
- python - mnist cannot be imported
- python - tfkerasapplication model cannot be downloaded
- python - firebase_admin cannot be imported
- i cannot transition from the sbi securities home screen to the futures trading screen by scraping python + selenium
- python - the library installed by pip cannot be imported no module named'selenium'
- ios - transition screen in authentication with firebase
- python - cannot inherit a class with multiple arguments
- python - discord bot command cannot be started
- python - idle cannot resolve the "rootgeometry" error
- [python] cannot insert or delete to text box
- python - ephem module cannot be imported in vscode
- [rails] google authentication error using devise and omniauth cannot be resolved
- information entered in html in vuejs cannot be sent to python with cgi
- cannot read python dat file
- php - jwt authentication fails and the token cannot be obtained
Related questions
- python : WebSocket transport not available. Install eventlet or gevent and gevent-websocket for improved performance
- How do I pass a variable from template to function? Python
- python : The problem with Flask Restful displaying images
- python : Telegram bot sending messages to all users at a specific time
- python : Running Flask through gunicorn with a shared object
- python : Windows server creating a daemon like on Linux
- python : 500 Error problem
- python : Error: "There is no such table" in flask
I don't know the structure, but if you use Firebase Hosting, you can only use a cookie named
__ session
.Use of cookies
Otherwise, I want to check if the Set-Cookie header is on the response with
/sessionLogin
first.