# Login & Logout System โ€” Usage Guide ## ๐ŸŽฏ Overview The system now supports two modes: ### 1. **Local Mode** (Development with .env) - API key in `.env` file - **No login page** โ€” goes straight to setup - Logout button **hidden** - For developers/instructors ### 2. **Hosted Mode** (Render/Hugging Face) - No `.env` file (or empty `OPENAI_API_KEY`) - **Login page shown** โ€” users provide name + API key - Logout button **visible** in top-right corner - Each user pays for their own OpenAI usage --- ## ๐Ÿš€ Running Locally ### With API Key (Skip Login): ```bash # Make sure .env has OPENAI_API_KEY python challenge.py # or python app.py ``` ### Without API Key (Test Login Page): ```bash # Temporarily hide .env mv .env .env.backup # Run the app python app.py # You'll see the login page! # Enter name + API key to continue # Restore .env when done mv .env.backup .env ``` --- ## ๐ŸŒ Deploying to Render See [RENDER_DEPLOYMENT.md](RENDER_DEPLOYMENT.md) for full guide. **Quick steps:** 1. Push code to GitHub 2. Connect Render to your repo 3. Render auto-detects `render.yaml` 4. Deploy! ๐Ÿš€ **Don't add `OPENAI_API_KEY` to Render** โ€” let users provide their own! --- ## ๐Ÿ” How Login Works ### Login Page Components: - **Name field**: Student's name (shown in header) - **API Key field**: OpenAI API key (sk-proj-...) - **Continue button**: Validates key and proceeds ### Validation: - Checks format (must start with `sk-`) - Makes test call to OpenAI API - Shows error if invalid/no quota - Sets key in environment on success ### Logout: - **Button location**: Top-right corner (after login) - **Action**: Clears API key, returns to login page - **Note**: Refresh won't logout โ€” must click button explicitly --- ## ๐Ÿ”„ Auto-Detection Logic ```python has_env_key = bool(os.getenv("OPENAI_API_KEY")) if has_env_key: # Show setup page directly # Hide login page # Hide logout button else: # Show login page # Hide setup page # Show logout button after login ``` --- ## ๐Ÿ“ Files Modified - โœ… `student_interface.py` โ€” Added `validate_openai_key()`, login page, logout button - โœ… `challenge.py` โ€” Made API key optional for GUI mode - โœ… `app.py` โ€” New entry point for hosting (uses port from env) - โœ… `render.yaml` โ€” Render configuration - โœ… `RENDER_DEPLOYMENT.md` โ€” Deployment guide --- ## โš ๏ธ Important Notes ### For Local Users: - Keep your `.env` file **private** (in `.gitignore`) - Never commit API keys to git! ### For Hosted Users: - Each student needs their own OpenAI API key - Keys are **session-only** (not stored) - Logout clears the key from memory ### Cost Management: - **Hosted mode**: Each user pays for their own usage - **Local mode**: You pay (instructor/developer) - **Recommendation**: Use hosted mode for courses/public access --- ## ๐Ÿงช Testing ### Test Login Flow: ```bash # 1. Hide .env to force login page mv .env .env.backup # 2. Run app python app.py # 3. Try login with: # - Empty fields โ†’ error # - Invalid key โ†’ error # - Valid key โ†’ success! # 4. Test logout button # - Click logout โ†’ returns to login page # - Login again โ†’ works! # 5. Restore .env mv .env.backup .env ``` ### Test Local Mode: ```bash # With .env present python challenge.py # Should skip login page entirely # Go straight to setup page # No logout button visible ``` --- ## ๐Ÿ› Troubleshooting **Q: Login page not showing?** A: Check that `OPENAI_API_KEY` is not in `.env` or environment **Q: Logout button not visible?** A: It only appears after successful login (hosted mode) **Q: "API key valid but no quota"?** A: User needs to add billing/credits at platform.openai.com/billing **Q: Refresh goes back to setup page?** A: Known Gradio limitation โ€” use explicit logout button instead **Q: Can I force login page even with .env?** A: Yes! Temporarily comment out `OPENAI_API_KEY` in `.env` --- ## ๐Ÿ“š Related Documentation - [RENDER_DEPLOYMENT.md](RENDER_DEPLOYMENT.md) โ€” How to deploy to Render - [add_login.md](add_login.md) โ€” Detailed implementation notes - [README.md](README.md) โ€” Main project documentation