Skip to main content

WeChat Login

WeChat login is a third-party authentication method based on WeChat Open Platform OAuth2.0 protocol, implementing seamless integration with WeChat user system through QR code login and user information retrieval. It handles WeChat user identity authentication, account binding management, and login status maintenance, supporting frontend-backend separation architecture and complete user authentication flow.

The hierarchical structure of WeChat login elements is Meta (auths.loginTypes.Meta) → Type (auths.loginTypes.WeChatType) → Instance. Developers can quickly create WeChat login instance elements through JitAi's visual development tools.

WeChat login is based on standard OAuth2.0 authentication flow. After users authorize through WeChat client QR code scanning, the system obtains access tokens and user identifiers, completing identity authentication and account association. It supports user binding, unbinding operations, and identity management in multi-application scenarios.

Of course, developers can also create their own Type elements or modify the official auths.loginTypes.WeChatType element provided by JitAi in their own App to implement their own encapsulation.

Quick Start

Creating Instance Elements

Directory Structure

Recommended Directory Structure
auths/
└── loginTypes/
└── MyWeChatLogin/ # Instance element name, customizable
├── e.json # Element definition file
└── MyWeChatLogin.json # Business configuration file, filename matches instance element name

e.json File

e.json Configuration Example
{
"type": "auths.loginTypes.WeChatType",
"title": "WeChat Login",
"appSecret": "Your WeChat application secret",
"backendBundleEntry": ".",
"frontBundleEntry": "./MyWeChatLogin.json"
}

Business Configuration File

MyWeChatLogin.json Configuration Example
{
"authConfig": {
"appId": "Your WeChat application ID",
"appSecret": "Your WeChat application secret"
}
}

Usage Example

Get WeChat Login Instance and Use
# Get WeChat login instance
wechat_auth = app.getElement("auths.loginTypes.MyWeChatLogin")

# Get login configuration information
login_config = wechat_auth.getLoginConfig()

# Handle WeChat QR code callback, get login code
login_result = wechat_auth.getLoginCode("WeChat returned code")

# Bind user WeChat account
wechat_auth.bind("User ID", "WeChat returned code")

Element Configuration

e.json Configuration

Configuration ItemTypeCorresponding Native TypeRequiredDescription
typeStextstrYesFixed value: auths.loginTypes.WeChatType
titleStextstrYesLogin method display name
appSecretStextstrYesWeChat application secret, used for server-side verification
backendBundleEntryStextstrYesBackend entry, fixed value: "."
frontBundleEntryStextstrYesFrontend configuration file path, usually "./config_filename.json"

Business Configuration File

Configuration ItemTypeCorresponding Native TypeRequiredDescription
authConfig.appIdStextstrYesWeChat Open Platform application ID
authConfig.appSecretStextstrYesWeChat Open Platform application secret

Methods

getLoginConfig

Get login configuration information for frontend login option display.

Parameter Details

No parameters

Return Value

FieldTypeCorresponding Native TypeDescription
isActiveBooleanboolWhether login method is enabled
authTypeStextstrAuthentication type identifier
authConfig.appIdStextstrWeChat application ID

Usage Example

Get Login Configuration
wechat_auth = app.getElement("auths.loginTypes.MyWeChatLogin")
config = wechat_auth.getLoginConfig()
print(config)
# Output: {"isActive": True, "authType": "WeChatType", "authConfig": {"appId": "wx123456"}}

getLoginCode

Handle WeChat OAuth callback, get system login code.

Parameter Details

Parameter NameTypeCorresponding Native TypeRequiredDescription
codeStextstrYesAuthorization code returned by WeChat OAuth callback

Return Value

FieldTypeCorresponding Native TypeDescription
loginCodeStextstrSystem login code
corpListJitListlistList of organizations associated with user

Usage Example

Handle WeChat Login Callback
wechat_auth = app.getElement("auths.loginTypes.MyWeChatLogin")
try:
result = wechat_auth.getLoginCode("001234567890abcdef")
print(f"Login Code: {result['loginCode']}")
print(f"Organization List: {result['corpList']}")
except Exception as e:
print(f"Login failed: {e}")

bind

Bind user WeChat account.

Parameter Details

Parameter NameTypeCorresponding Native TypeRequiredDescription
userIdStextstrYesSystem user ID
codeStextstrYesWeChat OAuth authorization code

Return Value

FieldTypeCorresponding Native TypeDescription
successBooleanboolFixed return True, indicates operation success
messageStextstrFixed return "Operation successful"

Usage Example

Bind WeChat Account
wechat_auth = app.getElement("auths.loginTypes.MyWeChatLogin")
try:
result = wechat_auth.bind("user123", "001234567890abcdef")
print("WeChat account bound successfully")
except Exception as e:
print(f"Binding failed: {e}")

unbind

Unbind user WeChat account.

Parameter Details

Parameter NameTypeCorresponding Native TypeRequiredDescription
userIdStextstrYesSystem user ID

Return Value

FieldTypeCorresponding Native TypeDescription
successBooleanboolFixed return True, indicates operation success
messageStextstrFixed return "Operation successful"

Usage Example

Unbind WeChat Account
wechat_auth = app.getElement("auths.loginTypes.MyWeChatLogin")
result = wechat_auth.unbind("user123")
print("WeChat account unbound successfully")

getUserAuthInfo

Get user WeChat authentication information.

Parameter Details

Parameter NameTypeCorresponding Native TypeRequiredDescription
authObjRowDatadictYesWeChat authentication data object

Return Value

FieldTypeCorresponding Native TypeDescription
isActiveBooleanboolWhether authentication is valid

Usage Example

Get User Authentication Information
wechat_auth = app.getElement("auths.loginTypes.MyWeChatLogin")
# Get user data from authentication model
auth_model = app.getElement("auths.loginTypes.WeChatType.WeChatAuthModel")
auth_obj = auth_model.get(filter="Q(userId='user123')", orderList=[])

auth_info = wechat_auth.getUserAuthInfo(auth_obj)
print(f"Authentication Status: {auth_info['isActive']}")

Properties

authType

Authentication type identifier, fixed value is "WeChatType".

authConfig

WeChat application configuration information, containing appId and appSecret.

isActive

Login method enabled status, boolean type.

Advanced Features

Custom Authentication Model

WeChat login uses built-in WeChatAuthModel to store user authentication information, containing the following fields:

WeChatAuthModel Field Structure
# WeChat authentication model fields
auth_model = app.getElement("auths.loginTypes.WeChatType.WeChatAuthModel")

# Main field descriptions
# id: Primary key ID (AutoInt)
# appId: WeChat application ID (Stext)
# userId: System user ID (Stext)
# openId: WeChat user openId (Stext)
# unionId: WeChat user unionId (Stext)

Frontend Login Component Integration

WeChat login Type element provides complete frontend login components, supporting QR code login interface and status management:

Frontend Component Configuration Example
# Get frontend login component configuration
wechat_auth = app.getElement("auths.loginTypes.MyWeChatLogin")
frontend_config = {
"componentType": "WeChatLogin",
"appId": wechat_auth.authConfig["appId"],
"redirectUri": "https://yourdomain.com/auth/callback",
"scope": "snsapi_login"
}

# Configuration when using login component in page
login_component_props = {
"authType": "WeChatType",
"config": frontend_config,
"onSuccess": "handleLoginSuccess",
"onError": "handleLoginError"
}
JitAI AssistantBeta
Powered by JitAI