File size: 14,723 Bytes
6998881
 
be25939
6998881
be25939
 
 
6998881
be25939
 
 
 
 
 
 
 
 
 
 
 
6924415
be25939
 
 
6924415
be25939
 
 
6924415
be25939
 
 
6924415
be25939
6924415
 
be25939
6924415
be25939
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b4cd87
 
6998881
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51aab2e
 
 
 
 
be25939
51aab2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b4cd87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51aab2e
be25939
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import requests
from requests.auth import HTTPBasicAuth
from flask import Blueprint, request, jsonify, session, current_app
from firebase_admin import auth as firebase_auth
from app.models.user import User
from bson.objectid import ObjectId
from datetime import datetime
import os

auth_bp = Blueprint('auth', __name__)

def get_mongo():
    return current_app.extensions['pymongo'][0]

@auth_bp.route('/register', methods=['POST'])
def register():
    """User registration endpoint"""
    try:
        data = request.get_json()
        # Validate required fields
        required_fields = ['id','name','authProvider']
        for field in required_fields:
            if field not in data or not data[field]:
                return jsonify({"error": f"{field} is required"}), 400
        id = data['id']
        email = data['email'].lower().strip()
        name = data['name'].strip()
        phone = data['phone']
        authProvider = data['authProvider']
        # Validate email format
        if not User.validate_email(email):
            return jsonify({"error": "Invalid email format"}), 400
        
        # Check if user already exists
        if User.find_by_id(id):
            return jsonify({"error": "User with this id already exists"}), 409
        
        user = User(id, email, name, phone, authProvider)
        user_id = user.save()
        
        return jsonify({
            "message": "User registered successfully",
            "user": {
                "id": user_id,
                "email": email,
                "name": name,
                "phone": phone,
            }
        }), 201
        
    except Exception as e:
        return jsonify({"error": "Registration failed", "details": str(e)}), 500

@auth_bp.route('/sendVerificationCodeForRegistration', methods=['POST'])
def send_verification_code_for_registration():
    """Send verification code via Twilio Verify if user does not already exist"""
    try:
        data = request.get_json()

        if 'phone' not in data or not data['phone']:
            return jsonify({"error": "phone is required"}), 400

        phone = data['phone'].strip()

        # βœ… Check if user already exists in MongoDB
        if User.find_by_phone(phone):
            return jsonify({"error": "User with this phone already exists in MongoDB"}), 409

        # βœ… Check if user already exists in Firebase
        try:
            fb_user = firebase_auth.get_user_by_phone_number(phone)
            if fb_user:
                return jsonify({"error": "User with this phone already exists in Firebase"}), 409
        except firebase_auth.UserNotFoundError:
            pass  # βœ… Safe, means phone is not registered in Firebase

        # πŸ”Ή If no user in MongoDB or Firebase β†’ send Twilio OTP
        TWILIO_SID = os.getenv("TWILIO_SID")
        TWILIO_AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")
        VERIFY_SERVICE_SID = os.getenv("VERIFY_SERVICE_SID")

        url = f"https://verify.twilio.com/v2/Services/{VERIFY_SERVICE_SID}/Verifications"

        response = requests.post(
            url,
            data={
                "To": phone,
                "Channel": "sms"
            },
            auth=HTTPBasicAuth(TWILIO_SID, TWILIO_AUTH_TOKEN)
        )

        # Twilio returns 201 Created on success
        if response.status_code == 201:
            return jsonify({
                "message": "Verification code sent successfully",
                "details": response.json()
            }), 201
        else:
            return jsonify({
                "error": "Failed to send verification code",
                "details": response.json()
            }), response.status_code

    except Exception as e:
        return jsonify({"error": "Verification request failed", "details": str(e)}), 500

