Replit Auth with Python Flask (Using replit.web)

Want to make a Flask site with Replit authentication? Here’s how!

Basic app with auth

Here’s a basic Flask application:

from flask import Flask

app = Flask(__name__)


@app.route('/')
def index():
    return 'Hello!'


if __name__ == '__main__':
    app.run('0.0.0.0', 80)

Now let’s use replit.web to add authentication:

...
from replit import web

...

@app.route('/')
@web.authenticated
def index():
    return f'Hello {web.auth.name}!'

...

If you want a custom login page:

...

@app.route('/')
@web.authenticated('Please log in: <script authed="location.reload()" src="https://auth.util.repl.co/script.js"></script>')
def index():
    ...

User store

replit.web comes with a DB, the user store. Here’s how you can use it:

...
users = web.UserStore()

@app.route('/')
@web.authenticated
def index():
    users.current["visits"] = users.current.get("visits", 0) + 1
    return f"Hello {web.auth.name}! You have visited this page {users.current["visits"]} times.'

...

You can read the blog post about replit.web here and find documentation here.

Thanks for reading! This is a wiki so any members can make changes.

7 Likes

@QwertyQwerty88 It’s not a wiki.

4 Likes

:woman_facepalming:

It got late and I was distracted lol. @Firepup650 made it a wiki though, so thanks.

4 Likes