lewiskimaru commited on
Commit
7fd0b89
1 Parent(s): 36bcfe1

Update modules/app.py

Browse files
Files changed (1) hide show
  1. modules/app.py +0 -84
modules/app.py CHANGED
@@ -10,13 +10,6 @@ from fastapi.middleware.cors import CORSMiddleware
10
  from fastapi.responses import HTMLResponse
11
  import uvicorn
12
 
13
- from pydantic import BaseModel
14
- from pymongo import MongoClient
15
- import jwt
16
- from jwt import encode as jwt_encode
17
- from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
18
- from bson import ObjectId
19
-
20
  import ctranslate2
21
  import sentencepiece as spm
22
  import fasttext
@@ -42,83 +35,6 @@ fasttext.FastText.eprint = lambda x: None
42
  # User interface
43
  templates_folder = os.path.join(os.path.dirname(__file__), "templates")
44
 
45
- # Authentication
46
- class User(BaseModel):
47
- username: str = None # Make the username field optional
48
- email: str
49
- password: str
50
-
51
- # Connect to the MongoDB database
52
- client = MongoClient("mongodb://localhost:27017")
53
- db = client["mydatabase"]
54
- users_collection = db["users"]
55
-
56
- # Secret key for signing the token
57
- SECRET_KEY = "helloworld"
58
- security = HTTPBearer()
59
-
60
- #Implement the login route:
61
-
62
- @app.post("/login")
63
- def login(user: User):
64
- # Check if user exists in the database
65
- user_data = users_collection.find_one(
66
- {"email": user.email, "password": user.password}
67
- )
68
- if user_data:
69
- # Generate a token
70
- token = generate_token(user.email)
71
- # Convert ObjectId to string
72
- user_data["_id"] = str(user_data["_id"])
73
- # Store user details and token in local storage
74
- user_data["token"] = token
75
- return user_data
76
- return {"message": "Invalid email or password"}
77
-
78
- #Implement the registration route:
79
- @app.post("/register")
80
- def register(user: User):
81
- # Check if user already exists in the database
82
- existing_user = users_collection.find_one({"email": user.email})
83
- if existing_user:
84
- return {"message": "User already exists"}
85
- #Insert the new user into the database
86
- user_dict = user.dict()
87
- users_collection.insert_one(user_dict)
88
- # Generate a token
89
- token = generate_token(user.email)
90
- # Convert ObjectId to string
91
- user_dict["_id"] = str(user_dict["_id"])
92
- # Store user details and token in local storage
93
- user_dict["token"] = token
94
- return user_dict
95
-
96
-
97
- #Implement the `/api/user` route to fetch user data based on the JWT token
98
- @app.get("/api/user")
99
- def get_user(credentials: HTTPAuthorizationCredentials = Depends(security)):
100
- # Extract the token from the Authorization header
101
- token = credentials.credentials
102
- # Authenticate and retrieve the user data from the database based on the token
103
- # Here, you would implement the authentication logic and fetch user details
104
- # based on the token from the database or any other authentication mechanism
105
- # For demonstration purposes, assuming the user data is stored in local storage
106
- # Note: Local storage is not accessible from server-side code
107
- # This is just a placeholder to demonstrate the concept
108
- user_data = {
109
- "username": "John Doe",
110
- "email": "johndoe@example.com"
111
- }
112
- if user_data["username"] and user_data["email"]:
113
- return user_data
114
- raise HTTPException(status_code=401, detail="Invalid token")
115
-
116
- #Define a helper function to generate a JWT token
117
- def generate_token(email: str) -> str:
118
- payload = {"email": email}
119
- token = jwt_encode(payload, SECRET_KEY, algorithm="HS256")
120
- return token
121
-
122
 
123
  # Get time of request
124
 
 
10
  from fastapi.responses import HTMLResponse
11
  import uvicorn
12
 
 
 
 
 
 
 
 
13
  import ctranslate2
14
  import sentencepiece as spm
15
  import fasttext
 
35
  # User interface
36
  templates_folder = os.path.join(os.path.dirname(__file__), "templates")
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  # Get time of request
40