login complete
This commit is contained in:
@@ -1,32 +1,36 @@
|
|||||||
from fastapi import FastAPI, HTTPException, Request, Response
|
from fastapi import FastAPI, HTTPException, Request, Response
|
||||||
#from fastapi import Body, Cookie, File, Form, Header, Path, Query
|
#from fastapi import Body, Cookie, File, Form, Header, Path, Query
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
from pydantic import BaseModel, ValidationError
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
import uvicorn
|
import uvicorn
|
||||||
import aiomysql
|
import aiomysql
|
||||||
import bcrypt
|
import bcrypt
|
||||||
|
#import asyncio
|
||||||
app = FastAPI()
|
|
||||||
|
|
||||||
class db:
|
class db:
|
||||||
pool = None
|
pool = None
|
||||||
|
|
||||||
host = "127.0.0.1"
|
host = "192.168.1.208"
|
||||||
user = "yt2mp3_nuxt"
|
user = "suricata"
|
||||||
db_name = "yt2mp3_nuxt"
|
db_name = "yt2mp3_nuxt"
|
||||||
pwd = "ultratopsecret,bro"
|
pwd = "19_P@NDCATA"
|
||||||
|
|
||||||
@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()
|
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI):
|
||||||
|
db.pool = await aiomysql.create_pool(host=db.host,user=db.user,password=db.pwd,db=db.db_name, minsize=1, maxsize=10)
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
db.pool.close()
|
||||||
|
await db.pool.wait_closed()
|
||||||
|
|
||||||
|
app = FastAPI(lifespan=lifespan)
|
||||||
|
|
||||||
|
|
||||||
async def fetch_user_info(email: str):
|
async def fetch_user_info(email: str):
|
||||||
async with await db.get_conn() as conn:
|
async with db.pool.acquire() as conn:
|
||||||
async with conn.cursor(aiomysql.DictCursor) as cur:
|
async with conn.cursor(aiomysql.DictCursor) as cur:
|
||||||
await cur.execute("SELECT * FROM users WHERE email = %s", (email,))
|
await cur.execute("SELECT * FROM users WHERE email = %s", (email,))
|
||||||
return await cur.fetchone()
|
return await cur.fetchone()
|
||||||
@@ -35,15 +39,41 @@ async def fetch_user_info(email: str):
|
|||||||
@app.post("/convert")
|
@app.post("/convert")
|
||||||
async def convert_endpoint(): pass
|
async def convert_endpoint(): pass
|
||||||
|
|
||||||
|
class register_model(BaseModel):
|
||||||
|
first_name:str
|
||||||
|
last_name:str
|
||||||
|
email:str
|
||||||
|
password:str
|
||||||
|
|
||||||
|
@app.post("/register")
|
||||||
|
async def create_user(req: Request):
|
||||||
|
body = await req.json()
|
||||||
|
|
||||||
|
body = register_model(**body)
|
||||||
|
# Auto exceptions give more verbose to Nuxt.
|
||||||
|
|
||||||
|
#try:
|
||||||
|
# body = register_model(**body)
|
||||||
|
#except ValidationError:
|
||||||
|
# raise HTTPException(status_code=400, detail="Missing / invalid arguments")
|
||||||
|
|
||||||
|
body.password = bcrypt.hashpw(body.password.encode(), bcrypt.gensalt()).decode()
|
||||||
|
async with db.pool.acquire() as conn:
|
||||||
|
async with conn.cursor() as cur:
|
||||||
|
await cur.execute("INSERT INTO users (`first_name`,`last_name`,`email`,`password`) VALUES (%s,%s,%s,%s)", (body.first_name, body.last_name, body.email, body.password))
|
||||||
|
await conn.commit()
|
||||||
|
|
||||||
|
class login_data(BaseModel):
|
||||||
|
email: str
|
||||||
|
password: str
|
||||||
|
|
||||||
@app.post("/login")
|
@app.post("/login")
|
||||||
async def login_endpoint(req: Request, res: Response):
|
async def login_endpoint(req: Request):
|
||||||
|
|
||||||
body = await req.json()
|
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:
|
try:
|
||||||
|
login = login_data(**body)
|
||||||
|
# ensure it's only email and password
|
||||||
user_data = await fetch_user_info(body["email"])
|
user_data = await fetch_user_info(body["email"])
|
||||||
if (user_data is None):
|
if (user_data is None):
|
||||||
raise HTTPException(status_code=401)
|
raise HTTPException(status_code=401)
|
||||||
@@ -55,17 +85,27 @@ async def login_endpoint(req: Request, res: Response):
|
|||||||
raise HTTPException(status_code=401)
|
raise HTTPException(status_code=401)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
raise HTTPException(status_code=400)
|
raise HTTPException(status_code=400)
|
||||||
|
#except ValidationError:
|
||||||
|
# raise HTTPException(status_code=400)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/me")
|
@app.get("/me")
|
||||||
async def me_get_endpoint():
|
async def me_get_endpoint(req: Request):
|
||||||
"""
|
"""
|
||||||
Retrieves user profile data.
|
Retrieves user profile data.
|
||||||
|
|
||||||
Trusts Nuxt's backend to allow it or not.
|
Trusts Nuxt's backend to allow it or not.
|
||||||
"""
|
"""
|
||||||
|
args = dict(req.query_params)
|
||||||
|
#print(args)
|
||||||
|
try:
|
||||||
|
me = await fetch_user_info(args["email"])
|
||||||
|
if (me is None):
|
||||||
|
raise HTTPException(404, "User doesn't exist")
|
||||||
|
else: return me
|
||||||
|
except KeyError:
|
||||||
|
raise HTTPException(400, "Please provide email")
|
||||||
|
|
||||||
return await fetch_user_info()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user