Skip to content

This Replit is used for user authentication, particularly to create redirect URL’s for applications.

Notifications You must be signed in to change notification settings

Cybersoulja/replitAuth

Repository files navigation

Sure! Here's the documentation formatted in Markdown for a GitHub README file. If you have the template, start at line 106.

# Repl Auth Template

This Repl Auth Template provides a simple way to handle user authentication and redirection using Replit's infrastructure. It includes an Express.js server and a basic HTML login page. Users are redirected to a personalized home page upon successful authentication.

## File Structure

ReplAuth-Template/ ├── public/ │ ├── login.html ├── index.js ├── package.json ├── package-lock.json


## Files

### `public/login.html`

```html
<!DOCTYPE html>
<html>
<head>
    <title>Repl Auth Template</title>
</head>
<body>
    <h1>Please login to view the home page.</h1>
    <script
        authed="location.reload()"
        src="https://auth.turbio.repl.co/script.js">
    </script>
</body>
</html>

index.js

const express = require('express'); // Import Express.js
const app = express();
app.use(express.static('public')); // Serve static files from the 'public' directory

// Root route
app.get('/', function (req, res) {
    if (req.header('X-Replit-User-Id')) { // Check if the user is logged in
        res.redirect(`/home/?user=${req.header('X-Replit-User-Name')}`); // Redirect to home page with user name as query parameter
    } else {
        res.sendFile(__dirname + '/public/login.html'); // Send login page if user is not logged in
    }
});

// Home route
app.get('/home', function (req, res) {
    if (!req.query.user) { // Ensure user parameter is present
        res.redirect('/'); // Redirect to login if user parameter is missing
    } else {
        res.send(`<h1>Hello, ${req.query.user}</h1>`); // Send a personalized greeting to the user
    }
});

// Start the server on port 8080
app.listen(8080, function () {
    console.log('Server up!');
});

package.json

{
  "name": "repl-auth-template",
  "version": "1.0.0",
  "description": "A simple authentication template using Repl Auth",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "express": "^4.17.1"
  },
  "author": "",
  "license": "ISC"
}

Setup Instructions

  1. Create a New Replit Project:

    • Go to Replit and create a new Repl using the Node.js template.
  2. Set Up the Project Structure:

    • Create the necessary directories and files as described above.
  3. Add the HTML Login Page:

    • Create a file public/login.html with the content provided in the public/login.html section.
  4. Add the Server Script:

    • Create a file index.js with the content provided in the index.js section.
  5. Add the Package Configuration:

    • Create a file package.json with the content provided in the package.json section.
  6. Install Dependencies:

    • Open the Replit shell and run npm install to install the Express.js dependency.
  7. Configure Your Replit Project:

    • Ensure your project is set to run npm start to start the server.
  8. Run the Application:

    • Click the "Run" button in Replit to start the server.
  9. Test the Application:

    • Open the provided Replit URL to test the application.
  10. Set Redirect URL in Your App:

    • Use the Replit URL as the redirect URL in your application configuration. For example:
      https://your-repl-username.your-repl-name.repl.co/
      

Usage

  • Login Page:

    • When a user visits the root URL, they are served the login page (public/login.html).
    • The login page includes a script that handles authentication and reloads the page upon successful authentication.
  • Authentication Handling:

    • The server checks for the presence of the X-Replit-User-Id header to determine if a user is logged in.
    • If the user is logged in, they are redirected to the home page with their username passed as a query parameter.
    • If the user is not logged in, they are served the login page.
  • Home Page:

    • The home page (/home) displays a personalized greeting using the username passed as a query parameter.
    • If the user parameter is missing, the server redirects back to the login page.

Deployment

If you plan to deploy this to a live server, ensure that your server is configured to handle SSL (HTTPS) and that the redirect URLs in your apps are updated to use the live server's URL.

For example, if deploying to Heroku, the redirect URL might be:

https://your-app-name.herokuapp.com/

Summary

By following these steps, you can use this template to handle authentication and redirection for your apps on Replit, ensuring a seamless authentication flow for your users.


This Markdown file can be saved as `README.md` in your project root directory and will serve as documentation for anyone using your Repl Auth Template.

About

This Replit is used for user authentication, particularly to create redirect URL’s for applications.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published