OAuth2 — Đăng nhập bên thứ ba

Cấu hình đăng nhập qua Google, Facebook, LDAP cho VieLang Mobile.

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

  1. Đăng nhập với tài khoản System Admin
  2. Vào SettingsOAuth2
  3. Click + Add client
  4. Điền thông tin provider (Google, Facebook, Custom...)

Bước 2: Đăng ký App trong Mobile Center

  1. Vào Mobile CenterBundle → Chọn bundle
  2. Tab OAuth 2.0
  3. Mở "Mobile applications" panel
  4. Click + Add application
  5. Điền:
    • Platform: Android hoặc iOS
    • Package name: com.yourcompany.app (Android) / Bundle ID (iOS)
    • Application Secret: auto-generate hoặc đặt tay
  6. 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.json

Mà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

  1. Vào Google Cloud Console
  2. Tạo project → APIs & ServicesCredentials
  3. + Create CredentialsOAuth client ID
  4. Application type: Web application
  5. Authorized redirect URIs:
    https://your-vielang-domain.com/login/oauth2/code/
  6. Copy Client IDClient 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ỗiNguyên nhânGiải pháp
Nút OAuth không hiệnApp Secret saiKiểm tra thingsboardOAuth2AppSecret
Callback loopURL scheme saiKiểm tra scheme trong Manifest + Info.plist
redirect_uri_mismatchRedirect URI chưa thêm vào GoogleThêm URI vào Google Cloud Console
User không được tạoallowUserCreation: falseBật allowUserCreation trong config