question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use cookie based session like Flask?

See original GitHub issue

I saw in the docs there is a mention of session but I couldn’t find any code example.

Specifically didn’t see how to leverage the app’s secret key and decrypt the message on subsequent requests.

I would like to do the following Flask route in starlette :

Also, is there a way to flash messages like Flask? Trying to migrate a project

@app.route("/login", methods=["GET", "POST"])
def login():
    email = None
    if request.method == "POST":
        #Check authentication in db here...
        session["email"] = request.form.get("email")
        return redirect(url_for("user_home"))
    elif "email" in session:
        return redirect(url_for("user_home"))
    return render_template("public.html", email=email)

@app.route("/home")
def user_home():
    #Not logged in
    if 'email' not in session :
        return redirect(url_for('login'))
    return render_template('home.html')

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
theruzievcommented, Jul 29, 2019

First of all you need register SessionMiddleware, after that you can use request.session https://www.starlette.io/middleware/#sessionmiddleware

0reactions
theruzievcommented, Jan 29, 2020

1. Redirecting (url_for) in my example.


@app.route('/', methods=['GET', 'POST'])
def homepage(request: Request):
    if request.method == "POST":
        url = request.url_for("homepage_get")
        return RedirectResponse(url)

    return PlainTextResponse("GET METHOD")


@app.route('/test', methods=['GET'])
def homepage_get(request: Request):
    return PlainTextResponse('GET METHOD')

2. Flashing message on page. Starlette doesn’t have a flash functionality.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sessions in Flask - OverIQ.com
Session is yet another way to store user-specific data between requests. It works similar to cookies. To use session you must set the...
Read more >
Python Flask: keeping track of user sessions? How to get ...
You can access request cookies through the request.cookies dictionary and set cookies by using either make_response or just storing the ...
Read more >
Authentication with Cookies and Sessions in Flask
In Chrome, you can try this out in the application tab. Go to the "Cookies" section under "Storage", and then tamper with the...
Read more >
Get and set cookies with Flask - Python Tutorial
In Flask, set the cookie on the response object.Use the make_response() function to get the response object from the return value of the...
Read more >
Flask-Session — Flask-Sessions 0.0.4 documentation
Configuration¶ ; SESSION_PERMANENT, Whether use permanent session or not, default to be True ; SESSION_USE_SIGNER, Whether sign the session cookie sid or not,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found