@auth_bp.route('/verifyCodeAndRegisterWithPhone', methods=['POST'])
def verify_code_and_register_with_phone():
    """Register user with phone number after Twilio Verify + Firebase + MongoDB"""
    try:
        data = request.get_json()

        # Required fields (id removed)
        required_fields = ['name', 'phone', 'authProvider', 'code']
        for field in required_fields:
            if field not in data or not data[field]:
                return jsonify({"error": f"{field} is required"}), 400

        name = data['name'].strip()
        phone = data['phone'].strip()
        authProvider = data['authProvider']
        code = data['code'].strip()

        # Validate phone format (basic E.164 check)
        if not phone.startswith('+') or not phone[1:].isdigit():
            return jsonify({"error": "Invalid phone number format. Use E.164 format (e.g. +919876543210)"}), 400

        # βœ… Step 1: Verify OTP with Twilio
        TWILIO_SID = os.getenv("TWILIO_SID")
        TWILIO_AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")
        VERIFY_SERVICE_SID = os.getenv("VERIFY_SERVICE_SID")

        verify_url = f"https://verify.twilio.com/v2/Services/{VERIFY_SERVICE_SID}/VerificationCheck"

        response = requests.post(
            verify_url,
            data={"To": phone, "Code": code},
            auth=HTTPBasicAuth(TWILIO_SID, TWILIO_AUTH_TOKEN)
        )

        result = response.json()

        if response.status_code != 200 or result.get("status") != "approved":
            return jsonify({
                "error": "Phone verification failed",
                "details": result
            }), 400

        # βœ… Step 2: Check if user already exists in MongoDB
        if User.find_by_phone(phone):
            return jsonify({"error": "User with this phone already exists"}), 409

        # βœ… Step 3: Check Firebase user
        try:
            existing_user = firebase_auth.get_user_by_phone_number(phone)
            if not User.find_by_id(existing_user.uid):
                user = User(existing_user.uid, None, name, phone, authProvider)
                saved_id = user.save()
            else:
                saved_id = existing_user.uid

            return jsonify({
                "message": "User already exists in Firebase",
                "firebaseUid": existing_user.uid,
                "user": {
                    "id": saved_id,
                    "name": name,
                    "phone": phone,
                    "authProvider": authProvider
                }
            }), 200

        except firebase_auth.UserNotFoundError:
            # βœ… Step 4: No user β†’ create new in Firebase
            fb_user = firebase_auth.create_user(
                phone_number=phone,
                display_name=name
            )

            user = User(fb_user.uid, None, name, phone, authProvider)
            saved_id = user.save()

            return jsonify({
                "message": "User registered successfully with phone",
                "firebaseUid": fb_user.uid,
                "user": {
                    "id": saved_id,
                    "name": name,
                    "phone": phone,
                    "authProvider": authProvider
                }
            }), 201

    except Exception as e:
        return jsonify({"error": "Registration failed", "details": str(e)}), 500

@auth_bp.route('/login', methods=['POST'])
def login():
    """User login endpoint"""
    try:
        data = request.get_json()

        # Always required
        if 'authProvider' not in data or not data['authProvider']:
            return jsonify({"error": "authProvider is required"}), 400

        authProvider = data['authProvider']

        # If provider = email β†’ need email field
        if authProvider == "email":
            required_fields = ['email']
        else:
            required_fields = ['id']

        # Validate required fields
        for field in required_fields:
            if field not in data or not data[field]:
                return jsonify({"error": f"{field} is required"}), 400

        # Find user depending on provider
        if authProvider == "email":
            email = data['email'].lower().strip()
            user = User.find_by_email(email)
        else:
            user_id = data['id']
            user = User.find_by_id(user_id)

        if not user:
            return jsonify({"error": "User not found"}), 404

        # Verify authProvider matches
        if user.get("authProvider") != authProvider:
            return jsonify({"error": "Authentication provider mismatch"}), 401

        # Update last_login_at
        User.update_last_login(user["_id"])

        # Fetch updated user
        updated_user = User.find_by_id(user["_id"])

        return jsonify({
            "message": "Login successful",
            "user": {
                "id": updated_user["_id"],
                "email": updated_user["email"],
                "name": updated_user["name"],
                "phone": updated_user["phone"],
                "authProvider": updated_user["authProvider"],
                "last_login_at": updated_user["last_login_at"].isoformat() + "Z"
            }
        }), 200

    except Exception as e:
        return jsonify({"error": "Login failed", "details": str(e)}), 500

