Spaces:
Sleeping
Sleeping
Commit ·
10fe951
1
Parent(s): 1a3a466
added additional parameters for token generation
Browse files
app/services/merchant_services.py
CHANGED
|
@@ -211,12 +211,26 @@ async def login_service(identifier: str, otp: str) -> dict:
|
|
| 211 |
otp_data = await get_otp(f"otp:login:{identifier}")
|
| 212 |
if not otp_data or otp_data.get("otp") != otp:
|
| 213 |
raise HTTPException(status_code=400, detail="Invalid or expired OTP")
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
await set_otp(f"token:login:access:{identifier}", {"access_token": tokens['access_token'], "expiry_duration": 30 * 60}) # 30 minutes
|
| 218 |
-
await set_otp(f"token:login:refresh:{identifier}", {"refresh_token": tokens['refresh_token'], "expiry_duration": 1 * 24 * 60 * 60})#
|
| 219 |
-
|
| 220 |
return tokens
|
| 221 |
|
| 222 |
async def refresh_token_service(identifier: str, refresh_token: str) -> dict:
|
|
@@ -234,10 +248,25 @@ async def refresh_token_service(identifier: str, refresh_token: str) -> dict:
|
|
| 234 |
if not token_data or token_data["refresh_token"] != refresh_token:
|
| 235 |
raise HTTPException(status_code=400, detail="Invalid or expired refresh token")
|
| 236 |
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
await set_otp(f"token:login:access:{identifier}", {"access_token": tokens['access_token'], "expiry_duration": 30 * 60}) # 30 minutes
|
| 239 |
-
await set_otp(f"token:login:refresh:{identifier}", {"refresh_token": tokens['refresh_token'], "expiry_duration": 1 * 24 * 60 * 60})#
|
| 240 |
-
|
| 241 |
return tokens
|
| 242 |
|
| 243 |
async def logout_service(identifier: str) -> dict:
|
|
|
|
| 211 |
otp_data = await get_otp(f"otp:login:{identifier}")
|
| 212 |
if not otp_data or otp_data.get("otp") != otp:
|
| 213 |
raise HTTPException(status_code=400, detail="Invalid or expired OTP")
|
| 214 |
+
|
| 215 |
+
# Fetch merchant details after OTP verification
|
| 216 |
+
if "@" in identifier:
|
| 217 |
+
merchant = await get_merchant_by_email(identifier)
|
| 218 |
+
else:
|
| 219 |
+
merchant = await get_merchant_by_mobile(identifier)
|
| 220 |
+
|
| 221 |
+
if not merchant:
|
| 222 |
+
raise HTTPException(status_code=404, detail="Merchant not found")
|
| 223 |
+
|
| 224 |
+
merchant_id = merchant.get("merchant_id")
|
| 225 |
+
merchant_name = merchant.get("merchant_name")
|
| 226 |
+
role = merchant.get("role", "admin") # Default to "admin" if role is not set
|
| 227 |
+
|
| 228 |
+
# Generate tokens with merchant_id and role in the payload
|
| 229 |
+
tokens = generate_tokens(identifier, merchant_id=merchant_id, role=role)
|
| 230 |
+
|
| 231 |
await set_otp(f"token:login:access:{identifier}", {"access_token": tokens['access_token'], "expiry_duration": 30 * 60}) # 30 minutes
|
| 232 |
+
await set_otp(f"token:login:refresh:{identifier}", {"refresh_token": tokens['refresh_token'], "expiry_duration": 1 * 24 * 60 * 60}) # 1 day
|
| 233 |
+
|
| 234 |
return tokens
|
| 235 |
|
| 236 |
async def refresh_token_service(identifier: str, refresh_token: str) -> dict:
|
|
|
|
| 248 |
if not token_data or token_data["refresh_token"] != refresh_token:
|
| 249 |
raise HTTPException(status_code=400, detail="Invalid or expired refresh token")
|
| 250 |
|
| 251 |
+
# Fetch merchant details after OTP verification
|
| 252 |
+
if "@" in identifier:
|
| 253 |
+
merchant = await get_merchant_by_email(identifier)
|
| 254 |
+
else:
|
| 255 |
+
merchant = await get_merchant_by_mobile(identifier)
|
| 256 |
+
|
| 257 |
+
if not merchant:
|
| 258 |
+
raise HTTPException(status_code=404, detail="Merchant not found")
|
| 259 |
+
|
| 260 |
+
merchant_id = merchant.get("merchant_id")
|
| 261 |
+
merchant_name = merchant.get("merchant_name")
|
| 262 |
+
role = merchant.get("role", "admin") # Default to "admin" if role is not set
|
| 263 |
+
|
| 264 |
+
# Generate tokens with merchant_id and role in the payload
|
| 265 |
+
tokens = generate_tokens(identifier, merchant_id=merchant_id, role=role)
|
| 266 |
+
|
| 267 |
await set_otp(f"token:login:access:{identifier}", {"access_token": tokens['access_token'], "expiry_duration": 30 * 60}) # 30 minutes
|
| 268 |
+
await set_otp(f"token:login:refresh:{identifier}", {"refresh_token": tokens['refresh_token'], "expiry_duration": 1 * 24 * 60 * 60}) # 1 day
|
| 269 |
+
|
| 270 |
return tokens
|
| 271 |
|
| 272 |
async def logout_service(identifier: str) -> dict:
|