replit auth - 88天 - 在线学python100天

记录

今天继续学习 replit 的 Auth 知识点，上次学习的是在所有页面中使用验证，这次是在指定页面中验证。

这个知识点需要引入 replit 的 js 来实现。

今天的编码练习是在之前 blog 的基础上增加页面验证。用户登录管理页面时进行验证。

CODE

main.py

Copy

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":
print(uname)
return redirect('/log')
if session.get('name'):
text1 = ""
f = open("index.html", "r")
page = f.read()
f.close()
post = f"""

Title:
Text:
submit

"""
for key in db.keys():
if key and key != "user":
name = db[key]
text = f"""

{name["title"]}
{key}
{name["text"]}
"""
text1 += text
page = page.replace("{text}", text1)
if uname == "DengLe":
page = page.replace("{post}", post)
else:
page = page.replace("{post}", uname)
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)