@auth_bp.route('/sendVerificationCodeForLogin', methods=['POST'])
def send_verification_code_for_login():
    """Send verification code via Twilio Verify if user already exist"""
    try:
        data = request.get_json()

        if 'phone' not in data or not data['phone']:
            return jsonify({"error": "phone is required"}), 400

        phone = data['phone'].strip()

        # βœ… Check if user not exists in MongoDB
        if not User.find_by_phone(phone):
            return jsonify({"error": "User with this phone does not exists in MongoDB"}), 404

        # βœ… Check if user not already exists in Firebase
        try:
            firebase_auth.get_user_by_phone_number(phone)
        except firebase_auth.UserNotFoundError:
            return jsonify({"error": "User with this phone does not exists in Firebase"}), 404

        # πŸ”Ή If no user in MongoDB or Firebase β†’ send Twilio OTP
        TWILIO_SID = os.getenv("TWILIO_SID")
        TWILIO_AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")
        VERIFY_SERVICE_SID = os.getenv("VERIFY_SERVICE_SID")

        url = f"https://verify.twilio.com/v2/Services/{VERIFY_SERVICE_SID}/Verifications"

        response = requests.post(
            url,
            data={
                "To": phone,
                "Channel": "sms"
            },
            auth=HTTPBasicAuth(TWILIO_SID, TWILIO_AUTH_TOKEN)
        )

        # Twilio returns 201 Created on success
        if response.status_code == 201:
            return jsonify({
                "message": "Verification code sent successfully",
                "details": response.json()
            }), 200
        else:
            return jsonify({
                "error": "Failed to send verification code",
                "details": response.json()
            }), response.status_code

    except Exception as e:
        return jsonify({"error": "Verification request failed", "details": str(e)}), 500

@auth_bp.route('/verifyCodeAndLoginWithPhone', methods=['POST'])
def verify_code_and_login_with_phone():
    """Login user with phone number after Twilio Verify + Firebase + MongoDB"""
    try:
        data = request.get_json()

        # Required fields (id removed)
        required_fields = ['phone', 'authProvider', 'code']
        for field in required_fields:
            if field not in data or not data[field]:
                return jsonify({"error": f"{field} is required"}), 400

        phone = data['phone'].strip()
        authProvider = data['authProvider']
        code = data['code'].strip()

        # Validate phone format (basic E.164 check)
        if not phone.startswith('+') or not phone[1:].isdigit():
            return jsonify({"error": "Invalid phone number format. Use E.164 format (e.g. +919876543210)"}), 400

        # βœ… Step 1: Verify OTP with Twilio
        TWILIO_SID = os.getenv("TWILIO_SID")
        TWILIO_AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")
        VERIFY_SERVICE_SID = os.getenv("VERIFY_SERVICE_SID")

        verify_url = f"https://verify.twilio.com/v2/Services/{VERIFY_SERVICE_SID}/VerificationCheck"

        response = requests.post(
            verify_url,
            data={"To": phone, "Code": code},
            auth=HTTPBasicAuth(TWILIO_SID, TWILIO_AUTH_TOKEN)
        )

        result = response.json()

        if response.status_code != 200 or result.get("status") != "approved":
            return jsonify({
                "error": "Phone verification failed",
                "details": result
            }), 400

        # βœ… Step 2: Check if user not exists in MongoDB
        if not User.find_by_phone(phone):
            return jsonify({"error": "User with this phone does not exists in MongoDB"}), 404

        # βœ… Step 3: Check Firebase user
        try:
            existing_user = firebase_auth.get_user_by_phone_number(phone)
            if User.find_by_id(existing_user.uid):
                user = User.find_by_id(existing_user.uid)
                User.update_last_login(user["_id"])    

            return jsonify({
                "message": "User login successful!",
                "firebaseUid": existing_user.uid,
                "user": {
                    "id": user["_id"],
                    "name": user["name"],
                    "phone": user["phone"],
                    "authProvider": user["authProvider"]
                }
            }), 200

        except firebase_auth.UserNotFoundError:
            # User not exist in firebase
            return jsonify({"message": "User with this phone does not exists"}), 404

    except Exception as e:
        return jsonify({"error": "Login failed", "details": str(e)}), 500

# @auth_bp.route('/check-session', methods=['GET'])
# def check_session():
#     """Check if user is logged in"""
#     try:
#         if 'user_id' not in session:
#             return jsonify({"authenticated": False}), 401
        
#         user = User.find_by_id(session['user_id'])
#         if not user or not user.get('is_active', True):
#             session.clear()
#             return jsonify({"authenticated": False}), 401
        
#         return jsonify({
#             "authenticated": True,
#             "user": {
#                 "id": session['user_id'],
#                 "email": session['user_email'],
#                 "first_name": user['first_name'],
#                 "last_name": user['last_name'],
#                 "role": session.get('user_role', 'customer')
#             }
#         }), 200
        
#     except Exception as e:
#         return jsonify({"error": "Session check failed", "details": str(e)}), 500