> ## Documentation Index
> Fetch the complete documentation index at: https://r2r-patch-fix-ingestion.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Authentication

> Manage users in R2R with built-in authentication

## User Authentication and Management

R2R provides a comprehensive set of user authentication and management features, allowing you to implement secure and feature-rich authentication systems in your applications.

### User Registration

To register a new user:

```python
register_response = client.register("user@example.com", "password123")
```

<AccordionGroup>
  <Accordion title="Response">
    <ResponseField name="response" type="dict">
      ```python
      {
        'results': {
          'email': 'user@example.com',
          'id': 'bf417057-f104-4e75-8579-c74d26fcbed3',
          'hashed_password': '$2b$12$p6a9glpAQaq.4uzi4gXQru6PN7WBpky/xMeYK9LShEe4ygBf1L.pK',
          'is_superuser': False,
          'is_active': True,
          'is_verified': False,
          'verification_code_expiry': None,
          'name': None,
          'bio': None,
          'profile_picture': None,
          'created_at': '2024-07-16T22:53:47.524794Z',
          'updated_at': '2024-07-16T22:53:47.524794Z'
        }
      }
      ```
    </ResponseField>
  </Accordion>
</AccordionGroup>

### Email Verification

If email verification is enabled, verify a user's email:

```python
verify_response = client.verify_email("verification_code_here")
```

<AccordionGroup>
  <Accordion title="Response">
    <ResponseField name="response" type="dict">
      ```python
      {
        "results": {
          "message": "Email verified successfully"
        }
      }
      ```
    </ResponseField>
  </Accordion>
</AccordionGroup>

### User Login

To log in and obtain access tokens:

```python
login_response = client.login("user@example.com", "password123")
```

<AccordionGroup>
  <Accordion title="Response">
    <ResponseField name="response" type="dict">
      ```python
      {
        'results': {
          'access_token': {
            'token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
            'token_type': 'access'
          },
          'refresh_token': {
            'token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
            'token_type': 'refresh'
          }
        }
      }
      ```
    </ResponseField>
  </Accordion>
</AccordionGroup>

### Get Current User Info

Retrieve information about the currently authenticated user:

```python
user_info = client.user()
```

<AccordionGroup>
  <Accordion title="Response">
    <ResponseField name="response" type="dict">
      ```python
      {
        'results': {
          'email': 'user@example.com',
          'id': '76eea168-9f98-4672-af3b-2c26ec92d7f8',
          'hashed_password': 'null',
          'is_superuser': False,
          'is_active': True,
          'is_verified': True,
          'verification_code_expiry': None,
          'name': None,
          'bio': None,
          'profile_picture': None,
          'created_at': '2024-07-16T23:06:42.123303Z',
          'updated_at': '2024-07-16T23:22:48.256239Z'
        }
      }
      ```
    </ResponseField>
  </Accordion>
</AccordionGroup>

### Refresh Access Token

Refresh an expired access token:

```python
refresh_response = client.refresh_access_token()
```

<AccordionGroup>
  <Accordion title="Response">
    <ResponseField name="response" type="dict">
      ```python
      {
        'results': {
          'access_token': {
            'token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
            'token_type': 'access'
          },
          'refresh_token': {
            'token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...',
            'token_type': 'refresh'
          }
        }
      }
      ```
    </ResponseField>
  </Accordion>
</AccordionGroup>

### Change Password

Change the user's password:

```python
change_password_result = client.change_password("password123", "new_password")
```

<AccordionGroup>
  <Accordion title="Response">
    <ResponseField name="response" type="dict">
      ```python
      {
        "result": {
          "message": "Password changed successfully"
        }
      }
      ```
    </ResponseField>
  </Accordion>
</AccordionGroup>

### Request Password Reset

Request a password reset for a user:

```python
reset_request_result = client.request_password_reset("user@example.com")
```

<AccordionGroup>
  <Accordion title="Response">
    <ResponseField name="response" type="dict">
      ```python
      {
        "result": {
          "message": "If the email exists, a reset link has been sent"
        }
      }
      ```
    </ResponseField>
  </Accordion>
</AccordionGroup>

### Confirm Password Reset

Confirm a password reset using the reset token:

```python
reset_confirm_result = client.confirm_password_reset("reset_token_here", "new_password")
```

<AccordionGroup>
  <Accordion title="Response">
    <ResponseField name="response" type="dict">
      ```python
      {
        "result": {
          "message": "Password reset successfully"
        }
      }
      ```
    </ResponseField>
  </Accordion>
</AccordionGroup>

### Update User Profile

Update the user's profile information:

```python
update_result = client.update_user(name="John Doe", bio="R2R enthusiast")
```

<AccordionGroup>
  <Accordion title="Response">
    <ResponseField name="response" type="dict">
      ```python
      {
        'results': {
          'email': 'user@example.com',
          'id': '76eea168-9f98-4672-af3b-2c26ec92d7f8',
          'hashed_password': 'null',
          'is_superuser': False,
          'is_active': True,
          'is_verified': True,
          'verification_code_expiry': None,
          'name': 'John Doe',
          'bio': 'R2R enthusiast',
          'profile_picture': None,
          'created_at': '2024-07-16T23:06:42.123303Z',
          'updated_at': '2024-07-16T23:22:48.256239Z'
        }
      }
      ```
    </ResponseField>
  </Accordion>
</AccordionGroup>

### Delete User Account

Delete the user's account:

```python
user_id = register_response["results"]["id"] # input unique id here
delete_result = client.delete_user(user_id, "password123")
```

<AccordionGroup>
  <Accordion title="Response">
    <ResponseField name="response" type="dict">
      ```python
      {
        'results': {
          'message': 'User account deleted successfully'
        }
      }
      ```
    </ResponseField>
  </Accordion>
</AccordionGroup>

### User Logout

Log out and invalidate the current access token:

```python
logout_response = client.logout()
```

<AccordionGroup>
  <Accordion title="Response">
    <ResponseField name="response" type="dict">
      ```python
      {
        'results': {
          'message': 'Logged out successfully'
        }
      }
      ```
    </ResponseField>
  </Accordion>
</AccordionGroup>

### Superuser Capabilities

Superusers have additional privileges, including access to system-wide operations and sensitive information. To use superuser capabilities, authenticate as a superuser or the default admin:

```python
# Login as admin
login_result = client.login("admin@example.com", "admin_password")

# Access superuser features
users_overview = client.users_overview()
logs = client.logs()
analytics_result = client.analytics(
    {"all_latencies": "search_latency"},
    {"search_latencies": ["basic_statistics", "search_latency"]}
)
```

<Note>
  Superuser actions should be performed with caution and only by authorized personnel. Ensure proper security measures are in place when using superuser capabilities.
</Note>

## Security Considerations

When implementing user authentication, consider the following best practices:

1. Always use HTTPS in production to encrypt data in transit.
2. Implement rate limiting to protect against brute-force attacks.
3. Use secure password hashing (R2R uses bcrypt by default).
4. Consider implementing multi-factor authentication (MFA) for enhanced security.
5. Conduct regular security audits of your authentication system.

For more advanced use cases or custom implementations, refer to the R2R documentation or reach out to the community for support.
