Learn to Code via Tutorials on Repl.it!

← Back to all posts
Authenticating users with Repl.it Auth
mat1 (4456)

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

Starting off

We'll start off with a basic Flask template (main.py)

from flask import Flask, render_template, request
app = Flask('app')

def hello_world():
  return render_template('index.html')

app.run(host='', port=8080)


<!doctype html>
	<title>Repl Auth</title>

Nothing interesting yet.

The authentication script

Now, we'll add the authentication script.

	<script authed="location.reload()" src="https://auth.turbio.repl.co/script.js"></script>

This can be placed anywhere in the document body and will create an iframe in its parent element. Additionally, any JavaScript placed in the 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.

The headers

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.

def hello_world():
	return render_template(


	{% if user_id %}
	<h1>Hello, {{ user_name }}!</h1>
	<p>Your user id is {{ user_id }}.</p>
	{% else %}
	Hello! Please log in.
		<script authed="location.reload()" src="https://auth.turbio.repl.co/script.js"></script>
	{% endif %}


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.


Also, be aware that if you're going to be using an accounts system, PLEASE do all the specific logic for checking users on the BACKEND, that means NOT doing it with JavaScript in your HTML. That is all.

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.

amasad (3536)

First person makes something cool with this will get a MAJOR shoutout on the next newsletter.

qualladoom (371)

@mat time for a real life example!?

Coder100 (18873)

How about creating and storing data with the repl.it accounts? @enigma_dev

Scoder12 (881)

@amasad I used to constantly use this bookmarklet script that would allow me to get to a repl quickly, now I made it into a site that anyone can use with repl auth: http://repl-chooser.scoder12.repl.co/

amasad (3536)

@21natzil can you please showcase repl auth and repl mail/chat in the next newsletter? @MrEconomical

MrEconomical (2302)

@amasad thanks for featuring my project! it really means a lot to me!

IMightBeMe (33)

You could use replit’s database. (Tho u prob already know that know lol) @Coder100

mylesbartlett (3)

Yeah, JavaScript logic for checking users is asking for some attacker to come and bypass authentication.

Scoder12 (881)

@mylesbartlett theres no way to circumvent it as repl sends the authentication headers to the server and theres no way to forge them. Content security policies prevent most iframes attacks you can think of, so I would say its pretty secure.

ShivankChhaya (241)

How do you get the user's avatar?

AgastyaSandhuja (170)

Would getting the user's profile picture be

def hello_world():
  return render_template(

or something like that...?

Nayoar (581)

@AgastyaSandhuja I don't think so. If you want a user's avatar you'll probably have to scrape it off their profile page.

RahulChoubey1 (183)

@AgastyaSandhuja os.getenv("REPL_IMAGE") gives the URL


How do you get a persons cycles tho.

JakeHu2020 (32)

Very Cool, but how do you log out?

ruiwenge2 (1106)

@JakeHu2020 you have to delete the cookie

JakeHu2020 (32)

@ruiwenge2 I don't really see a cookie...

JakeHu2020 (32)

@ruiwenge2 now I see it, but I can't change it to an expired date with document.cookie

ruiwenge2 (1106)

@JakeHu2020 just click on it then press delete/backspace

JWZ6 (711)

thank u so much

jimbraga (0)

Thanks so much. I did it.
geometry dash

VulcanWM (2875)

uh so this will save by itself and i dont need cookies or anything to store user?

IMightBeMe (33)

How would I add logout functionality with repl auth? I'm trying to make a password manager in flask and thought that repl auth would be a good choice.

Baconman321 (1103)

Is this really safe? Can it be spoofed? I'm thinking of using my own login system for admin powers and replit auth for everything else.

ch1ck3n (2333)

I have a question, can you do this with django?

ch1ck3n (2333)

@ch1ck3n Never mind i got it


CoolCoderSJ (689)

Im doing the exact same thing with webpy, and i copied the html part as it is, but the page reloads as soon as i click login with replit and the popup login box doesnt go away after auth.

Pixl8 (3)

@CoolCoderSJ Try opening it in a popup, worked for me :D
Check the link for an easy explanation. Replace the two links with your replit auth link.

mollthecoder (34)

Is it possible to do this in express?

hg0428 (196)

An Error occured:
Failed to authenticate :(

InvisibleOne (3200)

That usually happens if you are on safari, what browser are you using? @hg0428

xxpertHacker (931)

Why does the script blindly assume that it was placed in the body, as opposed to the head?

// if it's in the <head> it won't show
selem.parentNode.insertBefore(err, selem);
RayhanADev (2595)

@xxpertHacker i stole turbios code and slightly remake it for my repl auth projects.

xxpertHacker (931)

@RayhanADev Same, I rewrote it entirely as a module.

Helpin Kooiez out and made:

To load:

<script data-replace="your URL" type="module" src="another URL of your choice/signin.js" />
<button data-repl-auth />

If data-replace is present then it redirects to its value, otherwise, it reloads the current page.

And it targets the data-repl-auth element to show the button in the DOM.

Also, we had server-side HTTPS redirects, so we were able to cut out a section of code related to that.

Much nicer overall.

YodaCode (96)

Is there a way to do this in Node.js?

RayhanADev (2595)

@YodaCode yessir! Cookie made two tutorials on it.

AliA6 (0)

Is there a way to update node.js?

ExariumDev09 (0)

Really cool! But, if you were to use Django, what will the code be? I'm still learning Python so it will be helpful.

hello4691 (31)

How do I save the username to a variable with python?

MarcusWeinberger (795)

Hey, I'm working on my own auth provider similar to repl auth and I need some help. How does repl set the X-Replit headers?

MarcusWeinberger (795)

Hey, do you have any idea why this wouldn't be working on an iPhone? I'm getting the error message An error occured Failed to authenticate :( on a plain white screen. This is inside the popup window, by the way.

InvisibleOne (3200)

Probably your browser @MarcusWeinberger
For some reason it hates safari on my computer, but works just fine with firefox

MarcusWeinberger (795)

@InvisibleOne yeah there's an issue with all safari browsers

InvisibleOne (3200)

Yep, safari works great when it does, and sucks when it doesn't. Chrome is pretty good about working with everything, firefox is great too. @MarcusWeinberger

PYer (4015)

Any way to add styling to the button that shows up?

CodeSalvageON (613)

imma use this to burn users at da stake

LiamDonohue (294)

tip from google


JamesGordon1 (151)

How would I make this backend? I need it for this project I'm making: https://repl.it/@JamesGordon1/Simple-chat-V01

AdCharity (1325)

@JamesGordon1 you could probably make a request to repl.it/login but since you don't host a site with flask or something similar I'm not sure how you'd set it up.