Auth for Pinoox themes — configured in PHP, zero hardcoding in the SPA.
Token storage, authorized HTTP, me() / logout, and login redirects for Vue, React, and Svelte. Values come from window.__PINOOX__.auth (published by auth.client in app.php).
- Schematics & how it works ← start here
- Setup guide — Independent
- Setup guide — SSO
- Platform variant
- Quick theme module
- Why
auth.client? - App configuration reference
- Theme integration
- HTTP, redirects, adapters
- API reference
- Vite
Two layers always work together:
| Layer | Where | What it controls |
|---|---|---|
transport |
app.php |
Which app owns users / JWT secret / token key on the server |
auth.client |
app.php → __PINOOX__.auth |
What the SPA knows: strategy, login URL, API endpoints |
Each app logs users in by itself. Tokens are not shared.
flowchart LR
User --> Shop
User --> CRM
Shop -.- Sown[own login + token]
CRM -.- Cown[own login + token]
How it works
app.php:auth.mode+auth.key+client => true(notransport).- Strategy is
local— login UI lives in this app. - Login issues a JWT stored under that app’s
auth.key. me/logouthit this app’s API only.
One app (com_pinoox_account) is the login. Shop and CRM reuse the same session.
flowchart LR
Shop -->|no token| Account
CRM -->|no token| Account
Account -->|shared token| Shop
Account -->|shared token| CRM
How it works
- Account owns users +
auth.key(local login). - Shop / CRM:
transport.user => com_pinoox_account+strategy: remote. - Guest → redirect to Account → login → back with
?redirect=. - Same browser key unlocks every dependent app.
| Account | Shop / CRM | |
|---|---|---|
| Login | Yes | No |
| Token key | Defines it | Shared via transport |
| Strategy | local |
remote |
Goal: Shop has its own login. CRM is unrelated (or also independent with a different key).
// apps/com_pinoox_shop/app.php
return [
'package' => 'com_pinoox_shop',
// … theme, router, …
// no 'transport' → local users + local auth
'auth' => [
'mode' => 'jwt',
'key' => 'pinoox_shop_auth',
'lifetime' => 30,
'lifetime_unit' => 'day',
'client' => true,
],
];php pinoox pinker:rebuild com_pinoox_shop -ncd apps/com_pinoox_shop/theme/default
npm install @pinooxhq/auth axios// src/utils/auth/index.js
import axios from 'axios'
import { defineStore } from 'pinia'
import { createAuth, createHttp } from '@pinooxhq/auth'
import { createPiniaAuthStore } from '@pinooxhq/auth/vue'
export const auth = createAuth({ debug: import.meta.env.DEV })
export const http = createHttp({ auth, axios })
export const useAuthStore = createPiniaAuthStore(defineStore, 'auth')<script setup>
import { http, useAuthStore } from '@/utils/auth'
import { useAuthRedirect } from '@pinooxhq/auth/vue'
const store = useAuthStore()
const { redirectBack } = useAuthRedirect()
const onLogin = async () => {
const { data } = await http.post('auth/login', { username, password })
const body = data?.data ?? data
store.login(body.token, body.user)
redirectBack()
}
</script>import { useAuthStore } from '@/utils/auth'
export async function authGuard(to, _from, next) {
const store = useAuthStore()
store.syncTokenFromStorage()
if (!store.isAuth) await store.canUserAccess(true)
if (to.meta.requiresAuth && !store.isAuth) {
return next({ name: 'login', query: { redirect: to.fullPath } })
}
next()
}-
auth.mode+auth.keyon this app -
auth.client => true - Login UI in this theme
- Do not set
strategy: remote - Twig:
pinoox_bootstrapbeforevite_tags
Goal: Account is the only login. Shop and CRM reuse one session.
// apps/com_pinoox_account/app.php
'auth' => [
'mode' => 'jwt',
'key' => 'pinoox_ecosystem', // shared by every dependent
'client' => true, // local strategy
],Account theme: normal local login (same as Independent Step 3), then redirectBack() so ?redirect= returns the user to Shop/CRM.
php pinoox pinker:rebuild com_pinoox_account -n// apps/com_pinoox_shop/app.php
'depends' => ['com_pinoox_account'],
'transport' => [
'user' => 'com_pinoox_account', // inherit users + JWT secret + key
],
'auth' => [
'client' => [
'strategy' => 'remote',
'loginUrl' => '/account/login',
'baseUrl' => '/account/api/v1',
'endpoints' => [
'login' => 'auth/login',
'logout' => 'auth/logout',
'me' => 'auth/get',
],
],
],// apps/com_pinoox_crm/app.php
'depends' => ['com_pinoox_account'],
'transport' => ['user' => 'com_pinoox_account'],
'auth' => [
'client' => [
'strategy' => 'remote',
'loginUrl' => '/account/login',
'baseUrl' => '/account/api/v1',
'endpoints' => [
'me' => 'auth/get',
'logout' => 'auth/logout',
],
],
],php pinoox pinker:rebuild com_pinoox_shop -n
php pinoox pinker:rebuild com_pinoox_crm -nSame utils/auth as Independent — no hardcoded key. createAuth() reads remote settings from __PINOOX__.auth.
export const auth = createAuth({ debug: import.meta.env.DEV })
export const http = createHttp({ auth, axios })
export const useAuthStore = createPiniaAuthStore(defineStore, 'auth')Guard — redirect to Account when guest:
import { auth, useAuthStore } from '@/utils/auth'
export async function authGuard(to, _from, next) {
const store = useAuthStore()
store.syncTokenFromStorage()
if (!store.isAuth) await store.canUserAccess(true)
if (to.meta.requiresAuth && !store.isAuth) {
auth.redirectToLogin() // → /account/login?redirect=…
return
}
next()
}Do not build a login form on Shop/CRM for SSO — only Account has the form.
-
transport.user→com_pinoox_account -
auth.client.strategy=remote -
loginUrl=/account/login -
baseUrl+endpointsforme/logout - Do not set a different
auth.key(unless you want a separate session) - Pinker rebuild after
app.phpchanges
| Independent | SSO | |
|---|---|---|
| Example | Shop alone; CRM alone | Account ← Shop, CRM |
transport.user |
— | com_pinoox_account |
| Login UI | Each app | Account only |
| Browser key | Per app (pinoox_shop_auth, …) |
One key (pinoox_ecosystem) |
auth.client |
true |
map with strategy: remote |
Same idea as SSO, but the login owner is the platform auth app (Manager), not Account. Use for admin tools that must not share the customer Account session.
flowchart LR
CRM -->|no token| Manager
Inventory -->|no token| Manager
Manager -->|one token| CRM
Manager -->|one token| Inventory
// dependent admin app
'transport' => ['user' => 'platform'],
'auth' => [
'client' => [
'strategy' => 'remote',
'loginUrl' => '/manager/login',
'baseUrl' => '/crm/api/v1',
'endpoints' => [
'me' => 'auth/get',
'logout' => 'auth/logout',
],
],
],transport.user |
Auth / users come from |
|---|---|
omitted / local |
This app only (independent) |
'com_pinoox_account' |
Account SSO |
'platform' |
Platform / Manager SSO |
Same for all topologies — only __PINOOX__.auth changes.
cd apps/com_pinoox_shop/theme/default
npm install @pinooxhq/auth axios
php pinoox pinker:rebuild com_pinoox_shop -nsrc/utils/auth/index.js:
import axios from 'axios'
import { defineStore } from 'pinia'
import { createAuth, createHttp } from '@pinooxhq/auth'
import { createPiniaAuthStore } from '@pinooxhq/auth/vue'
export const auth = createAuth({ debug: import.meta.env.DEV })
export const http = createHttp({ auth, axios })
export const useAuthStore = createPiniaAuthStore(defineStore, 'auth')import { auth, useAuthStore } from '@/utils/auth'
const store = useAuthStore()
await store.canUserAccess(true)
if (!store.isAuth) {
auth.redirectToLogin() // remote → loginUrl; local → your login route
}PHP already owns mode, key, JWT secret, and login URLs. The SPA must not invent them.
auth.client publishes a safe subset to window.__PINOOX__.auth (via pinoox_bootstrap()).
| Stays on server | May go to the client |
|---|---|
jwt_secret, lifetimes, … |
mode, key, provider, source |
optional: strategy, loginUrl, baseUrl, endpoints |
app.php auth.client → pinker + pinoox_bootstrap() → __PINOOX__.auth
↓
createAuth() / createHttp()
Twig (order matters):
{{ pinoox_bootstrap(bootstrap|default({}))|raw }}
{{ vite_tags() }}The server always resolves a base payload from auth + transport:
// Always available on the server (AuthConfig::forClient base)
[
'mode' => 'jwt', // jwt | cookie | session
'key' => 'pinoox_ecosystem', // localStorage / cookie name
'provider' => 'com_pinoox_account', // package that owns auth (or this app)
'source' => 'com_pinoox_account', // transport auth source (or null)
]auth.client decides how much of that (and extras) the browser may see.
// e.g. com_pinoox_shop — independent login
'auth' => [
'mode' => 'jwt',
'key' => 'pinoox_shop_auth',
'client' => true,
],// window.__PINOOX__.auth
{ mode: 'jwt', key: 'pinoox_shop_auth', provider: 'com_pinoox_shop', source: null }Use for a normal local login app. createAuth() gets mode + key with no JS hardcoding.
'auth' => [
'mode' => 'jwt',
'key' => 'pinoox_crm_auth',
'client' => false,
],__PINOOX__.auth is omitted. You must pass everything in JS:
createAuth({ mode: 'jwt', key: 'pinoox_crm_auth', strategy: 'local' })Use only when you deliberately keep auth config off the page (or hydrate from elsewhere).
A PHP list (array_is_list) means: from the base payload, keep only these keys. Nothing else is added.
// Account (or Shop) — only mode + key in the page; hide provider/source package names
'auth' => [
'mode' => 'jwt',
'key' => 'pinoox_ecosystem',
'client' => ['mode', 'key'],
],// window.__PINOOX__.auth
{ mode: 'jwt', key: 'pinoox_ecosystem' }
// no provider, no sourceWhen to use
- The SPA only needs
mode+keyto store/send the token - You do not want
provider/sourcepackage ids visible in page HTML - Strategy / login URLs are either defaults (
local) or set in JS / via a map (form 4)
Allowed whitelist names (only these exist on the base):
| Field | Purpose |
|---|---|
mode |
How the token is stored / sent |
key |
Storage key (localStorage name) |
provider |
Auth-owning package |
source |
Transport auth source package |
Unknown names are ignored. You cannot whitelist strategy / loginUrl here — those are not base fields; use a map (below).
Another example — token key only (mode defaults to jwt in createAuth if missing, but publishing mode is safer):
'client' => ['key'],
// → __PINOOX__.auth = { key: 'pinoox_ecosystem' }An associative array merges onto the full base. Use this for strategy, loginUrl, baseUrl, endpoints:
// com_pinoox_shop — depends on Account
'auth' => [
'client' => [
'strategy' => 'remote',
'loginUrl' => '/account/login',
'baseUrl' => '/account/api/v1',
'endpoints' => [
'me' => 'auth/get',
'logout' => 'auth/logout',
],
],
],// window.__PINOOX__.auth
{
mode: 'jwt',
key: 'pinoox_ecosystem', // inherited via transport from Account
provider: '…',
source: '…',
strategy: 'remote',
loginUrl: '/account/login',
baseUrl: '/account/api/v1',
endpoints: { me: 'auth/get', logout: 'auth/logout' },
}List vs map (easy rule)
| Shape | PHP | Meaning |
|---|---|---|
| List | ['mode', 'key'] |
Filter base → only these keys |
| Map | ['strategy' => 'remote', …] |
Keep all base keys, then merge extras |
// ❌ Wrong — this is a list, so "strategy" is ignored (not a base field)
'client' => ['mode', 'key', 'strategy'],
// ✅ Right — map merges strategy onto the full base
'client' => [
'strategy' => 'remote',
'loginUrl' => '/account/login',
],Aliases for auth.client: via, expose, bootstrap.
| Value | Meaning |
|---|---|
local (default) |
This app owns login |
remote |
Redirect to another app’s login |
Aliases: provider → local, consumer / external → remote.
Prefer a shared API prefix (here: Account as provider):
'baseUrl' => '/account/api/v1',
'endpoints' => [
'me' => 'auth/get', // → /account/api/v1/auth/get
'logout' => 'auth/logout',
],Or absolute paths (no baseUrl):
'endpoints' => [
'me' => '/account/api/v1/auth/get',
'logout' => '/account/api/v1/auth/logout',
],For a dependent that validates the session on its own API (same shared key):
// com_pinoox_crm with transport.user => platform or account
'baseUrl' => '/crm/api/v1',
'endpoints' => [
'me' => 'auth/get',
'logout' => 'auth/logout',
],Rules:
https://…or path starting with/→ used as-is- relative +
baseUrl→ joined - relative without
baseUrl→ left as-is
| mode | Storage | Request |
|---|---|---|
jwt |
token under auth.key |
Authorization: Bearer … |
cookie / session |
optional | cookies (credentials: include) |
<script setup>
import { http, useAuthStore } from '@/utils/auth'
import { useAuthRedirect } from '@pinooxhq/auth/vue'
const store = useAuthStore()
const { redirectBack } = useAuthRedirect()
const onLogin = async () => {
const { data } = await http.post('auth/login', { username, password })
const body = data?.data ?? data
store.login(body.token, body.user)
redirectBack()
}
</script>import { auth, useAuthStore } from '@/utils/auth'
export async function authGuard(to, _from, next) {
const store = useAuthStore()
store.syncTokenFromStorage()
if (!store.isAuth) {
await store.canUserAccess(true)
}
if (to.meta.requiresAuth && !store.isAuth) {
auth.redirectToLogin()
return
}
next()
}On remote apps (Shop / CRM), auth.redirectToLogin() goes to loginUrl?redirect=<current path>.
createHttp({ auth, axios }) wires:
Authorizationfromauth.getAuthHeader()401→auth.notifyUnauthorized()- shared transport for
login/me/logout
baseURL defaults to __PINOOX__.url.API.
auth.redirectToLogin() // remote → other app; local → loginUrl / endpoints.login
auth.redirectBack() // after login → safe ?redirect= (SITE origin aware for Vite HMR)import { useAuthRedirect } from '@pinooxhq/auth/vue'
const { redirectQuery, redirectBack, returnUrl } = useAuthRedirect()import { useAuth, useAuthRedirect, createPiniaAuthStore } from '@pinooxhq/auth/vue'
import { AuthProvider, useAuth } from '@pinooxhq/auth/react'
import { createAuthStore, createAuthRedirect } from '@pinooxhq/auth/svelte'auth.login({ username, password }) // local only; remote → redirect
auth.me()
auth.logout()
auth.getToken() / auth.setToken(t) / auth.clearToken()
auth.getAuthHeader()
auth.redirectToLogin() / auth.redirectBack()
auth.getReturnUrl() / auth.getRedirectQuery()
auth.notifyUnauthorized()
http.get / http.post / …@pinooxhq/vite-plugin — Twig shell + Vite entry so __PINOOX__ (and auth.client) loads before the SPA.
{{ pinoox_bootstrap(bootstrap|default({}))|raw }}
{{ vite_tags() }}MIT