|
| 1 | +# Authentication Setup Guide |
| 2 | + |
| 3 | +This guide will help you set up Appwrite authentication for your Practice Exams Platform. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +1. An Appwrite account (sign up at [appwrite.io](https://appwrite.io)) |
| 8 | +2. A new Appwrite project |
| 9 | + |
| 10 | +## Step 1: Create Appwrite Project |
| 11 | + |
| 12 | +1. Log in to your Appwrite console |
| 13 | +2. Click "Create Project" |
| 14 | +3. Give your project a name (e.g., "Practice Exams Platform") |
| 15 | +4. Choose your preferred region |
| 16 | +5. Click "Create" |
| 17 | + |
| 18 | +## Step 2: Configure Authentication |
| 19 | + |
| 20 | +### Enable Authentication Methods |
| 21 | + |
| 22 | +1. In your project dashboard, go to **Auth** → **Settings** |
| 23 | +2. Enable the following authentication methods: |
| 24 | + - **Email/Password** (for OTP) |
| 25 | + - **Google OAuth** |
| 26 | + - **Apple OAuth** |
| 27 | + |
| 28 | +### Configure OAuth Providers |
| 29 | + |
| 30 | +#### Google OAuth |
| 31 | + |
| 32 | +1. Go to **Auth** → **OAuth2 Providers** |
| 33 | +2. Click on **Google** |
| 34 | +3. Enable the provider |
| 35 | +4. Add your Google OAuth credentials: |
| 36 | + - Client ID |
| 37 | + - Client Secret |
| 38 | +5. Set redirect URL: `https://yourdomain.com/auth/callback` |
| 39 | + |
| 40 | +#### Apple OAuth |
| 41 | + |
| 42 | +1. Go to **Auth** → **OAuth2 Providers** |
| 43 | +2. Click on **Apple** |
| 44 | +3. Enable the provider |
| 45 | +4. Add your Apple OAuth credentials: |
| 46 | + - Client ID |
| 47 | + - Client Secret |
| 48 | + - Team ID |
| 49 | +5. Set redirect URL: `https://yourdomain.com/auth/callback` |
| 50 | + |
| 51 | +### Configure Email Templates |
| 52 | + |
| 53 | +1. Go to **Auth** → **Templates** |
| 54 | +2. Customize the email templates for: |
| 55 | + - Email OTP verification |
| 56 | + - Email verification |
| 57 | + |
| 58 | +## Step 3: Environment Variables |
| 59 | + |
| 60 | +Create a `.env.local` file in your project root with: |
| 61 | + |
| 62 | +```bash |
| 63 | +APPWRITE_PUBLIC_ENDPOINT=https://[REGION].cloud.appwrite.io/v1 |
| 64 | +APPWRITE_PROJECT_ID=your_project_id_here |
| 65 | +``` |
| 66 | + |
| 67 | +Replace `your_project_id_here` with your actual Appwrite project ID. |
| 68 | + |
| 69 | +## Step 4: Update Callback URLs |
| 70 | + |
| 71 | +In your Appwrite project settings, you need to configure callback URLs for OAuth providers: |
| 72 | + |
| 73 | +### For Google OAuth: |
| 74 | + |
| 75 | +1. Go to **Auth** → **OAuth2 Providers** → **Google** |
| 76 | +2. In the Google OAuth configuration, you'll see a **Redirect URL** field |
| 77 | +3. Set this to: `https://yourdomain.com/auth/callback` |
| 78 | + |
| 79 | +### For Apple OAuth: |
| 80 | + |
| 81 | +1. Go to **Auth** → **OAuth2 Providers** → **Apple** |
| 82 | +2. In the Apple OAuth configuration, you'll see a **Redirect URL** field |
| 83 | +3. Set this to: `https://yourdomain.com/auth/callback` |
| 84 | + |
| 85 | +**Note**: The success/failure URLs mentioned in the original documentation are not standard Appwrite settings. Appwrite handles OAuth redirects automatically to the redirect URL you specify above. The success/failure parameters are handled by your application logic in the callback route. |
| 86 | + |
| 87 | +## Step 5: Test Authentication |
| 88 | + |
| 89 | +1. Start your development server |
| 90 | +2. Navigate to any practice or exam page |
| 91 | +3. You should see the 15-minute trial timer |
| 92 | +4. After 15 minutes, the authentication modal should appear |
| 93 | +5. Test all three authentication methods: |
| 94 | + - Email OTP |
| 95 | + - Google OAuth |
| 96 | + - Apple OAuth |
| 97 | + |
| 98 | +## Features Implemented |
| 99 | + |
| 100 | +### Authentication Methods |
| 101 | + |
| 102 | +- **Email OTP**: 6-digit verification code sent via email |
| 103 | +- **Google OAuth**: Sign in with Google account |
| 104 | +- **Apple OAuth**: Sign in with Apple ID |
| 105 | + |
| 106 | +### Trial System |
| 107 | + |
| 108 | +- **15-minute trial** for unauthenticated users |
| 109 | +- **Automatic blocking** after trial expires |
| 110 | +- **Persistent trial state** across browser sessions |
| 111 | +- **Visual indicators** for trial status |
| 112 | + |
| 113 | +### User Experience |
| 114 | + |
| 115 | +- **Seamless integration** with existing UI |
| 116 | +- **Responsive design** for mobile and desktop |
| 117 | +- **User profile management** in navigation |
| 118 | +- **Automatic redirects** after authentication |
| 119 | + |
| 120 | +## PWA Compatibility |
| 121 | + |
| 122 | +This authentication system is fully compatible with PWA Builder for: |
| 123 | + |
| 124 | +- **Android** deployment |
| 125 | +- **iOS** deployment |
| 126 | +- **Microsoft Store** deployment |
| 127 | + |
| 128 | +The authentication flow works seamlessly across all platforms. |
| 129 | + |
| 130 | +## Troubleshooting |
| 131 | + |
| 132 | +### Common Issues |
| 133 | + |
| 134 | +1. **OAuth redirect errors**: Ensure callback URLs are correctly configured |
| 135 | +2. **Email not sending**: Check Appwrite email service configuration |
| 136 | +3. **Trial timer not working**: Clear localStorage and refresh page |
| 137 | +4. **Authentication state not persisting**: Check browser console for errors |
| 138 | + |
| 139 | +### Debug Mode |
| 140 | + |
| 141 | +Enable debug logging by adding to your `.env.local`: |
| 142 | + |
| 143 | +```bash |
| 144 | +NEXT_PUBLIC_DEBUG_AUTH=true |
| 145 | +``` |
| 146 | + |
| 147 | +## Security Considerations |
| 148 | + |
| 149 | +- All authentication is handled server-side by Appwrite |
| 150 | +- No sensitive credentials are stored in the frontend |
| 151 | +- Session management is handled securely by Appwrite |
| 152 | +- OAuth tokens are never exposed to the client |
| 153 | + |
| 154 | +## Next Steps |
| 155 | + |
| 156 | +After setup, consider: |
| 157 | + |
| 158 | +1. Adding user profile management |
| 159 | +2. Implementing role-based access control |
| 160 | +3. Adding analytics for user engagement |
| 161 | +4. Setting up email notifications for user actions |
0 commit comments