# Postman Setup Guide for Staff Login API

## Quick Setup

### 1. Import Collection
1. Open Postman
2. Click **Import** in the top left
3. Select **File** tab
4. Choose `postman_collection.json` from your project directory
5. Click **Import**

### 2. Configure Environment Variables
The collection includes these variables:
- `baseUrl`: `http://localhost:8001` (your Laravel server URL)
- `authToken`: Automatically set after successful login

### 3. Test the API

#### Step 1: Login
1. Open the **Staff Login** request
2. Update the email and password in the request body:
   ```json
   {
       "email": "your-staff-email@example.com",
       "password": "your-password",
       "device_name": "Postman Client"
   }
   ```
3. Click **Send**
4. **Auto-magic**: The token will be automatically extracted and saved to `authToken` variable

#### Step 2: Get Staff Info
1. Open the **Get Staff Info** request
2. Click **Send** (no need to manually add token - it's auto-populated)
3. View your staff information

#### Step 3: Refresh Token (Optional)
1. Open the **Refresh Token** request
2. Click **Send**
3. New token will be automatically saved

#### Step 4: Logout
1. Open the **Staff Logout** request
2. Click **Send**
3. Token will be automatically cleared

## Manual Setup (Alternative)

If you prefer to set up manually:

### 1. Create New Collection
1. Click **New** → **Collection**
2. Name it "Staff Login API"

### 2. Add Variables
1. Go to collection **Variables** tab
2. Add:
   - `baseUrl`: `http://localhost:8001`
   - `authToken`: (leave empty)

### 3. Create Requests

#### Login Request
- **Method**: POST
- **URL**: `{{baseUrl}}/api/staff/login`
- **Headers**:
  - Content-Type: `application/json`
  - Accept: `application/json`
- **Body** (raw JSON):
  ```json
  {
      "email": "admin@example.com",
      "password": "password",
      "device_name": "Postman Client"
  }
  ```

#### Get Staff Info Request
- **Method**: GET
- **URL**: `{{baseUrl}}/api/staff/me`
- **Headers**:
  - Authorization: `Bearer {{authToken}}`
  - Accept: `application/json`

#### Refresh Token Request
- **Method**: POST
- **URL**: `{{baseUrl}}/api/staff/refresh`
- **Headers**:
  - Authorization: `Bearer {{authToken}}`
  - Accept: `application/json`

#### Logout Request
- **Method**: POST
- **URL**: `{{baseUrl}}/api/staff/logout`
- **Headers**:
  - Authorization: `Bearer {{authToken}}`
  - Accept: `application/json`

### 4. Add Test Scripts (Optional)

For the **Login** request, add this test script:
```javascript
if (pm.response.code === 200) {
    const response = pm.response.json();
    if (response.status === 'success' && response.data.token) {
        pm.collectionVariables.set('authToken', response.data.token);
        console.log('Token saved:', response.data.token);
    }
}
```

For the **Logout** request, add this test script:
```javascript
if (pm.response.code === 200) {
    pm.collectionVariables.set('authToken', '');
    console.log('Token cleared');
}
```

## Expected Responses

### Successful Login (200)
```json
{
    "status": "success",
    "message": "Login successful",
    "data": {
        "user": {
            "id": 1,
            "name": "John Doe",
            "email": "staff@example.com",
            "role": "staff",
            "is_active": true,
            "created_at": "2024-01-01T00:00:00.000000Z"
        },
        "token": "1|abc123def456...",
        "token_type": "Bearer",
        "expires_at": "2024-01-08T00:00:00.000000Z"
    }
}
```

### Get Staff Info (200)
```json
{
    "status": "success",
    "data": {
        "user": {
            "id": 1,
            "name": "John Doe",
            "email": "staff@example.com",
            "role": "staff",
            "is_active": true,
            "created_at": "2024-01-01T00:00:00.000000Z",
            "updated_at": "2024-01-01T00:00:00.000000Z"
        }
    }
}
```

### Error Examples

#### Invalid Credentials (401)
```json
{
    "status": "error",
    "message": "Invalid credentials"
}
```

#### Validation Error (422)
```json
{
    "status": "error",
    "message": "Validation failed",
    "errors": {
        "email": ["The email field is required."],
        "password": ["The password field is required."]
    }
}
```

## Troubleshooting

### Common Issues

1. **404 Not Found**: Make sure your Laravel server is running (`php artisan serve`)
2. **401 Unauthorized**: Check your email/password and ensure user is active
3. **403 Forbidden**: User might not have staff/admin role
4. **422 Validation Error**: Check request body format

### Debug Tips

1. Check **Console** tab in Postman for test script output
2. Verify `authToken` variable is set after login
3. Check **Headers** tab to ensure Authorization header is correct
4. Use **Postman Console** (View → Show Postman Console) for detailed logs

## Pro Tips

1. **Environment**: Create separate environments for development/production
2. **Tests**: Add more test scripts to validate responses
3. **Documentation**: Use Postman's documentation feature for sharing
4. **Automation**: Export collection for CI/CD testing

## Video Tutorial Steps

1. Import the collection
2. Update login credentials
3. Send login request
4. Verify token is auto-saved
5. Test protected endpoints
6. Logout and verify token is cleared

That's it! Your Staff Login API is now ready to use in Postman.
