How do I force a user to log in?

So I’m using Replit Authentication and creating a Chat Room. Note: Authentication is on. This is my code:

app.get('/', (req, res) => {
  const user = getUserInfo(req);
  if (user) {
    res.render('index.html', { username: user.name });
    console.log(user.name + " joined.");
  } else {
    res.render('index.html', { username: "Guest" + getRandomInt(500) });
    console.log("User not authenticated; Using Guest Process.);
  }
});

For some reason, my webview is logged in, so the username comes up as doxr. When I open the website on a new tab, I’m not signed in and Replit is not giving me the Automatic sign-in page. What do I do?

1 Like

Use @PikachuB2005’s repl-auth package.

Usage

const express=require("express");
const repl_auth=require("repl-auth");
const app=express();
app.use("/", repl_auth);
1 Like

You might want to check if there are the Replit auth headers that tell you who is signed in. Then if they don’t exist, force them to log in. That or you use repl-auth as @9pfs1 suggested.

1 Like

@9pfs1, I don’t understand how to use it, can you show me how to use in my repl: https://replit.com/@doxr/DadCord?v=1

@youngchief There are none here: https://docs.replit.com/hosting/repl-auth-sidebar

There is only

  • id
  • name
  • profileImage
  • bio
  • url
  • roles
  • teams
1 Like

Just look for X-REPLIT prefixed headers.

2 Likes

I believe it’s x-replit-user- as the prefix

1 Like

It’s X-Replit-User-. Pretty sure you need to have it capitalized

2 Likes

Whether it’s capitalized shouldn’t matter, and I believe nodejs or express makes it not capitalized.

Sooo…

function LoginWithReplit() {
  window.addEventListener("message", authComplete);
  var h = 500;
  var w = 350;
  var left = screen.width / 2 - w / 2;
  var top = screen.height / 2 - h / 2;

  var authWindow = window.open(
    "https://replit.com/auth_with_repl_site?domain=" + location.host,
    "_blank",
    "modal =yes, toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=" +
      w +
      ", height=" +
      h +
      ", top=" +
      top +
      ", left=" +
      left
  );

  function authComplete(e) {
    if (e.data !== "auth_complete") {
      return;
    }

    window.removeEventListener("message", authComplete);

    authWindow.close();
    location.reload();
  }
}

app.get('/', (req, res) => {
  const user = getUserInfo(req);
  if (X-Replit-User-) {
    res.render('index.html', { username: user.name });
    console.log(user.name + " joined.");
  } else {
    LoginWithReplit();
  }
});

Please tell me if that will work…

I’m no Node JS expert but uh yeah no that is not going to work. That’s not how you check a header. I think you can do this because that’s how it works in Python Flask but I have not tested it.

app.get('/', (req, res) => {
  const user = getUserInfo(req);
  if (req.headers["X-Replit-User-Id"]) {
    res.render('index.html', { username: user.name });
    console.log(user.name + " joined.");
  } else {
    // LoginWithReplit();
  }
});

Oh and as 9pfs pointed out, LoginWithReplit() won’t work. You could try using SocketIO for that.

2 Likes

You’re trying to run frontend JS in the backend, so no, that will not work. (window won’t work in the backend, the app object won’t exist in the frontend, and X-Replit-User- will throw a syntax error.)

3 Likes

Just something im noticing; incase that two ‘Guests’ have the name number, this could cause conflict if your website relies on the username… try tracking (using database to store guest numbers) the already used gust numbers and keep rolling numbers until one is found.

1 Like

thanks, but I’m not that good with databases

i believe node js supports replit db. Replit DB is incredibly simple to use. Like this simple:

python

import:
from replit import db
import (requires creating a db variable and db url):
from replit import Database

assign:
db['name'] = value
get:
variable = db['name']
safe get:
variable = db.get('name')
delete:
del db['name']

node js

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.