73 lines
1.8 KiB
Python
73 lines
1.8 KiB
Python
from fastapi import FastAPI, HTTPException, Request, Response
|
|
#from fastapi import Body, Cookie, File, Form, Header, Path, Query
|
|
from typing import Optional
|
|
import uvicorn
|
|
import aiomysql
|
|
import bcrypt
|
|
|
|
app = FastAPI()
|
|
|
|
class db:
|
|
pool = None
|
|
|
|
host = "127.0.0.1"
|
|
user = "yt2mp3_nuxt"
|
|
db_name = "yt2mp3_nuxt"
|
|
pwd = "ultratopsecret,bro"
|
|
|
|
@staticmethod
|
|
async def init_db_pool():
|
|
db.pool = await aiomysql.create_pool(db.host,db.user,db.pwd,db.db_name)
|
|
|
|
@staticmethod
|
|
async def get_conn():
|
|
return db.pool.acquire()
|
|
|
|
|
|
|
|
async def fetch_user_info(email: str):
|
|
async with await db.get_conn() as conn:
|
|
async with conn.cursor(aiomysql.DictCursor) as cur:
|
|
await cur.execute("SELECT * FROM users WHERE email = %s", (email,))
|
|
return await cur.fetchone()
|
|
|
|
|
|
@app.post("/convert")
|
|
async def convert_endpoint(): pass
|
|
|
|
|
|
@app.post("/login")
|
|
async def login_endpoint(req: Request, res: Response):
|
|
|
|
body = await req.json()
|
|
if sorted(list(body.keys())) != sorted(["email","password"]):
|
|
# ensure it's only email and password
|
|
raise HTTPException(status_code=400)
|
|
try:
|
|
user_data = await fetch_user_info(body["email"])
|
|
if (user_data is None):
|
|
raise HTTPException(status_code=401)
|
|
else:
|
|
pwd = user_data.get("password")
|
|
if (bcrypt.checkpw(body["password"].encode(), user_data.get("password").encode())):
|
|
return user_data
|
|
else:
|
|
raise HTTPException(status_code=401)
|
|
except KeyError:
|
|
raise HTTPException(status_code=400)
|
|
|
|
|
|
@app.get("/me")
|
|
async def me_get_endpoint():
|
|
"""
|
|
Retrieves user profile data.
|
|
|
|
Trusts Nuxt's backend to allow it or not.
|
|
"""
|
|
|
|
return await fetch_user_info()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run(app, host="127.0.0.1", port=8003) |