论坛

    • 登录
    • 版块
    • 最新
    • 标签
    • 热门

    用fastapi開發註冊登入頁面,問題求助。

    技术交流
    1
    1
    94
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • eouzoe
      eouzoe 最后由 编辑

      附上代碼:
      import hashlib
      import re

      from pydantic import BaseModel#, EmailStr
      from typing import Union
      from fastapi import FastAPI, HTTPException
      from deta import Deta

      app = FastAPI()

      deta = Deta("a0160j9a_AuyhDiXyZgZRVjUKH92jizzgCQRT8sy5")
      users = deta.Base("users")

      def is_email(email: str) -> bool:
      if re.match(r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$', email):
      return True
      return False

      class RegisterInput(BaseModel):
      username: str
      password: str
      email: str

      class LoginInput(BaseModel):
      username: str
      password: str

      @app.get("/")
      def home():
      return {
      "message":"welcome"
      }

      @app.post("/register")
      async def register(input: RegisterInput):
      if not is_email(input.email):
      raise HTTPException(status_code=400, detail="Invalid email")
      hashed_password = hashlib.sha256(input.password.encode()).hexdigest()
      await users.put({"username": input.username, "email": input.email, "hashed_password": hashed_password})
      return {"message": "Successfully registered"}

      @app.post("/login")
      async def login(input: LoginInput):
      hashed_password = hashlib.sha256(input.password.encode()).hexdigest()
      result = await users.query({"username": input.username}).get()
      if not result:
      raise HTTPException(status_code=400, detail="Incorrect username or password")
      if result["hashed_password"] != hashed_password:
      raise HTTPException(status_code=400, detail="Incorrect username or password")
      return {"message": "Successfully logged in"}
      有許多問題搞不懂,然後不知道該如何著手。

      1 条回复 最后回复 回复 引用 0
      • First post
        Last post