pallets-eco / flask-session

Server side session extension for Flask
https://flask-session.readthedocs.io
BSD 3-Clause "New" or "Revised" License
490 stars 236 forks source link

RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret. #182

Closed jeff13401769 closed 4 months ago

jeff13401769 commented 11 months ago
import os
from flask import Flask, render_template, request, redirect, jsonify, redirect, url_for, session
from flask_session import Session
from flask_cors import *
from flask_redis import FlaskRedis
import requests
import asyncio
from datetime import timedelta
import random

from pymongo import MongoClient
import pymongo
import certifi
from utility.config import config
from cogs.function_in import function_in
import json

app = Flask(__name__, static_folder='templates', static_url_path='/static')
secret_key = os.urandom(24)
app.config['SESSION_TYPE'] = 'REDIS'
app.config['SESSION_USE_SIGNER'] = True
app.config['SECRET_KEY'] = secret_key
app.config['SESSION_PERMANENT'] = True
app.config['PERMANENT_SESSION_LIFETIME'] = 1800
app.config['REDIS_URL'] = os.environ.get("REDIS_TLS_URL")
app.config['REDIS_PASSWORD'] = os.environ.get("REDIS_PASSWORD")
CORS(app, supports_credentials=True)
Session(app)
redis = FlaskRedis(app)

...

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))

Flask==2.3.2 Flask-Cors==4.0.0 Flask-Session==0.5.0 flask-redis==0.4.0 run on heroku and have use Heroku Data for Redis®*

here is my code, I have set secret key, but app reply me [RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret]

Lxstr commented 4 months ago

Did you resolve this? Your code seems to work fine for me. A few newer versions are released since this

Lxstr commented 4 months ago

Ah yes, you have set SESSION_REDIS = "REDIS" it should be "redis". Also the error message is misleading, because previously Flask-Session used a nullsession like Flask, and that was causing an error directly on flask that is completely misleading. This is now fixed from 0.7.0, with specific error.

Lxstr commented 4 months ago

In fact I might pass a .lower filter on the string to avoid this