|
| 1 | +from firebase_functions import https_fn |
| 2 | +from firebase_admin import firestore, auth |
| 3 | +import google.cloud.firestore |
| 4 | +import json |
| 5 | + |
| 6 | +@https_fn.on_call() |
| 7 | +def create_user_profile(req: https_fn.CallableRequest) -> dict: |
| 8 | + """ |
| 9 | + Callable function to create a user profile. |
| 10 | + Called after user registration. |
| 11 | + """ |
| 12 | + if not req.auth: |
| 13 | + raise https_fn.HttpsError( |
| 14 | + code=https_fn.FunctionsErrorCode.UNAUTHENTICATED, |
| 15 | + message="User must be authenticated" |
| 16 | + ) |
| 17 | + |
| 18 | + try: |
| 19 | + # Get user data from Firebase Auth |
| 20 | + user = auth.get_user(req.auth.uid) |
| 21 | + |
| 22 | + # Create user profile in Firestore |
| 23 | + firestore_client: google.cloud.firestore.Client = firestore.client() |
| 24 | + user_ref = firestore_client.collection('users').document(user.uid) |
| 25 | + |
| 26 | + user_data = { |
| 27 | + 'displayName': user.display_name or '', |
| 28 | + 'email': user.email, |
| 29 | + 'photoURL': user.photo_url or '', |
| 30 | + 'createdAt': firestore.SERVER_TIMESTAMP, |
| 31 | + 'lastLogin': firestore.SERVER_TIMESTAMP, |
| 32 | + 'role': 'student', # Default role |
| 33 | + 'courses': [] # Empty list of courses initially |
| 34 | + } |
| 35 | + |
| 36 | + user_ref.set(user_data) |
| 37 | + |
| 38 | + return {"success": True, "message": "User profile created successfully"} |
| 39 | + |
| 40 | + except Exception as e: |
| 41 | + raise https_fn.HttpsError( |
| 42 | + code=https_fn.FunctionsErrorCode.INTERNAL, |
| 43 | + message=f"Error creating user profile: {str(e)}" |
| 44 | + ) |
| 45 | + |
| 46 | +@https_fn.on_call() |
| 47 | +def delete_user_data(req: https_fn.CallableRequest) -> dict: |
| 48 | + """ |
| 49 | + Callable function to delete a user's data. |
| 50 | + Called before deleting a user account. |
| 51 | + """ |
| 52 | + if not req.auth: |
| 53 | + raise https_fn.HttpsError( |
| 54 | + code=https_fn.FunctionsErrorCode.UNAUTHENTICATED, |
| 55 | + message="User must be authenticated" |
| 56 | + ) |
| 57 | + |
| 58 | + try: |
| 59 | + # Delete user profile from Firestore |
| 60 | + firestore_client: google.cloud.firestore.Client = firestore.client() |
| 61 | + user_ref = firestore_client.collection('users').document(req.auth.uid) |
| 62 | + |
| 63 | + # Delete the user document |
| 64 | + user_ref.delete() |
| 65 | + |
| 66 | + return {"success": True, "message": "User data deleted successfully"} |
| 67 | + |
| 68 | + except Exception as e: |
| 69 | + raise https_fn.HttpsError( |
| 70 | + code=https_fn.FunctionsErrorCode.INTERNAL, |
| 71 | + message=f"Error deleting user data: {str(e)}" |
| 72 | + ) |
| 73 | + |
| 74 | +@https_fn.on_request() |
| 75 | +def get_user_profile(req: https_fn.Request) -> https_fn.Response: |
| 76 | + """ |
| 77 | + Get a user's profile data from Firestore. |
| 78 | + Requires authentication token in Authorization header. |
| 79 | + """ |
| 80 | + # Check if request has authorization header |
| 81 | + if not req.headers.get('Authorization'): |
| 82 | + return https_fn.Response( |
| 83 | + json.dumps({'error': 'No authorization token provided'}), |
| 84 | + status=401, |
| 85 | + headers={'Content-Type': 'application/json'} |
| 86 | + ) |
| 87 | + |
| 88 | + try: |
| 89 | + # Verify the Firebase ID token |
| 90 | + id_token = req.headers['Authorization'].split('Bearer ')[1] |
| 91 | + decoded_token = auth.verify_id_token(id_token) |
| 92 | + uid = decoded_token['uid'] |
| 93 | + |
| 94 | + # Get user profile from Firestore |
| 95 | + firestore_client: google.cloud.firestore.Client = firestore.client() |
| 96 | + user_doc = firestore_client.collection('users').document(uid).get() |
| 97 | + |
| 98 | + if not user_doc.exists: |
| 99 | + return https_fn.Response( |
| 100 | + json.dumps({'error': 'User profile not found'}), |
| 101 | + status=404, |
| 102 | + headers={'Content-Type': 'application/json'} |
| 103 | + ) |
| 104 | + |
| 105 | + # Update last login time |
| 106 | + user_doc.reference.update({ |
| 107 | + 'lastLogin': firestore.SERVER_TIMESTAMP |
| 108 | + }) |
| 109 | + |
| 110 | + # Return user profile data |
| 111 | + return https_fn.Response( |
| 112 | + json.dumps(user_doc.to_dict(), default=str), |
| 113 | + headers={'Content-Type': 'application/json'} |
| 114 | + ) |
| 115 | + |
| 116 | + except Exception as e: |
| 117 | + return https_fn.Response( |
| 118 | + json.dumps({'error': str(e)}), |
| 119 | + status=400, |
| 120 | + headers={'Content-Type': 'application/json'} |
| 121 | + ) |
| 122 | + |
| 123 | +@https_fn.on_request() |
| 124 | +def update_user_profile(req: https_fn.Request) -> https_fn.Response: |
| 125 | + """ |
| 126 | + Update a user's profile data in Firestore. |
| 127 | + Requires authentication token in Authorization header. |
| 128 | + """ |
| 129 | + # Check if request has authorization header |
| 130 | + if not req.headers.get('Authorization'): |
| 131 | + return https_fn.Response( |
| 132 | + json.dumps({'error': 'No authorization token provided'}), |
| 133 | + status=401, |
| 134 | + headers={'Content-Type': 'application/json'} |
| 135 | + ) |
| 136 | + |
| 137 | + try: |
| 138 | + # Verify the Firebase ID token |
| 139 | + id_token = req.headers['Authorization'].split('Bearer ')[1] |
| 140 | + decoded_token = auth.verify_id_token(id_token) |
| 141 | + uid = decoded_token['uid'] |
| 142 | + |
| 143 | + # Get update data from request body |
| 144 | + try: |
| 145 | + update_data = json.loads(req.data.decode()) |
| 146 | + except json.JSONDecodeError: |
| 147 | + return https_fn.Response( |
| 148 | + json.dumps({'error': 'Invalid JSON in request body'}), |
| 149 | + status=400, |
| 150 | + headers={'Content-Type': 'application/json'} |
| 151 | + ) |
| 152 | + |
| 153 | + # Remove any fields that shouldn't be updated by users |
| 154 | + protected_fields = ['email', 'createdAt', 'role'] |
| 155 | + for field in protected_fields: |
| 156 | + update_data.pop(field, None) |
| 157 | + |
| 158 | + # Update user profile in Firestore |
| 159 | + firestore_client: google.cloud.firestore.Client = firestore.client() |
| 160 | + user_ref = firestore_client.collection('users').document(uid) |
| 161 | + user_ref.update(update_data) |
| 162 | + |
| 163 | + # Get and return updated profile |
| 164 | + updated_profile = user_ref.get() |
| 165 | + return https_fn.Response( |
| 166 | + json.dumps(updated_profile.to_dict(), default=str), |
| 167 | + headers={'Content-Type': 'application/json'} |
| 168 | + ) |
| 169 | + |
| 170 | + except Exception as e: |
| 171 | + return https_fn.Response( |
| 172 | + json.dumps({'error': str(e)}), |
| 173 | + status=400, |
| 174 | + headers={'Content-Type': 'application/json'} |
| 175 | + ) |
0 commit comments