k.account.user
Current platform user information and user query
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
Server-side KScript (k.api, <script env="server">) | get/exists/emailExists work anytime; current/isOfOrganization/changeOrganization/changePassword require login; generateTestUserToken also requires an active organization | Read current user profile, departments, tenants, and roles; look up accounts; switch orgs |
Quick Start
Inspect the currently logged-in user's profile, organization count, and resolved application role:
k.api.get(() => {
if (!k.account.isLogin) {
return { isLogin: false }
}
const u = k.account.user.current
return {
userName: u.userName,
email: u.email,
isAdmin: u.isAdmin,
orgCount: u.organizations.length,
tenantCount: u.tenants.length,
appRole: u.applicationRole ? u.applicationRole.name : null
}
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
current | UserModel of the currently signed-in user, or null when logged out | UserModel | null |
get(userName) | Looks up a platform user by username (password fields cleared) | User | null |
exists(userName) | Checks whether a username exists | boolean |
emailExists(email) | Checks whether an email address is registered | boolean |
isOfOrganization(organizationName) | Checks whether the current user belongs to an organization (name or ID) | boolean |
changeOrganization(organizationName) | Switches the current user to the specified organization | User | null |
generateTestUserToken(username) | Signs a local test token for username@orgname in the current org | string |
current.changePassword(...) | Updates password (2 args) or UI language preference (1 arg) | boolean |
Methods & Properties
current
Returns the UserModel of the currently logged-in user, or null when unauthenticated. For login-related APIs, see login.md.
| Property | Type | Description |
|---|---|---|
departments | DepartmentModel[] | Departments the user belongs to in the current organization. |
tenants | TenantModel[] | Tenants associated with the user. |
organizations | OrganizationModel[] | Organizations the user is a member of. |
applicationRole | ApplicationRoleModel | null | Resolved application role for the user on the current site, or null if none is assigned. |
Returns: UserModel | null.
k.api.get(() => {
const u = k.account.user.current
if (!u) return null
return {
userName: u.userName,
currentOrgId: String(u.currentOrgId),
departments: u.departments.map((d) => d.name),
appRole: u.applicationRole ? u.applicationRole.name : null
}
})get()
Get the user by username (the password field in the returned object is cleared).
| Parameter | Type | Required | Description |
|---|---|---|---|
userName | string | Yes | Platform username. |
Returns: User | null. Matching user, or null when it does not exist.
k.api.get(() => {
const user = k.account.user.get("some-user")
return user ? { userName: user.userName } : null
})exists() / emailExists()
Checks whether a username or email address already exists in the platform user store.
| Method | Parameters | Returns | Description |
|---|---|---|---|
exists(userName) | userName: string | boolean | Whether the username exists. |
emailExists(email) | email: string | boolean | Whether the email exists. |
Returns: boolean. true when a matching user exists; false otherwise.
k.api.get(() => {
return {
byName: k.account.user.exists("some-user"),
byEmail: k.account.user.emailExists("a@example.com")
}
})isOfOrganization()
Determine whether the current user belongs to the specified organization (name or organization ID string).
| Parameter | Type | Required | Description |
|---|---|---|---|
organizationName | string | Yes | Organization name or organization ID. |
Returns: boolean. Returns true when the current user belongs to the organization.
k.api.get(() => {
return {
inOrg: k.account.user.isOfOrganization("my-org")
}
})changeOrganization()
Switch the current user to the specified organization (must have access to the organization). Note that this method does not reassign context.User or invalidate the cached k.account.user.current on the current request, so k.account.user.current retains the pre-switch user info for the remainder of the request; read currentOrgId from the returned user object instead.
| Parameter | Type | Required | Description |
|---|---|---|---|
organizationName | string | Yes | Organization name or organization ID to switch to. |
Returns: User | null. User information after switching, or null on failure.
k.api.post(() => {
const org = k.request.form.organization
const user = k.account.user.changeOrganization(org)
return {
ok: !!user,
currentOrgId: user ? String(user.currentOrgId) : null
}
})generateTestUserToken()
Generates a locally signed test token for username@orgname using the currently logged-in user's organization (for testing only). Throws an error if unauthenticated or if no current organization is active.
| Parameter | Type | Required | Description |
|---|---|---|---|
username | string | Yes | Prefix name for the test account. |
Returns: string. Generated test token.
k.api.post(() => {
if (!k.account.isLogin) {
return { error: "not logged in" }
}
const token = k.account.user.generateTestUserToken(k.request.form.username)
return { token }
})changePassword()
user.current.changePassword() provides two overloads:
changePassword(oldPassword, newPassword): updates the current user's password.changePassword(language): when called with a single string argument, updates the user's interface language rather than their password.
| Parameter | Type | Required | Description |
|---|---|---|---|
oldPassword | string | Yes (password overload) | Current password. |
newPassword | string | Yes (password overload) | New password. |
language | string | Yes (language overload) | Target UI language code, such as "en" or "zh". |
Returns: boolean. Returns true when the update succeeds.
k.api.post(() => {
k.account.ensureLogin("/login")
if (!k.account.isLogin) return
const ok = k.account.user.current.changePassword(
k.request.form.oldPassword,
k.request.form.newPassword
)
return { ok }
})TypeScript Definitions
interface KUser {
current: UserModel;
isOfOrganization(organizationName: string): boolean;
changeOrganization(organizationName: string): User;
get(userName: string): User | null;
exists(userName: string): boolean;
emailExists(email: string): boolean;
generateTestUserToken(username: string): string;
}
interface ApplicationRoleModel {
name: string;
displayName: string;
permissions: any;
organizationRoleNames: string[];
}
interface UserModel {
userName: string;
firstName: string;
lastName: string;
email: string;
phone: string;
language: string;
currentOrgId: string;
isAdmin: boolean;
id: any;
departments: DepartmentModel[];
tenants: TenantModel[];
organizations: OrganizationModel[];
applicationRole: ApplicationRoleModel | null;
changePassword(oldpassword: string, newPassword: string): boolean;
changePassword(language: string): boolean;
}