Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to Repl Auth Express.js
Coder100

How to use the Repl.it Authentication API with express.js

TOC

TL;DR

code, example

Introduction

@mat1 Has made an epic tutorial on how to use repl.it authentication!
Sadly... it is with python.py and no offense, but python is pretty hard to use.
That's why I use express.js with node.js!

So, here's my tutorial:

You do what @mat1 did, but different: https://repl.it/talk/learn/Authenticating-users-with-Replit-Auth/23460

I used pug to achieve the same results, and instead of request.headers, you use request.get().

Try logging in!

Examine the code here: https://repl.it/@Coder100/Repl-Auth-Express

There's really not much more to say.

Read @mat1's tutorial and examine my code. Upvote if it worked for you and you could understand the code!

Deriving the API

If you go to Mozilla Firefox, open the repl auth website and type in a random domain, then click on inspect elements, and go to the network tab, fill in the form, you will see something interesting indeed. To get all that interesting headers, you will have to reload the page, as that is how you usually do anything that required updating. Well anyways, the rest is pretty self explanatory but I had to go to the https://expressjs.com documentation to learn how to use res.get().

How the code works

Pretty self explanatory, but if you want to know the gist of it, here you go.
index.js

views/index.pug

If you have any more questions, please comment below with your question!

Pictures and summary

Example of data:

Summary: View the code here. Live working example here

Be sure to vote up this tutorial if you liked it!

Voters
XCode101
OldWizard209
Baconman321
PixiGem
Ravost
sojs
programmertest2
Spotandjake
Wilke000
mollthecoder
Comments
hotnewtop
ChezCoder

i am only sure of two things in life, I am an idiot, and @Coder100 is a coder god.

Coder100

lol :p @ChezCoder

HackermonDev

As amasad said, "The first person to create something with this will get a shoutout in the next newsletter."

Coder100

Yes. Why don't you make something cool with this? @PDanielY

HackermonDev

@Coder100 I would, but I don't know what to create

Coder100

Why don't you use the user ID to save things to jsonstore.io? @PDanielY

HackermonDev

@Coder100 Ok, I guess I'll create that later.

Coder100

Ok, good luck! @PDanielY

RohilPatel

We already are with cyclebank. I'm pretty sure you are already a member. @PDanielY

HackermonDev

@RohilPatel oh no, Vandesm invite me so I could fix API error

RohilPatel

Oh. Whoops: sorry! @PDanielY

Coder100

Cycle bank has nothing to do with repl.it authentication.
You should probably read @amasad 's post @RohilPatel

AdCharity

@Coder100 wait you were in cycle bank?

Coder100

No, what is it? @AdCharity

AdCharity
MatthewDoan1

Nice! I've been waiting for this.

Coder100

Thanks! @MatthewDoan1

MahadKalam

I have a question, couldn't this be spoofed?

Coder100

in what way @MahadKalam

MahadKalam

@Coder100 like, couldn't you change X-Replit-User-Name or the other headers to authenticate as someone else?

Coder100

well try it and see @MahadKalam

codingjlu

req.get, not res.get.

TsunamiOrSumth

O great Coder100, can you help me? (〃 ̄︶ ̄)

Whacko

How do you log out? @Coder100

TalinSharma

There is a cookie called REPL_AUTH, delete it...

@Whacko

YodaCode

Is there a way to use normal HTML files instead of pug? HTML preprocessors are pointless in my opinion, and it would be a lot neater to just use regular HTML and Node.js.

Coder100

@YodaCode yes, you can, what makes you think you can't?

YodaCode

@Coder100 How would one go about doing that? Would I have to add AJAX requests to the auth page when it loads?

Coder100

@YodaCode no, you literally just have the auth button, and then once you log in, you have a backend that would do redirects for you based on the headers

YodaCode

@Coder100 Oh. Thanks.

Coder100

one thing tho, HTML preprocessors are not pointless, they will really speed up your workflow among others @YodaCode

YodaCode

@Coder100 I have one more question. How do you log out using Repl.it?

Coder100

delete all cookies and headers . @YodaCode

YodaCode

@Coder100 ok thats what I thought. thank you

codingjlu

@Coder100 cmon, be nice. He doesn't no what Pug is.

codingjlu

@YodaCode I know it's late but Pug gets turned into HTML. You can do it online: https://pughtml.com/

Coder100

no that is not the point of pug
yes of course it gets turned into HTML, but it is used for templating @codingjlu

codingjlu

@Coder100 oh yeah true dat just use ejs it's better

YodaCode

@codingjlu I do know what Pugjs is. It's not needed for the work I do. A great templating / backend engine is EasyJS (https://easyjs.yodacode.repl.co)

codingjlu

@YodaCode lol what? never heard of that. EJS is better.

YodaCode

@codingjlu You've never heard of it because I made it. Also, how can you say something is better than something you've never used? 🤔

codingjlu

@YodaCode lol idk just joking

[deleted]

Nice! But is it possible to do it with a normal html file? I don't know how pug files work

Coder100

well, ig you could but it definitely wouldn't give you as much flexibility @CarlosRosiles

Whippingdot

Hey Coder100, you know the website we are doing for repl.it.You are in on the project, we are adding repl auth. This is in this repl. I am doing my best to implement the repl auth by myself but if you can try to make it better. Also this is a different repl cause I already shared the other one. The link of the shared post is this @Coder100

mamamia5x

How do you do so without .pug files?

Coder100

what do you mean? @mamamia5x

mamamia5x

@Coder100 I mean, I want to make a HTML5 project that uses your username. How do I use the username and have the system remember it, within HTML5.

Coder100

No, that's unfortunately not possible yet. You need a have a backend of some sort. @mamamia5x

ChezCoder

@Coder100 we can set a route that res.send's the auth info and use an XMLHttpRequest to send a req to this route each time

Coder100

ah remote backends BIG BRAIN @ChezCoder

ChezCoder

@Coder100 lol :D

Coder100
AgastyaSandhuja

How do you get the profile picture?

Coder100

why not use repl.it graphQL
Be sure to set these headers (found on bottom left):

@AgastyaSandhuja

TalinSharma

I don't understand... Could you help?

Using with express.js...

@Coder100
@Coder100

RohilPatel

Good job! Upvote if you agree!

ChezCoder

@RohilPatel up-vote-far-mer

HackermonDev

Thanks for making this

Coder100

You're welcome! @PDanielY