Learn to Code via Tutorials on Repl.it!

← Back to all posts
How to Repl Auth Express.js
h
Coder100 (2988)

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

Line 2: create an express application
Line 3: Not needed, this was for something else
Line 4: Use pug templates
Line 5: More unnecessary things
Line 8-25:
   Line 8: Get ‘/‘ directory
   Line 10-20: If the user has already logged in, we show template 1, which gives all the headers we know.
   Line 20-24: If the user hasn’t, we show template 2, which suggests the user should log in with repl.it
Line 28: Listen I’m on port 8080

views/index.pug

 Lines 1-13: SEO
 Line 14-28:
    Line 14: Gets user and starts if statement
    Line 15-22: If the user has logged in, show user data
    Line 22-28: Otherwise, prompt user to log in

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

Pictures and summary

Example of data:

You have authed. Welcome Coder100!
- Name: Coder100
- ID: 700842
- roles: self_learner

Summary: View the code here. Live working example here

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

Commentshotnewtop
PDanielY (966)

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

Coder100 (2988)

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

PDanielY (966)

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

Coder100 (2988)

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

PDanielY (966)

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

RohilPatel (799)

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

PDanielY (966)

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

Coder100 (2988)

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

AdCharity (1261)

@Coder100 wait you were in cycle bank?

MatthewDoan1 (336)

Nice! I've been waiting for this.

ChezCoder (1456)

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

mamamia5x (68)

How do you do so without .pug files?

mamamia5x (68)

@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 (2988)

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

ChezCoder (1456)

@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 (2988)

ah remote backends BIG BRAIN @ChezCoder

AgastyaSandhuja (146)

How do you get the profile picture?

Coder100 (2988)

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

{
  "X-Requested-With": "AgastyaSandhuja",
  "Referer": "https://repl.it",
  "Content-Type": "application/json",
  "User-Agent": "Mosaic"
}

@AgastyaSandhuja

RohilPatel (799)

Good job! Upvote if you agree!

PDanielY (966)

Thanks for making this