Command Palette

Search for a command to run...

UnylyUnyly
Назад к скиллам

sfnext-authentication

БесплатноБез исполняемых скриптовНе проверен

Work with Storefront Next's server-only authentication and session architecture (SLAS guest/registered tokens, httpOnly cookies, auth.server.ts middleware)…

SalesforceCommerceCloudот SalesforceCommerceCloud★ 58 звёзд на GitHub

Об этом скилле

Storefront Next Authentication

Authentication is server-only. One middleware (src/middlewares/auth.server.ts, registered in the middleware array of src/root.tsx) runs on every request: it reads cookies, validates or refreshes the SLAS access token, performs a guest login when nothing usable exists, and writes Set-Cookie. Tokens never reach the browser. Components only see a small non-sensitive slice through useAuth().

Read docs/README-AUTH.md in your project for the full reference; this skill is the task-oriented summary. Route modules use server loader/action only, never clientLoader/clientAction.

Request flow

  1. Valid access token (JWT exp in the future): use it.
  2. Expired access token with a refresh token: refresh, rewrite cc-at.
  3. No tokens: guest login.
  4. Registered shopper whose refresh fails: 307 to /login?returnUrl=<path>&error=session_expired (site/locale prefix included) and a fresh guest session is created in parallel. Guests are silently re-issued a guest session.
  5. A SCAPI call returning 401 throws AuthTokenInvalidError; the middleware clears state, re-runs login/refresh and 307-redirects to the same URL. The short-lived cc-auth-recover cookie (30 s) prevents loops.

User type comes from the access-token JWT (registered tokens carry an rcid claim), not from which cookie exists. customerId is not a cookie; it is derived per request from the JWT.

Read auth on the server

import { getAuth } from '@/middlewares/auth.server';
import type { LoaderFunctionArgs } from 'react-router';

export async function loader({ context }: LoaderFunctionArgs) {
    const auth = getAuth(context); // full SessionData, server only
    const isRegistered = auth.userType === 'registered';
    return { isRegistered };       // never return auth.accessToken / refreshToken
}

Read auth in components

import { useAuth } from '@/providers/auth';

function AccountBadge() {
    const auth = useAuth(); // PublicSessionData | undefined
    return auth?.userType === 'registered' ? <span>Welcome back</span> : <SignInLink />;
}

PublicSessionData is exactly customerId, userType, usid, encUserId, trackingConsent. The root loader derives it with getPublicSessionData (from @/middlewares/auth.utils) and passes it to AuthProvider; there is no client getAuth.

Protect a route

Branch inside the server loader. Prefer the existing account layout (src/routes/_app.account.tsx) for anything under /account instead of adding per-route checks.

import { redirect } from 'react-router';
import { getAuth } from '@/middlewares/auth.server';
import { buildUrlFromContext } from '@/lib/url.server';
import { routes } from '@/route-paths';

export async function loader({ context }: LoaderFunctionArgs) {
    if (getAuth(context).userType !== 'registered') {
        throw redirect(buildUrlFromContext(routes.login, context)); // keeps site/locale prefix
    }
    // ...
}

Log in and out

Never write auth cookies yourself. Use the helpers exported from @/middlewares/auth.server:

import { loginRegisteredUser, updateAuth } from '@/middlewares/auth.server';

export async function action({ request, context }: ActionFunctionArgs) {
    const form = await request.formData();
    const tokens = await loginRegisteredUser(context, String(form.get('email')), String(form.get('password')));
    updateAuth(context, tokens);            // re-derives userType/customerId/usid, swaps cc-nx-g for cc-nx
    return redirect('/account');
}

updateAuth accepts a SLAS token response or an updater function (updateAuth(context, (cur) => ({ ...cur, codeVerifier }))). Logout: destroyAuth(context) clears every auth cookie on the response; the shipped src/routes/_empty.logout.ts also calls clients.auth.logout and destroyBasket. Prefer src/lib/api/auth/standard-login.server.ts and social-login.server.ts over hand-rolled flows: they already handle the merge below.

Other exported helpers: loginGuestUser, refreshAccessToken, authorizePasswordless, getPasswordLessAccessToken, getPasswordResetToken, resetPasswordWithToken, requestOtp, verifyOtp, flashAuth(context, message) (clear session plus an error message), clearInvalidSessionAndRestoreGuest(context) (deleted customer or corrupted session).

Guest to registered merge

customerId changes from the guest gcid to the registered rcid at the token swap, and SCAPI rejects the guest id under the registered token. So: snapshot guest-owned data (wishlist) before the swap, then mergeBasket and mergeWishlist after. src/lib/api/auth/social-login.server.ts is the reference implementation. dw_dnt tracking consent is preserved across the swap.

Common SCAPI calls for account data

These are real operations: clients.shopperCustomers.getCustomerOrders, getCustomerProductLists (wishlists), getCustomer. Use the existing helpers (`s

Полный SKILL.md на GitHub

Установить sfnext-authentication в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Без ограничений — скилл может использовать любой инструмент.

Вложенные файлы

evals/trigger-evals.jsonreferences/COOKIES.mdreferences/LOGIN-FLOWS.md

FAQ

Что делает скилл sfnext-authentication?

Work with Storefront Next's server-only authentication and session architecture (SLAS guest/registered tokens, httpOnly cookies, auth.server.ts middleware). Use for getAuth, useAuth, updateAuth, destroyAuth, protecting account routes, login/logout actions, guest-to-registered basket/wishlist merge, "session expired" redirects, 401 recovery loops, cc-at/cc-nx/usid/dwsid cookies, cookie names or expiry, passwordless login, social login, OTP email verification, passkeys, guest order lookup, or refresh-token lifetime settings. Do not use for Turnstile, CSP headers or cookie-domain config (use `storefront-next:sfnext-security`), calling SCAPI clients (use `storefront-next:sfnext-scapi`), or sharing sessions with SFRA (use `storefront-next:sfnext-hybrid-storefronts`).

Как установить скилл sfnext-authentication?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл sfnext-authentication запускает скрипты?

Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.

Похожие скиллы

Сравнить sfnext-authentication с