Authentication and Authorization
Platform user login, session checks, and access guards (for storefront customer accounts, use k.commerce.customer)
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
Api, CodeBlock, <script env="server"> | k.account targets Kooboo platform/org users (not storefront customers) | Check login state, sign in, guard endpoints, and sign out |
Quick Start
k.api.get("me", () => {
if (!k.account.isLogin) {
k.response.statusCode(401)
return { authenticated: false, error: "login required" }
}
return { authenticated: true, user: k.account.user.current }
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
k.account.isLogin | Check whether the current request is logged in | boolean |
k.account.login(username, password, code?) | Sign in with username and password | User | null |
k.account.logout(returnUrl?) | Sign out, optionally redirecting afterward | void |
k.account.ensureLogin(redirectUrl) | Redirect (302) and end response when unauthenticated | void |
Overview
Operations related to "whether the current platform user is logged in" in Kooboo are mainly completed through k.account; see k.session for request-level sessions, k.cookie for cookies, and k.security for encryption and tokens. For storefront buyer login, use k.commerce.customer.
k.account.isLogin
Determine whether the current request has been logged in.
Parameters: none.
Returns: boolean. true when the current request is logged in.
k.api.get("isLogin", () => {
return { isLogin: k.account.isLogin }
})k.account.login()
Log in using username and password.
| Parameter | Type | Required | Description |
|---|---|---|---|
username | string | Yes | Username. |
password | string | Yes | Password. |
code | string | No | Verification code, etc. |
Returns: User | null. User object on success; null on failure.
k.api.post("login", () => {
const username = k.request.form.username
const password = k.request.form.password
k.account.login(username, password)
return { isLogin: k.account.isLogin }
})k.account.logout()
Log out, optionally jump to the specified URL.
| Parameter | Type | Required | Description |
|---|---|---|---|
returnUrl | string | No | URL to redirect to after logout. |
Returns: void.
// Sign out and redirect (302) to "/": return nothing so the Location header is preserved
k.api.get("logout", () => {
k.account.logout("/")
return
})
// Sign out and return JSON: omit returnUrl and do not read k.account.isLogin in the same request
k.api.post("logout-json", () => {
k.account.logout()
return { loggedOut: true }
})Important logout() behavior
- Return nothing when passing
returnUrl:k.account.logout("/")sets HTTP status302andLocation: /. Returning a non-null value from ak.apihandler causes the default result handler to callRedirect(302, response.Data), overwritingLocationwith the serialized JSON string. k.account.isLoginstays unchanged in the current request:logout()only deletes the auth JWT cookie for subsequent requests and does not clearcontext.User. Within the same request,k.account.isLoginstill evaluates to its incoming value (such astrue).
k.account.ensureLogin()
Sets a 302 redirect and ends the response when the user is not logged in (commonly used for protected pages or API entry points).
| Parameter | Type | Required | Description |
|---|---|---|---|
redirectUrl | string | Yes | URL to redirect to when the user is not logged in. |
Returns: void.
k.api.get("profile", () => {
k.account.ensureLogin("/login?return=" + encodeURIComponent(k.request.url))
if (!k.account.isLogin) {
return
}
return k.account.user.current
})Related Docs
- k.account — Complete account, user, organization, and OAuth APIs
- k.session — Server-side session storage
- k.security — Encryption, hashing, and JWT
- Quick Start