Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# from fastapi import FastAPI, Request
|
| 2 |
from flask import Flask, request, jsonify
|
| 3 |
from pymongo import MongoClient
|
| 4 |
from pydantic import BaseModel
|
|
@@ -6,8 +5,6 @@ from functools import wraps
|
|
| 6 |
import datetime
|
| 7 |
import uvicorn
|
| 8 |
import bcrypt
|
| 9 |
-
import base64
|
| 10 |
-
from bson.binary import Binary
|
| 11 |
import jwt
|
| 12 |
import os
|
| 13 |
|
|
@@ -95,7 +92,7 @@ def register_user():
|
|
| 95 |
"name": name,
|
| 96 |
"username": username,
|
| 97 |
"email": email,
|
| 98 |
-
"password":
|
| 99 |
"device_id": device_id,
|
| 100 |
"registered_on": datetime.datetime.now()
|
| 101 |
}
|
|
@@ -157,7 +154,7 @@ def register_google_user():
|
|
| 157 |
def login():
|
| 158 |
data = request.get_json()
|
| 159 |
email = data.get('username')
|
| 160 |
-
password = data.get('password')
|
| 161 |
|
| 162 |
# Fetch the user from the MongoDB database by email
|
| 163 |
user = users_collection.find_one({"email": email})
|
|
@@ -166,14 +163,10 @@ def login():
|
|
| 166 |
return jsonify({"success": False, "message": "User not found!"}), 404
|
| 167 |
|
| 168 |
# The stored hashed password (decode it to bytes if stored as a string)
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
# If it's stored as base64 Binary, we decode it to bytes
|
| 172 |
-
if isinstance(stored_password_binary, Binary):
|
| 173 |
-
stored_password_hash = base64.b64decode(stored_password_binary)
|
| 174 |
|
| 175 |
# Use bcrypt.checkpw to compare the plain password with the decoded hashed password
|
| 176 |
-
if
|
| 177 |
return jsonify({"success": False, "message": "Invalid credentials!"}), 401
|
| 178 |
|
| 179 |
# Generate a JWT token
|
|
|
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
from pymongo import MongoClient
|
| 3 |
from pydantic import BaseModel
|
|
|
|
| 5 |
import datetime
|
| 6 |
import uvicorn
|
| 7 |
import bcrypt
|
|
|
|
|
|
|
| 8 |
import jwt
|
| 9 |
import os
|
| 10 |
|
|
|
|
| 92 |
"name": name,
|
| 93 |
"username": username,
|
| 94 |
"email": email,
|
| 95 |
+
"password": password,
|
| 96 |
"device_id": device_id,
|
| 97 |
"registered_on": datetime.datetime.now()
|
| 98 |
}
|
|
|
|
| 154 |
def login():
|
| 155 |
data = request.get_json()
|
| 156 |
email = data.get('username')
|
| 157 |
+
password = data.get('password')
|
| 158 |
|
| 159 |
# Fetch the user from the MongoDB database by email
|
| 160 |
user = users_collection.find_one({"email": email})
|
|
|
|
| 163 |
return jsonify({"success": False, "message": "User not found!"}), 404
|
| 164 |
|
| 165 |
# The stored hashed password (decode it to bytes if stored as a string)
|
| 166 |
+
stored_password = user['password']
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
# Use bcrypt.checkpw to compare the plain password with the decoded hashed password
|
| 169 |
+
if password != stored_password:
|
| 170 |
return jsonify({"success": False, "message": "Invalid credentials!"}), 401
|
| 171 |
|
| 172 |
# Generate a JWT token
|