Record#
- The knowledge point learned today is how to use the login verification function of replit.
- I still don't quite understand this feature. It should be in the state of running the code to enable the page in order to run properly, which means the code needs to be started first.
- After enabling, obtain several different user information. For example, X-Replit-User-Name is the username, as well as user ID, group, and so on.
- This exercise is based on the previous 87 days and adds user login verification. Is it unnecessary to use two verifications?
CODE#
main.py#
from flask import Flask, request, redirect, session
from replit import db
import os
from datetime import datetime
app = Flask(__name__)
app.secret_key = os.environ['sission_key']
@app.route('/', methods=['GET'])
def index():
#db['user'] = {'name': 'reno', 'pass': '123'}
uname = request.headers['X-Replit-User-Name']
if uname != "Dengle":
return redirect('/log')
if session.get('name'):
text1 = ""
f = open("index.html", "r")
page = f.read()
f.close()
for key in db.keys():
if key and key != "user":
name = db[key]
text = f"""
</hr>
<h2>{name["title"]}</h2>
<p>{key}</p>
<p>{name["text"]}</p>
"""
text1 += text
page = page.replace("{text}", text1)
return page
else:
return redirect('/log')
@app.route('/', methods=["POST"])
def submit():
text = request.form
ttime = datetime.now()
title = text["title"]
text = text["text"]
db[ttime] = {"title": title, "text": text}
return redirect('/')
@app.route('/log1')
def log1():
return f"page{session.get('name')}"
@app.route('/log')
def log():
f = open("login.html", "r")
page = f.read()
f.close()
return page
@app.route('/login', methods=["POST"])
def login():
user = request.form
vuser = db['user']
if vuser['name'] == user["name"] and vuser['pass'] == user["pass"]:
session["name"] = vuser['name']
print("--")
print(session.get('name'))
print("--")
return redirect("/")
else:
return "NAME OR PASS ERROR!"
app.run(host='0.0.0.0', port=81)