Skip to content

Commit

Permalink
Add create group endpoint with Firebase authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
upayanmazumder committed Nov 29, 2024
1 parent a7158d8 commit 048f7ab
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
64 changes: 64 additions & 0 deletions api/auth/group/create.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# FILE: auth/group/create.py
from flask import Blueprint, request, jsonify
from firebase_admin import auth, firestore

create_group_bp = Blueprint('create_group', __name__)

db = firestore.client()

@create_group_bp.route('/auth/group/create', methods=['POST'])
def create_group():
"""
Route to create a new group. The user must be authenticated using a Firebase token.
"""
# Retrieve the Firebase ID token from the Authorization header
id_token = request.headers.get('Authorization')

if not id_token:
return jsonify({"error": "Missing Firebase ID token"}), 401

try:
# Verify the Firebase ID token
decoded_token = auth.verify_id_token(id_token)
user_id = decoded_token['uid']

# Retrieve the authenticated user's email
user_email = decoded_token.get('email')
if not user_email:
return jsonify({"error": "Unable to retrieve user email from token"}), 401

# Get the group name from the request body
group_data = request.json
if not group_data or 'group_name' not in group_data:
return jsonify({"error": "Missing 'group_name' in request body"}), 400

group_name = group_data['group_name']
description = group_data.get('description') # Optional field

# Generate a unique group ID (could use a Firestore auto-generated ID)
group_id = db.collection('groups').document().id

# Prepare the group entry
group_entry = {
"group_name": group_name,
"description": description,
"group_leader": user_email, # Store the creator's email as the group leader
"created_by": user_id,
"created_at": firestore.SERVER_TIMESTAMP,
"members": [user_email] # Add the creator's email as the first member
}

# Save the group in Firestore
group_ref = db.collection('groups').document(group_id)
group_ref.set(group_entry)

return jsonify({
"success": True,
"group_id": group_id,
"message": "Group created successfully"
}), 201

except auth.InvalidIdTokenError:
return jsonify({"error": "Invalid Firebase ID token"}), 401
except Exception as e:
return jsonify({"error": str(e)}), 500
3 changes: 3 additions & 0 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,16 @@
try:
from auth.signup import signup_bp
from auth.login import login_bp
from auth.group.create import create_group_bp

app.register_blueprint(signup_bp)
app.register_blueprint(login_bp)
app.register_blueprint(create_group_bp)
print("Auth blueprints registered successfully.")
except ImportError as e:
print(f"Error importing blueprints: {e}")


@app.route('/')
def home():
return "Welcome to the Flask App"
Expand Down

0 comments on commit 048f7ab

Please sign in to comment.