Skip to content

Authentication and Authorization ​

Platform user login, session checks, and access guards (for storefront customer accounts, use k.commerce.customer)

Execution ContextPrerequisitesTypical 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 ​

js
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 / PropertyDescriptionReturns
k.account.isLoginCheck whether the current request is logged inboolean
k.account.login(username, password, code?)Sign in with username and passwordUser | null
k.account.logout(returnUrl?)Sign out, optionally redirecting afterwardvoid
k.account.ensureLogin(redirectUrl)Redirect (302) and end response when unauthenticatedvoid

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.

js
k.api.get("isLogin", () => {
    return { isLogin: k.account.isLogin }
})

k.account.login() ​

Log in using username and password.

ParameterTypeRequiredDescription
usernamestringYesUsername.
passwordstringYesPassword.
codestringNoVerification code, etc.

Returns: User | null. User object on success; null on failure.

js
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.

ParameterTypeRequiredDescription
returnUrlstringNoURL to redirect to after logout.

Returns: void.

js
// 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

  1. Return nothing when passing returnUrl: k.account.logout("/") sets HTTP status 302 and Location: /. Returning a non-null value from a k.api handler causes the default result handler to call Redirect(302, response.Data), overwriting Location with the serialized JSON string.
  2. k.account.isLogin stays unchanged in the current request: logout() only deletes the auth JWT cookie for subsequent requests and does not clear context.User. Within the same request, k.account.isLogin still evaluates to its incoming value (such as true).

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).

ParameterTypeRequiredDescription
redirectUrlstringYesURL to redirect to when the user is not logged in.

Returns: void.

js
k.api.get("profile", () => {
    k.account.ensureLogin("/login?return=" + encodeURIComponent(k.request.url))
    if (!k.account.isLogin) {
        return
    }
    return k.account.user.current
})