1

I'm building a login form but I got stuck with sessions. When I try to login I get an error "The session is unavailable because no secret key was set". Please, check my code:

init.py (the main file)

from flask import Flask
...
from flask_session import Session

app = Flask(__name__)

app.config['SECRET_KEY'] = 'gfgfgghghgfhgfhgfhgfhfgghghghghghg'
Session(app)

routes.py (of users blueprint)

from flask import render_template, session, url_for, redirect, request, Blueprint
...
from mywebsite.users.forms import (LoginForm, RegisterForm)
from mywebsite import db, bcrypt, app

users = Blueprint('users', "__name__")

@users.route('/login', methods=['GET', 'POST'])
def login():
    loginForm = LoginForm()
    if request.method == 'POST':
        session.pop('user_id', None)

Maybe I should pass the secret key to the users blueprint? If so, how do I do that?

P.S. when I get rid off flask_session it works fine

j_noe
  • 147
  • 1
  • 8

1 Answers1

4
# app.config["SECRET_KEY"] = "..."  # wrong
app.secret_key = "My Secret key"  # right

Flask Documentation for Session

SO Question: Demystify Flask app secret key

EDIT: I have just noticed you are using Flask-Session.

Just a note that flask itself has sessions and you don't necessarily need this extension if you just need any sessions.

See Flask-Session is an extension for Flask that adds support for Server-side Session to your application. and explanation Flask-Session extension vs default session.

Anyways to get Flask-Session running you are additionally required to give it SESSION_TYPE configuration (See secret key not set in flask session, using the Flask-Session extension).

app.config['SESSION_TYPE'] = 'filesystem'
Wereii
  • 330
  • 5
  • 16
  • thank you so much!! Now it works! I thought the default session type would be okay. But I don't know anything about session types so that's why I had issues with flask_session. Thank you once again. You're truly the best! – j_noe Jul 31 '20 at 13:04