You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何创建基于Node-Express-PostgreSQL的无密码登录页面?

Oh totally, you can absolutely build this! Most tutorials throw in password checks because that's the standard use case, but skipping them is super straightforward. Let's walk through a simple implementation using Flask (Python backend), basic HTML, and PostgreSQL—feel free to share your existing code if you're using a different stack, and I can help adjust it.

Step 1: Set Up Your PostgreSQL Database

First, create a database and a simple users table that only tracks unique usernames (no password column needed):

-- Connect to your PostgreSQL server first, then run these commands:
CREATE DATABASE simple_login;

\c simple_login;

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    username VARCHAR(50) UNIQUE NOT NULL -- Enforce unique usernames
);
Step 2: Backend Implementation (Flask Example)

We'll build a minimal backend to handle registration (saving usernames) and login (checking if a username exists).

First, install required packages:

pip install flask psycopg2-binary

Then create an app.py file with this code (replace the database credentials with your own):

from flask import Flask, render_template, request, redirect, url_for
import psycopg2
from psycopg2 import OperationalError, IntegrityError

app = Flask(__name__)

# Helper to connect to the database
def get_db_connection():
    conn = psycopg2.connect(
        host='localhost',
        database='simple_login',
        user='your_postgres_user',  # Update this to your PostgreSQL username
        password='your_postgres_password'  # Update this to your PostgreSQL password
    )
    return conn

# Main page with login/register forms
@app.route('/')
def index():
    return render_template('index.html')

# Handle user registration (no password required)
@app.route('/register', methods=['POST'])
def register():
    username = request.form['username'].strip()
    if not username:
        return "Username can't be empty!", 400
    
    try:
        conn = get_db_connection()
        cur = conn.cursor()
        # Insert the username into the database
        cur.execute('INSERT INTO users (username) VALUES (%s)', (username,))
        conn.commit()
        cur.close()
        conn.close()
        return redirect(url_for('welcome', username=username))
    except IntegrityError:
        # Triggered if username is already taken
        return "That username is already in use! Pick another.", 409
    except OperationalError as e:
        return f"Database connection error: {e}", 500

# Handle login - just check if the username exists
@app.route('/login', methods=['POST'])
def login():
    username = request.form['username'].strip()
    if not username:
        return "Username can't be empty!", 400
    
    conn = get_db_connection()
    cur = conn.cursor()
    # Check if the username is in the users table
    cur.execute('SELECT id FROM users WHERE username = %s', (username,))
    user = cur.fetchone()
    cur.close()
    conn.close()
    
    if user:
        return redirect(url_for('welcome', username=username))
    else:
        return "Username not found! Please register first.", 404

# Welcome page after successful login/registration
@app.route('/welcome/<username>')
def welcome(username):
    return f"Hey {username}! You're logged in successfully (no password needed 😉)."

if __name__ == '__main__':
    app.run(debug=True)
Step 3: Frontend Login/Register Form

Create a templates folder in the same directory as app.py, then add an index.html file:

<!DOCTYPE html>
<html>
<head>
    <title>No-Password Login</title>
    <style>
        body { font-family: Arial, sans-serif; max-width: 600px; margin: 2rem auto; padding: 0 1rem; }
        form { margin-bottom: 2rem; padding: 1rem; border: 1px solid #ddd; border-radius: 8px; }
        label { display: block; margin-bottom: 0.5rem; }
        input { width: 100%; padding: 0.5rem; margin-bottom: 1rem; box-sizing: border-box; }
        button { padding: 0.5rem 1rem; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; }
    </style>
</head>
<body>
    <h1>Simple No-Password Login System</h1>
    
    <h2>Register a New Account</h2>
    <form action="/register" method="POST">
        <label for="reg-username">Choose a Username:</label>
        <input type="text" id="reg-username" name="username" required>
        <button type="submit">Register</button>
    </form>
    
    <hr>
    
    <h2>Login to Your Account</h2>
    <form action="/login" method="POST">
        <label for="login-username">Enter Your Username:</label>
        <input type="text" id="login-username" name="username" required>
        <button type="submit">Login</button>
    </form>
</body>
</html>
Step 4: Test the System
  1. Make sure your PostgreSQL server is running, and you've updated the database credentials in app.py
  2. Run the backend: python app.py
  3. Open your browser and go to http://localhost:5000
  4. Try registering a username, then logging in with the same name—you'll see the welcome page immediately. If you try registering a duplicate username or logging in with a non-existent one, you'll get helpful error messages.

The core idea here is super simple:

  • Registration just saves a unique username to the database
  • Login only checks if that username exists in the database

If you already have existing code (like using Node.js, Django, etc.), share it and I can help tweak it to remove password validation and focus solely on username checks.

内容的提问来源于stack exchange,提问作者Skylar-Osborne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 09:35:48