OAuth2 — Đăng nhập bên thứ ba
VieLang Mobile hỗ trợ đăng nhập qua các nhà cung cấp OAuth2 bên ngoài như Google, Facebook, GitHub, LDAP — người dùng không cần tạo tài khoản riêng.
Cấu hình trên Backend
Bước 1: Tạo OAuth2 Client
- Đăng nhập với tài khoản System Admin
- Vào Settings → OAuth2
- Click + Add client
- Điền thông tin provider (Google, Facebook, Custom...)
Bước 2: Đăng ký App trong Mobile Center
- Vào Mobile Center → Bundle → Chọn bundle
- Tab OAuth 2.0
- Mở "Mobile applications" panel
- Click + Add application
- Điền:
- Platform: Android hoặc iOS
- Package name:
com.yourcompany.app(Android) / Bundle ID (iOS) - Application Secret: auto-generate hoặc đặt tay
- Click Save
Cấu hình trong Flutter App
Bước 1: Cập nhật app_constants.dart
Mở file lib/constants/app_constants.dart:
class ThingsboardAppConstants {
// URL scheme cho OAuth2 callback (phải unique)
static final thingsboardOAuth2CallbackUrlScheme = 'com.yourcompany.app.auth';
// Application Secret từ Mobile Center
static final thingsboardOAuth2AppSecret = 'YOUR_APP_SECRET_HERE';
}Bước 2: Cập nhật Android Manifest
Mở android/app/src/main/AndroidManifest.xml, thêm activity:
<activity
android:name=".TbWebCallbackActivity"
android:exported="true"
android:launchMode="singleTask">
<intent-filter android:label="tb_web_auth">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="com.yourcompany.app.auth" />
</intent-filter>
</activity>Bước 3: Cấu hình iOS (Info.plist)
Mở ios/Runner/Info.plist, thêm:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>com.yourcompany.app.auth</string>
</array>
</dict>
</array>Bước 4: Kiểm tra
flutter run --dart-define-from-file configs.jsonMàn hình đăng nhập sẽ hiển thị các nút "Đăng nhập với Google" / "Facebook"...
Ví dụ: Cấu hình Google OAuth2
1. Tạo Google OAuth2 credentials
- Vào Google Cloud Console
- Tạo project → APIs & Services → Credentials
- + Create Credentials → OAuth client ID
- Application type: Web application
- Authorized redirect URIs:
https://your-vielang-domain.com/login/oauth2/code/ - Copy Client ID và Client Secret
2. Cấu hình trong VieLang IoT
POST /api/oauth2/config
Authorization: Bearer {SYS_ADMIN_JWT}
Content-Type: application/json
{
"enabled": true,
"clientRegistrations": [
{
"registrationId": "google",
"clientId": "YOUR_GOOGLE_CLIENT_ID",
"clientSecret": "YOUR_GOOGLE_CLIENT_SECRET",
"authorizationUri": "https://accounts.google.com/o/oauth2/v2/auth",
"tokenUri": "https://oauth2.googleapis.com/token",
"userInfoUri": "https://www.googleapis.com/oauth2/v3/userinfo",
"scope": ["email", "profile"],
"userNameAttributeName": "email",
"mapperConfig": {
"allowUserCreation": true,
"activateUser": true,
"type": "BASIC",
"basic": {
"emailAttributeKey": "email",
"firstNameAttributeKey": "given_name",
"lastNameAttributeKey": "family_name",
"tenantNameStrategy": "DOMAIN",
"alwaysFullScreen": false
}
}
}
]
}Troubleshooting
| Lỗi | Nguyên nhân | Giải pháp |
|---|---|---|
| Nút OAuth không hiện | App Secret sai | Kiểm tra thingsboardOAuth2AppSecret |
| Callback loop | URL scheme sai | Kiểm tra scheme trong Manifest + Info.plist |
redirect_uri_mismatch | Redirect URI chưa thêm vào Google | Thêm URI vào Google Cloud Console |
| User không được tạo | allowUserCreation: false | Bật allowUserCreation trong config |