Flask clear session not happening global

290 Views Asked by At

I have been trying to clear the session but it is not getting cleared globally.

Here is my code:

app = Flask(__name__)
app.secret_key = 'xxxxx'

@app.before_request
def make_session_permanent():
    session.permanent = True
    app.permanent_session_lifetime = timedelta(minutes=30)

@app.route('/')
def index():
    session['game'] = 1
    session['level'] = 1
    return render_template('index.html')


@app.route('/level', methods=['POST', 'GET'])
def show_session():
    x = (session.get('game'), session.get('level'))
    return jsonify(x)


@app.route('/clear-session', methods=['POST', 'GET'])
def clear_session():
    session.clear()
    response = {'level': session.get('level')}
    return jsonify(**response)

An this is the JavaScript code snippet that I am using to send AJAX requests:

$.post('http://localhost:8000/level', function(data){console.log(data);})   // returns data

$.post('http://localhost:8000/clear-session', function(data){ console.log(data);}) // returns null as data is cleared

$.post('http://localhost:8000/level', function(data){console.log('session cleared'); console.log(data);})  //returns data again. Session not cleared properly

How can I overcome this and clear the session permanently when I send a request to clear-session?

0

There are 0 best solutions below