# 🔀 Role-Based Redirect - Summary

Sistem redirect otomatis berdasarkan role user.

---

## 🎯 Alur Redirect

```
Login
  ↓
  ├─→ Admin → Dashboard Desktop (dashboard.html)
  └─→ User  → Dashboard Mobile (dashboard-mobile.html)
```

---

## ✅ Implementasi

### 1. Login Redirect (`login.php`)

**Setelah Login Berhasil:**
```php
if ($user['is_admin']) {
    header('Location: dashboard.html');      // Admin
} else {
    header('Location: dashboard-mobile.html'); // User
}
```

### 2. Index Redirect (`index.php`)

**Root URL:**
```php
if (logged_in) {
    if (is_admin) → dashboard.html
    else → dashboard-mobile.html
} else {
    → login.php
}
```

### 3. Dashboard Protection (`dashboard.html`)

**JavaScript Check:**
```javascript
// Auto-redirect non-admin to mobile
if (!user.isAdmin) {
    window.location.href = 'dashboard-mobile.html';
}
```

### 4. Mobile Menu (`dashboard-mobile.html`)

**Conditional Display:**
```javascript
// Admin: Show desktop link
// User: Hide desktop link
if (isAdmin) {
    desktopMenu.show();
}
```

---

## 🔐 Access Matrix

| Role | Desktop | Mobile | Admin Panel |
|------|---------|--------|-------------|
| Admin | ✅ | ✅ | ✅ |
| User | ❌ → Mobile | ✅ | ❌ |
| Guest | ❌ → Login | ❌ → Login | ❌ → Login |

---

## 📱 User Experience

### Admin:
1. Login → Dashboard Desktop
2. Can edit all positions
3. Access admin panel
4. Can switch to mobile view

### User:
1. Login → Dashboard Mobile
2. Can edit own position only
3. Mobile-optimized UI
4. Cannot access desktop (auto-redirect)

---

## 🧪 Testing

### Test Admin:
```bash
1. Login dengan username: admin
2. Verify redirect ke dashboard.html
3. Check dapat edit semua posisi
4. Check dapat akses admin panel
```

### Test User:
```bash
1. Login dengan username user biasa
2. Verify redirect ke dashboard-mobile.html
3. Check hanya bisa edit posisi sendiri
4. Try akses dashboard.html → auto-redirect
```

### Test Auto-Redirect:
```bash
1. Login sebagai user
2. Navigate ke dashboard.html
3. Verify auto-redirect ke dashboard-mobile.html
4. Check console log
```

---

## 📁 Modified Files

```
✅ login.php              - Role-based redirect after login
✅ index.php              - Root redirect based on session
✅ dashboard.html         - Auto-redirect non-admin users
✅ dashboard-mobile.html  - Hide desktop link for users
✅ assets/app-mobile.js   - Menu visibility logic
✅ check-access.php       - Access control helper (new)
```

---

## 🔧 Configuration

### Change Admin Redirect:
Edit `login.php` line ~25:
```php
header('Location: dashboard.html'); // Change URL
```

### Change User Redirect:
Edit `login.php` line ~28:
```php
header('Location: dashboard-mobile.html'); // Change URL
```

### Disable Auto-Redirect:
Comment in `dashboard.html`:
```javascript
// const canAccess = await checkUserRole();
```

---

## 🐛 Troubleshooting

| Issue | Solution |
|-------|----------|
| User tidak redirect | Clear cache, check session |
| Admin ke mobile | Check `is_admin` in database |
| Redirect loop | Clear cookies, check logs |
| Menu tidak muncul | Verify `isAdmin` in console |

---

## 📊 Flow Diagram

```
┌──────────┐
│  Login   │
└────┬─────┘
     │
     ├─→ Admin ──→ dashboard.html
     │              ├─→ Edit All
     │              ├─→ Admin Panel
     │              └─→ Full Access
     │
     └─→ User ───→ dashboard-mobile.html
                    ├─→ Edit Own
                    ├─→ Mobile UI
                    └─→ Limited Access
```

---

## ✨ Benefits

✅ **Better UX**: Right interface for right user  
✅ **Security**: Role-based access control  
✅ **Performance**: Mobile users get optimized UI  
✅ **Flexibility**: Admin can use both interfaces  
✅ **Automatic**: No manual selection needed  

---

## 📚 Documentation

- **Full Guide**: `docs/ROLE_BASED_REDIRECT.md`
- **Mobile Guide**: `docs/MOBILE_DASHBOARD_GUIDE.md`
- **Quick Start**: `MOBILE_QUICK_START.md`

---

**Version:** 1.0.0  
**Date:** 18 Feb 2026  
**Status:** ✅ Active
