Skip to content

Fix public study room join validation and preserve private room access controls#707

Open
akashgoudsidduluri wants to merge 3 commits into
durdana3105:mainfrom
akashgoudsidduluri:fix/issue-408-public-rooms-join-api
Open

Fix public study room join validation and preserve private room access controls#707
akashgoudsidduluri wants to merge 3 commits into
durdana3105:mainfrom
akashgoudsidduluri:fix/issue-408-public-rooms-join-api

Conversation

@akashgoudsidduluri
Copy link
Copy Markdown
Contributor

PR Description

Overview

This PR fixes an issue where authenticated users were incorrectly blocked from joining public study rooms through the API despite satisfying access requirements.

The update refactors join-room validation logic to correctly distinguish between public and private room workflows while preserving existing permission checks, room protections, and capacity validation behavior.


Fixes #663

Problem

Public study rooms should allow authenticated users to join without invitation-based permissions. However, the existing validation flow applied restrictive permission checks too early or inconsistently, causing valid join attempts to fail.

This created unintended access denial for public rooms while affecting overall room usability.


Changes Made

Join Room API Fixes

Updated the join-room workflow to:

  • correctly allow access to public rooms
  • preserve invitation/permission enforcement for private rooms
  • ensure capacity validation still executes before membership creation
  • prevent duplicate or invalid room membership states

Permission Logic Review

Reviewed and refined:

  • visibility checks
  • room access conditions
  • membership creation flow
  • authenticated user validation
  • public vs private room branching logic

Workflow Improvements

Public room flow:

  • authenticated users can join directly when capacity allows

Private room flow:

  • invitation or authorized access still required
  • unauthorized users remain blocked

Tests Added

Regression Coverage

Added tests covering:

Public Room Access

  • authenticated users can successfully join public rooms
  • membership records are created correctly
  • API responses return expected success states

Private Room Protection

  • unauthorized users cannot join private rooms
  • invitation/permission checks remain enforced

Capacity Validation

  • room limits continue functioning correctly
  • full rooms reject additional joins

Validation

Automated Testing

Verified:

  • public join workflow passes successfully
  • private room restrictions remain intact
  • room membership creation behaves correctly
  • no regressions introduced in room management functionality

Manual Verification

Tested:

  • joining public rooms via authenticated accounts
  • joining private rooms with and without permissions
  • capacity edge cases
  • room membership persistence

Impact

This PR restores expected accessibility for public study rooms while maintaining existing security and room management protections.

No unrelated business logic or database behavior was modified.
tabase layers

## Summary
Users were unable to join public study rooms through the API despite satisfying access requirements. This fix implements a secure RPC-based join mechanism with proper access controls.

## Changes Made

### Database Migration (supabase/migrations/20260603000000_join_public_study_room_rpc.sql)
- Created 'join_public_study_room' RPC function with SECURITY DEFINER
- Enforces explicit row-level security checks at database level
- Validates room exists before allowing join
- Prevents non-creators from joining private rooms
- Implements idempotent join (ON CONFLICT DO NOTHING)
- Updated study_room_participants RLS INSERT policy

### Frontend Updates
- Updated StudyRooms.tsx:
  - Implemented handleJoinRoom function calling join_public_study_room RPC
  - Shows 'Join' button only for public rooms or room creators
  - Displays 'Invite only' label for private rooms
  - Added proper error handling with user feedback

- Updated Room.tsx:
  - Modified fetchRoomDetails to auto-register user via RPC
  - Proper error handling for unauthorized private room access
  - Redirects to /rooms on access denied

### Testing
- Created comprehensive unit tests (backend/tests/studyRooms.test.js)
  - Public room join functionality
  - Private room access restrictions
  - Idempotent join behavior
  - Error handling and edge cases

- Created integration tests (backend/tests/studyRooms.integration.test.js)
  - Complete user workflows
  - RLS policy enforcement
  - UI state consistency
  - Concurrent join scenarios

## Acceptance Criteria Met
✓ Public rooms can be joined successfully
✓ Private room restrictions remain enforced
✓ Capacity checks continue to function
✓ Room membership records are created correctly
✓ Existing functionality is unaffected

## Security Considerations
- RPC uses SECURITY DEFINER with explicit search_path
- RLS policies enforce access control at database level
- Idempotent operations prevent race conditions
- Private room protection at both API and database layers
@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 3, 2026

@akashgoudsidduluri is attempting to deploy a commit to the durdana3105's projects Team on Vercel.

A member of the Team first needs to authorize it.

@akashgoudsidduluri
Copy link
Copy Markdown
Contributor Author

Hey @durdana3105 can you review this PR.

@durdana3105
Copy link
Copy Markdown
Owner

thi branch have merge conflicts. please resolve it

@akashgoudsidduluri
Copy link
Copy Markdown
Contributor Author

Hey @durdana3105 i have resolved the conflicts, you can review it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix Public Study Room Join Flow

2 participants