Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret. Flask/Heroku

The flask app can login and register all fine on localhost. But this becomes an issue when i push it to heroku. It shows the above mentioned error. Here's the app.py code

from flask import Flask, render_template, request, redirect, jsonify, url_for, flash
from sqlalchemy import create_engine, asc, desc
from sqlalchemy.orm import sessionmaker
from database_setup import Base, User, BlogPost
from flask import session as login_session
import random
import string
from wtforms import Form, BooleanField, TextField, PasswordField, validators
from passlib.hash import sha256_crypt


app = Flask(__name__)


#Connecting to database
engine = create_engine('sqlite:///travellerdata.db')
Base.metadata.bind = engine

DBSession = sessionmaker(bind=engine)
session = DBSession()

And ends with...

if __name__ == "__main__":
    app.secret_key = 'some secret key'
    app.debug = True
    app.run()
like image 611
Flyn Sequeira Avatar asked Feb 26 '16 17:02

Flyn Sequeira


People also ask

How do I change the secret key for a session in Flask?

Generate the Secret Key Using Different Ways in Flask and Python. To access a session ID, you need to use an encryption key assigned to the SECRET_KEY variable, so at the time, we set the value of the SECRET_KEY variable as a string is extremely dangerous. This key needs to be randomly generated.

What is secret key in Flask session?

Each Flask web application contains a secret key which used to sign session cookies for protection against cookie data tampering. It's very important that an attacker doesn't know the value of this secret key. Your application is using a weak/known secret key and Acunetix managed to guess this key.

What is Flask session?

Flask-Session is an extension for Flask that supports Server-side Session to your application. The Session is the time between the client logs in to the server and logs out of the server. The data that is required to be saved in the Session is stored in a temporary directory on the server.

Why is my session unavailable in flask?

flask runtimeerror: the session is unavailable because no secret key was set. set the secret_key on the application to something unique and secret. runtimeerror: the session is unavailable because no secret key was set. set the secret_key on the application to something unique and secret.

Why is Python session unavailable?

python - RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret - Stack Overflow I am making Flask app.

Why is the session unavailable in Salesforce?

Code Answer The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret. Whatever answers related to “The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.” No application encryption key has been specified.

Is it possible to set a secret_key for the session type?

It states that you must set SESSION_TYPE and only lists options where the session is stored server-side. But that is not [currently] correct. If you succeed in setting a SECRET_KEY then the default session type will store the session in an encrypted cookie (not server side).


1 Answers

I have the same issue when I use flask-login to generate a session ID, it works fine when I directly run it but will output error when I use HTTP server. The original code is like:

if __name__ == "__main__":
    app.secret_key = os.urandom(24)
    app.run()

Then I moved app.secret_key = os.urandom(24) out of __name__ and put it under app = Flask(__name__) like this:

app = Flask(__name__)
app.secret_key = os.urandom(24)

login_manager = flask_login.LoginManager()
login_manager.init_app(app)

And it works fine now.

like image 53
Yibei Huang Avatar answered Sep 18 '22 17:09

Yibei Huang