Authenticating users with Repl.it Auth
This tutorial will teach you how to use the Repl.it Auth API.
You are required to know the following before you start:
- Basic knowledge of Python/Flask
- Basic knowledge of Jinja2 (Flask templating)
- Basic knowledge of HTML
We'll start off with a basic Flask template (main.py)
Nothing interesting yet.
The authentication script
Now, we'll add the authentication script.
authed attribute will be executed when the person finishes authenticating, so the current one will just reload when the user authenticates.
If you run it now, you will notice a big Let (your site url) know who you are? with a small version of your profile and an Authorize button.
You can click the button but nothing will happen.
Now, let's make something happen.
Go back to your main.py file; we will be grabbing the Repl.it specific headers for the request and extracting data from them.
The main ones we care about are: X-Replit-User-Id, X-Replit-User-Name, and X-Replit-User-Roles. The username one will probably be the most useful for now.
With this information, we can let our HTML template be aware of them.
Now, run your code. It should display a big Hello, (your username)! along with your user ID.
If you want to port this to other languages or frameworks like NodeJS + Express, just be aware of how you can get specific request headers.
Please upvote my post if you found it helpful :)
If you want it, here is the source code for the basic Repl Auth script demonstrated in this tutorial https://repl.it/@mat1/repl-auth-example.