Download api.py from AhmedShahan/JobSathi_Backend: direct link, hf CLI and curl.
- Browser
- Download file 1.88 kB
-
https://huggingface.co/spaces/AhmedShahan/JobSathi_Backend/resolve/main/api.py
- Command line
-
hf download hf://spaces/AhmedShahan/JobSathi_Backend/api.py
-
curl -L -o api.py https://huggingface.co/spaces/AhmedShahan/JobSathi_Backend/resolve/main/api.py
1.88 kB
| from fastapi import FastAPI | |
| from queries import ( | |
| CHECK_PHONE_NUMBER_EXISTS, | |
| GET_PASSWORD_HASH_BY_PHONE_NUMBER, | |
| INSERT_USER, | |
| ) | |
| from request_model import LoginRequest, RegisterRequest | |
| from utils import get_connection, hash_password, verify_password | |
| app = FastAPI(title="JobSathi API") | |
| def login(request: LoginRequest): | |
| conn = None | |
| try: | |
| conn = get_connection() | |
| cur = conn.cursor() | |
| cur.execute(GET_PASSWORD_HASH_BY_PHONE_NUMBER, (request.phoneNumber,)) | |
| row = cur.fetchone() | |
| if row is None: | |
| return {"response": "Phone Number or Password is Incorrect"} | |
| if not verify_password(request.password, row[0]): | |
| return {"response": "Phone Number or Password is Incorrect"} | |
| return {"response": "Authorized"} | |
| except Exception as e: | |
| return {"response": f"Server error: {str(e)}"} | |
| finally: | |
| if conn is not None: | |
| conn.close() | |
| def register(request: RegisterRequest): | |
| conn = None | |
| try: | |
| conn = get_connection() | |
| cur = conn.cursor() | |
| cur.execute(CHECK_PHONE_NUMBER_EXISTS, (request.phoneNumber,)) | |
| row = cur.fetchone() | |
| if row is not None: | |
| return {"response": "Phone Number already registered"} | |
| hashed_password = hash_password(request.password) | |
| cur.execute(INSERT_USER, (request.phoneNumber, hashed_password)) | |
| conn.commit() | |
| return {"response": "Registered successfully"} | |
| except Exception as e: | |
| return {"response": f"Server error: {str(e)}"} | |
| finally: | |
| if conn is not None: | |
| conn.close() | |
| def root(): | |
| return {"response": "Login Auth API is running"} | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run("api:app", host="0.0.0.0", port=8000, reload=True) |