Auth from your code

04·Hands-on·15 min

Two tokens, never mixed. The API key identifies your project and rides on every request. The end-user access token identifies one signed-in person and rides in the X-Access-Token header, only on calls that act as that user.

Sign up

const res = await client.auth.signup({ email, password, request })

The response depends on a project setting. With email verification on, there are no tokens yet: verification_required is true and the user receives a 6-digit code by email. Your UI must show a code form and call verify-email with the code and the address it went to. With verification off, the account is usable at once; send the user to login.

Before any of that, DontCode checks the address. One that cannot receive mail, or a throwaway inbox, throws with code UndeliverableEmailException (400). A likely typo of a real domain (gmai.com) throws EmailTypoSuspected (422) and creates nothing yet. Either can carry err.didYouMean; offer it as a one-tap fix. If the person keeps a paused address, call signup again with acceptAsTyped: true, which never lets an undeliverable address through.

Log in

const login = await client.auth.login({ email, password, request })

request is the request your server is answering (a Fetch Request, Node's req, or await headers()): DontCode then limits sign-in tries per visitor, by their address, device and country, instead of counting everyone on your app as one.

Again two successes. If login.tokens is present, you have a session. If mfa_required is true, you hold a short-lived challenge_token instead; show an authenticator-code form and call mfa/challenge to trade it for tokens.

Keep the session

Your app owns the cookie. Store the access token httpOnly so page scripts cannot read it:

headers.append('Set-Cookie', serializeSessionCookie(login.tokens.AccessToken, {
    maxAge: login.tokens.ExpiresIn,
}))

Gate routes

sessionFromCookies(cookieHeader) decodes the token locally, with no network, and returns { status, user }. Use it on every navigation. Before anything sensitive, re-check with getSession({ accessToken, mode: 'verified' }), which asks the gateway and notices revocations. Optimistic for page loads, verified for payments and account changes.

Gate paid features

Payments use the same API key. The owner connects their own PortOne store once, in the console, and DontCode keeps its keys sealed, so there is no merchant key in your environment. The checkout popup runs in the browser; before you grant anything, your server confirms the charge:

const receipt = await client.payments.verify({ paymentId, expectedAmount: 9900, currency: 'KRW' })

For subscriptions, gate on a feature, not a plan name. await client.payments.hasFeature(userId, 'export_pdf') answers from the user's active subscriptions, so it follows upgrades and cancellations with no change to your code. Ask it on the server, the same rule as verified sessions.

Where DontCode fits

Password hashing, verification emails, MFA secrets, and rate limiting on login all happen in the platform's auth service. Your code renders the forms and branches on the responses. The one rule: a single submit can return "done" or "one more step"; never assume one round trip.

Check your understanding

  1. 1.Which header carries the end-user access token?

  2. 2.login() resolves with mfa_required: true. What do you hold?

  3. 3.When should you use getSession in verified mode?

Your task

Backend Project

From your code, sign up a test user with client.auth.signup (or POST /api/v1/auth/signup). The check passes once the new user exists in your project; you do not need to complete verification for this lesson.