Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -180,6 +180,45 @@ def get_user_profile():
|
|
| 180 |
return jsonify({'error': str(e)}), 500
|
| 181 |
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
def upload_log():
|
| 184 |
"""Uploads the log file to Firebase Storage and returns the download URL."""
|
| 185 |
try:
|
|
|
|
| 180 |
return jsonify({'error': str(e)}), 500
|
| 181 |
|
| 182 |
|
| 183 |
+
@app.route('/api/auth/google-signin', methods=['POST'])
|
| 184 |
+
def google_signin():
|
| 185 |
+
try:
|
| 186 |
+
auth_header = request.headers.get('Authorization', '')
|
| 187 |
+
if not auth_header.startswith('Bearer '):
|
| 188 |
+
return jsonify({'error': 'Missing or invalid token'}), 401
|
| 189 |
+
|
| 190 |
+
token = auth_header.split(' ')[1]
|
| 191 |
+
decoded_token = auth.verify_id_token(token) # Verify the token
|
| 192 |
+
uid = decoded_token['uid']
|
| 193 |
+
email = decoded_token.get('email')
|
| 194 |
+
|
| 195 |
+
# Check if user already exists in database
|
| 196 |
+
user_ref = db.reference(f'users/{uid}')
|
| 197 |
+
user_data = user_ref.get()
|
| 198 |
+
|
| 199 |
+
if not user_data:
|
| 200 |
+
# New user, create an entry in the database
|
| 201 |
+
user_data = {
|
| 202 |
+
'email': email,
|
| 203 |
+
'display_name': display_name,
|
| 204 |
+
'credits': 30, # Give new users initial credits
|
| 205 |
+
'is_admin': False,
|
| 206 |
+
'created_at': datetime.utcnow().isoformat(),
|
| 207 |
+
}
|
| 208 |
+
user_ref.set(user_data)
|
| 209 |
+
|
| 210 |
+
return jsonify({
|
| 211 |
+
'success': True,
|
| 212 |
+
'user': {
|
| 213 |
+
'uid': uid,
|
| 214 |
+
**user_data
|
| 215 |
+
}
|
| 216 |
+
}), 200
|
| 217 |
+
|
| 218 |
+
except Exception as e:
|
| 219 |
+
return jsonify({'error': str(e)}), 400
|
| 220 |
+
|
| 221 |
+
|
| 222 |
def upload_log():
|
| 223 |
"""Uploads the log file to Firebase Storage and returns the download URL."""
|
| 224 |
try:
|