This is a Rust-based template project created by Erio-Harrison for developing applications with user authentication and AI-powered functionality. It features a modular and extensible architecture, supporting JWT-based authentication, OAuth (Google and Facebook), and AI integration (currently compatible with Tongyi Qianwen, with flexibility to add other providers easily).
For data storage, the project originally used MongoDB (available in the mongodb-version branch) but has since migrated to PostgreSQL. It also utilizes Redis for caching and session management. Designed for rapid development and scalability, this template provides a robust foundation for building secure, AI-enhanced applications.
Detailed API documentation is available in:
src/auth/README.md: Covers authentication endpoints (register, login, profile, OAuth).src/ai/README.md: Covers AI endpoints (text and image analysis).
rs-auth-ai/
├── src/
│ ├── main.rs # Application entry point
│ ├── errors.rs # Error handling
│ ├── config.rs # Configuration management
│ ├── db.rs # PostgreSQL connection and operations
│ ├── models/ # Data models
│ │ ├── mod.rs # Models module entry
│ │ ├── user.rs # User models with database operations
│ │ └── ai.rs # AI request/response models
│ ├── service/ # Core services
│ │ ├── mod.rs # Service module entry
│ │ └── redis_service.rs # Redis service
│ ├── auth/ # Authentication module
│ │ ├── utils.rs # JWT utilities
│ │ ├── auth_handlers.rs # Authentication request handlers
│ │ ├── oauth/ # OAuth providers
│ │ │ ├── models.rs # OAuth data models
│ │ │ ├── google.rs # Google OAuth
│ │ │ └── facebook.rs # Facebook OAuth
│ │ └── README.md # Authentication API docs
│ └── ai/ # AI module
│ ├── mod.rs # AI module entry
│ ├── routes.rs # AI routes
│ ├── handlers.rs # AI request handlers
│ ├── service.rs # AI service logic
│ ├── providers/ # AI provider implementations
│ │ ├── mod.rs # Provider module entry
│ │ └── tongyi.rs # Tongyi Qianwen provider
│ └── README.md # AI API docs
└── Cargo.toml # Project dependencies
- JWT-based registration and login with Argon2 password hashing.
- OAuth 2.0 for Google and Facebook, extensible to other providers.
- User profile management with avatar support.
- Text and image analysis via Tongyi Qianwen.
- Modular provider interface for adding AI services (e.g., OpenAI, Claude).
- PostgreSQL for robust relational data storage with UUID primary keys.
- Advanced indexing for optimal query performance.
- Connection pooling with SQLx for efficient database operations.
- Redis for efficient caching and session management.
- Environment-based setup via .env files.
- Custom error types for consistent API responses.
- Rust (stable, latest version)
- Cargo
- MongoDB
- PostgreSQL 12+
- API keys for Google and Facebook OAuth (optional)
- Tongyi Qianwen API key (for AI features)
- Clone the repository:
git clone https://github.com/Erio-Harrison/rs-auth-ai.git
cd rs-auth-ai- Configure environment variables in a .env file:
DATABASE_URL=Your DATABASE_URL
REDIS_URL=redis://localhost:6379
SERVER_HOST=0.0.0.0
SERVER_PORT=8080
JWT_SECRET=your_strong_secret_key
JWT_EXPIRATION=604800
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
FACEBOOK_APP_ID=your_facebook_app_id
FACEBOOK_APP_SECRET=your_facebook_app_secret
AI_TONGYI_API_KEY=your_tongyi_api_key- Install dependencies:
cargo build- Run the application:
cargo run- Uses a Provider trait for seamless integration of new AI services.
- Supports text and image inputs, with Tongyi Qianwen as the default provider.
- Secure JWT with configurable expiration (default: 1 week).
- OAuth support for third-party logins, with MongoDB storage for user data.
- MongoDB stores user profiles (with avatars, OAuth providers) and AI results.
- Custom serialization for MongoDB datetime fields.
- Actix-Web for high-performance, asynchronous request handling.
- Redis for fast caching and session management.
- Create a new provider in
src/ai/providers/(e.g., openai.rs). - Implement the Provider trait for text/image processing and API calls.
- Update
src/ai/service.rsto select the provider based on configuration.
- Add a new file in
src/auth/oauth/(e.g., twitter.rs). - Implement the OAuth flow, similar to google.rs or facebook.rs.
- Update
src/auth/oauth/models.rsandsrc/auth/routes.rs.
- Define new models in
src/models/. - Update
src/db.rsfor MongoDB operations.
Key dependencies (see Cargo.toml for details):
- actix-web: Web framework
- mongodb: MongoDB driver
- redis: Redis client
- jsonwebtoken: JWT handling
- argon2: Password hashing
- reqwest: HTTP client for AI APIs
- serde: Data serialization
- Fork the repository.
- Create a feature branch (
git checkout -b feature/your-feature). - Commit changes (
git commit -m 'Add your feature'). - Push to the branch (
git push origin feature/your-feature). - Open a pull request.
MIT License. See LICENSE file for details.
For issues or feedback, open an issue on GitHub or contact Erio-Harrison.