login almost complete
This commit is contained in:
@@ -1,81 +1,72 @@
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from fastapi import FastAPI, HTTPException, Request, Response
|
||||
#from fastapi import Body, Cookie, File, Form, Header, Path, Query
|
||||
from typing import Optional
|
||||
import uvicorn
|
||||
import pymysql
|
||||
|
||||
class mysql_params:
|
||||
host = "127.0.0.1"
|
||||
user = "yt2mp3_nuxt"
|
||||
db = "yt2mp3_nuxt"
|
||||
pwd = "ultratopsecret,bro"
|
||||
import aiomysql
|
||||
import bcrypt
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
class ConversionData(BaseModel):
|
||||
input_value: str
|
||||
conversion_type: str
|
||||
class db:
|
||||
pool = None
|
||||
|
||||
class LoginData(BaseModel):
|
||||
username: str
|
||||
password: str
|
||||
host = "127.0.0.1"
|
||||
user = "yt2mp3_nuxt"
|
||||
db_name = "yt2mp3_nuxt"
|
||||
pwd = "ultratopsecret,bro"
|
||||
|
||||
class MeData(BaseModel):
|
||||
username: str
|
||||
email: str
|
||||
profile_picture: Optional[str] = None
|
||||
@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(data: ConversionData):
|
||||
"""
|
||||
Converts an input value based on the specified conversion type.
|
||||
"""
|
||||
# Dummy conversion logic
|
||||
if data.conversion_type == "uppercase":
|
||||
converted_value = data.input_value.upper()
|
||||
elif data.conversion_type == "lowercase":
|
||||
converted_value = data.input_value.lower()
|
||||
else:
|
||||
raise HTTPException(status_code=400, detail="Invalid conversion type")
|
||||
|
||||
return {"converted_value": converted_value}
|
||||
async def convert_endpoint(): pass
|
||||
|
||||
|
||||
@app.post("/login")
|
||||
async def login_endpoint(data: LoginData):
|
||||
"""
|
||||
Verifies login credentials.
|
||||
"""
|
||||
# Dummy verification logic
|
||||
if data.username == "user" and data.password == "password":
|
||||
return {"message": "Login successful"}
|
||||
else:
|
||||
raise HTTPException(status_code=401, detail="Invalid credentials")
|
||||
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.
|
||||
"""
|
||||
# Dummy user data
|
||||
user_data = {
|
||||
"username": "example_user",
|
||||
"email": "user@example.com",
|
||||
"profile_picture": "https://example.com/profile.jpg"
|
||||
}
|
||||
return user_data
|
||||
|
||||
@app.post("/me")
|
||||
async def me_post_endpoint(data: MeData):
|
||||
Trusts Nuxt's backend to allow it or not.
|
||||
"""
|
||||
Updates user profile data.
|
||||
"""
|
||||
# Dummy update logic
|
||||
return {"message": "Profile updated successfully", **data.dict()}
|
||||
|
||||
return await fetch_user_info()
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
export default defineEventHandler(async (event) => {
|
||||
const conf = useRuntimeConfig()
|
||||
|
||||
const body = await readBody(event)
|
||||
|
||||
const req = await fetch("http://127.0.0.1:99",
|
||||
if ( !body.email || !body.password )
|
||||
throw createError({ statusCode: 400, message: "Bad form. Use e-mail and password" })
|
||||
|
||||
const req = await fetch(conf.fastapi_url + "/login",
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
|
||||
Reference in New Issue
Block a user