Mcid Connect
A Dart package for Minecraft authentication that simplifies the process of logging into Minecraft services using Microsoft accounts.
Features
- Microsoft account authentication
- Xbox Live authentication
- Minecraft account validation
- Profile information retrieval
- Token management
Installation
Add this package to your pubspec.yaml
:
dependencies:
mcid_connect: ^0.0.1
Then run:
flutter pub get
Usage
Basic Authentication Flow
The authentication process involves several steps through Microsoft, Xbox Live, and Minecraft services. The AuthService
class handles this flow for you:
import 'package:mcid_connect/mcid_connect.dart';
// Initialize the auth service
final authService = AuthService(
clientId: 'your-microsoft-client-id',
redirectUri: 'http://localhost:3000',
scopes: ['XboxLive.signin', 'offline_access'],
onGetDeviceCode: (deviceCodeResponse) {
// Show verification URL and user code to the user
print('Please visit: ${deviceCodeResponse.verificationUri}');
print('And enter this code: ${deviceCodeResponse.userCode}');
},
);
// Start the authentication process
Future<void> login() async {
try {
// Will trigger the onGetDeviceCode callback with instructions for the user
final success = await authService.startAuthenticationFlow(useDeviceCode: true);
if (success) {
// Check if the user has a Minecraft profile
final hasProfile = await authService.hasMinecraftProfile();
if (hasProfile) {
final profile = await authService.getMinecraftProfile();
if (profile != null) {
print('Successfully logged in as: ${profile.name}');
print('UUID: ${profile.id}');
}
} else {
print('Authentication successful but no Minecraft profile found');
}
} else {
print('Authentication failed');
}
} catch (e) {
print('Authentication failed: $e');
}
}
Saving and Restoring Sessions
You can save and restore authentication sessions to avoid requiring users to log in repeatedly:
// Save session after successful login
Future<void> saveSession() async {
final session = await authService.getAuthenticationData();
// Store session data securely (e.g., using flutter_secure_storage)
}
// Restore session on app startup
Future<void> restoreSession(Map<String, dynamic> sessionData) async {
final success = await authService.restoreAuthentication(sessionData);
if (success) {
final profile = authService.minecraftProfile;
print('Session restored for: ${profile?.name}');
} else {
print('Session expired or invalid, new login required');
}
}
Advanced Usage: Direct Service Access
You can also use the individual services directly for more control:
// Microsoft authentication
final microsoftAuthService = MicrosoftAuthService(
clientId: 'your-microsoft-client-id',
redirectUri: 'http://localhost:8080',
);
// Xbox authentication
final xboxAuthService = XboxAuthService();
// Minecraft authentication
final minecraftAuthService = MinecraftAuthService(
clientId: 'your-client-id',
);
// Manual authentication flow
Future<void> manualAuthentication() async {
// 1. Get Microsoft token
final microsoftAccount = await microsoftAuthService.authenticateWithDeviceCode(
(deviceCode) => print('Visit ${deviceCode.verificationUri} and enter ${deviceCode.userCode}'),
);
// 2. Get Xbox token using Microsoft token
final xboxAccount = await xboxAuthService.authenticate(microsoftAccount.accessToken);
// 3. Get Minecraft token using Xbox token
final minecraftProfile = await minecraftAuthService.authenticate(xboxAccount.xstsToken);
print('Logged in as: ${minecraftProfile.name}');
}
Environment Variables
For security, you can use environment variables for sensitive information:
// Create a .env file in your project root:
// MICROSOFT_CLIENT_ID=your-client-id
// REDIRECT_URI=http://localhost:8080
// Then load it in your code:
import 'package:dotenv/dotenv.dart';
void main() {
final env = DotEnv()..load();
final authService = AuthService(
clientId: env['MICROSOFT_CLIENT_ID'] ?? '',
redirectUri: env['REDIRECT_URI'] ?? '',
scopes: ['XboxLive.signin', 'XboxLive.offline_access'],
// ...
);
}
License
This package is available under the MIT License.
Libraries
- data/auth/auth_models
- data/auth/microsoft/device_code_response
- data/auth/microsoft/microsoft_account
- data/auth/microsoft/microsoft_token_response
- data/auth/minecraft/minecraft_profile
- data/auth/minecraft/minecraft_token_response
- data/auth/xbox/xbox_live_response
- data/auth/xbox/xbox_profile
- data/auth/xbox/xsts_response
- data/profile/account_profile
- data/profile/microsoft_account_profile
- data/profile/minecraft_account_profile
- data/profile/minecraft_profile
- data/profile/xbox_account_profile
- env/env
- interfaces/auth_interfaces
- interfaces/auth_service_interface
- interfaces/base_auth_interface
- interfaces/microsoft_auth_service_interface
- interfaces/minecraft_auth_service_interface
- interfaces/xbox_auth_service_interface
- mcid_connect
- services/auth_service
- services/microsoft_auth_service
- services/minecraft_auth_service
- services/xbox_auth_service