|
| 1 | +Sure! Here's the documentation formatted in Markdown for a GitHub README file. If you have the template, start at line 106. |
| 2 | + |
| 3 | +```markdown |
| 4 | +# Repl Auth Template |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +## File Structure |
| 9 | + |
| 10 | +``` |
| 11 | +ReplAuth-Template/ |
| 12 | +├── public/ |
| 13 | +│ ├── login.html |
| 14 | +├── index.js |
| 15 | +├── package.json |
| 16 | +├── package-lock.json |
| 17 | +``` |
| 18 | +
|
| 19 | +## Files |
| 20 | +
|
| 21 | +### `public/login.html` |
| 22 | +
|
| 23 | +```html |
| 24 | +<!DOCTYPE html> |
| 25 | +<html> |
| 26 | +<head> |
| 27 | + <title>Repl Auth Template</title> |
| 28 | +</head> |
| 29 | +<body> |
| 30 | + <h1>Please login to view the home page.</h1> |
| 31 | + <script |
| 32 | + authed="location.reload()" |
| 33 | + src="https://auth.turbio.repl.co/script.js"> |
| 34 | + </script> |
| 35 | +</body> |
| 36 | +</html> |
| 37 | +``` |
| 38 | + |
| 39 | +### `index.js` |
| 40 | + |
| 41 | +```javascript |
| 42 | +const express = require('express'); // Import Express.js |
| 43 | +const app = express(); |
| 44 | +app.use(express.static('public')); // Serve static files from the 'public' directory |
| 45 | + |
| 46 | +// Root route |
| 47 | +app.get('/', function (req, res) { |
| 48 | + if (req.header('X-Replit-User-Id')) { // Check if the user is logged in |
| 49 | + res.redirect(`/home/?user=${req.header('X-Replit-User-Name')}`); // Redirect to home page with user name as query parameter |
| 50 | + } else { |
| 51 | + res.sendFile(__dirname + '/public/login.html'); // Send login page if user is not logged in |
| 52 | + } |
| 53 | +}); |
| 54 | + |
| 55 | +// Home route |
| 56 | +app.get('/home', function (req, res) { |
| 57 | + if (!req.query.user) { // Ensure user parameter is present |
| 58 | + res.redirect('/'); // Redirect to login if user parameter is missing |
| 59 | + } else { |
| 60 | + res.send(`<h1>Hello, ${req.query.user}</h1>`); // Send a personalized greeting to the user |
| 61 | + } |
| 62 | +}); |
| 63 | + |
| 64 | +// Start the server on port 8080 |
| 65 | +app.listen(8080, function () { |
| 66 | + console.log('Server up!'); |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +### `package.json` |
| 71 | + |
| 72 | +```json |
| 73 | +{ |
| 74 | + "name": "repl-auth-template", |
| 75 | + "version": "1.0.0", |
| 76 | + "description": "A simple authentication template using Repl Auth", |
| 77 | + "main": "index.js", |
| 78 | + "scripts": { |
| 79 | + "start": "node index.js" |
| 80 | + }, |
| 81 | + "dependencies": { |
| 82 | + "express": "^4.17.1" |
| 83 | + }, |
| 84 | + "author": "", |
| 85 | + "license": "ISC" |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +## Setup Instructions |
| 90 | + |
| 91 | +1. **Create a New Replit Project:** |
| 92 | + - Go to Replit and create a new Repl using the Node.js template. |
| 93 | + |
| 94 | +2. **Set Up the Project Structure:** |
| 95 | + - Create the necessary directories and files as described above. |
| 96 | + |
| 97 | +3. **Add the HTML Login Page:** |
| 98 | + - Create a file `public/login.html` with the content provided in the `public/login.html` section. |
| 99 | + |
| 100 | +4. **Add the Server Script:** |
| 101 | + - Create a file `index.js` with the content provided in the `index.js` section. |
| 102 | + |
| 103 | +5. **Add the Package Configuration:** |
| 104 | + - Create a file `package.json` with the content provided in the `package.json` section. |
| 105 | + |
| 106 | +6. **Install Dependencies:** |
| 107 | + - Open the Replit shell and run `npm install` to install the Express.js dependency. |
| 108 | + |
| 109 | +7. **Configure Your Replit Project:** |
| 110 | + - Ensure your project is set to run `npm start` to start the server. |
| 111 | + |
| 112 | +8. **Run the Application:** |
| 113 | + - Click the "Run" button in Replit to start the server. |
| 114 | + |
| 115 | +9. **Test the Application:** |
| 116 | + - Open the provided Replit URL to test the application. |
| 117 | + |
| 118 | +10. **Set Redirect URL in Your App:** |
| 119 | + - Use the Replit URL as the redirect URL in your application configuration. For example: |
| 120 | + ``` |
| 121 | + https://your-repl-username.your-repl-name.repl.co/ |
| 122 | + ``` |
| 123 | +
|
| 124 | +## Usage |
| 125 | +
|
| 126 | +- **Login Page:** |
| 127 | + - When a user visits the root URL, they are served the login page (`public/login.html`). |
| 128 | + - The login page includes a script that handles authentication and reloads the page upon successful authentication. |
| 129 | +
|
| 130 | +- **Authentication Handling:** |
| 131 | + - The server checks for the presence of the `X-Replit-User-Id` header to determine if a user is logged in. |
| 132 | + - If the user is logged in, they are redirected to the home page with their username passed as a query parameter. |
| 133 | + - If the user is not logged in, they are served the login page. |
| 134 | +
|
| 135 | +- **Home Page:** |
| 136 | + - The home page (`/home`) displays a personalized greeting using the username passed as a query parameter. |
| 137 | + - If the user parameter is missing, the server redirects back to the login page. |
| 138 | +
|
| 139 | +## Deployment |
| 140 | +
|
| 141 | +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. |
| 142 | +
|
| 143 | +For example, if deploying to Heroku, the redirect URL might be: |
| 144 | +``` |
| 145 | +https://your-app-name.herokuapp.com/ |
| 146 | +``` |
| 147 | +
|
| 148 | +## Summary |
| 149 | +
|
| 150 | +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. |
| 151 | +``` |
| 152 | + |
| 153 | +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. |
0 commit comments