Scopri chi è la persona
Quando una persona accetta la tua vista nella webapp, sceglie che cosa la vista può sapere di lei. La tua vista riceve sempre il nickname. Riceve anche i campi pubblici del profilo che la persona spunta (nome e cognome, titolo, organizzazione, città e paese) quando il profilo li contiene. Legge tutto in world.me senza chiedere niente alla persona né controllare token. L'esempio dà al suo mondo finto un me proprio per una persona che ha condiviso la sua città e saluta quella persona con il suo nickname.
import { useWorld } from './useWorld.js';
// Building with a fake world needs no account and no network.
// The `me` of the fake world is the person as the webapp would give them, with the nickname and here the city they chose to share.
const club = {
name: 'Book club',
now: { text: 'This month: The Name of the Rose' },
me: { nickname: 'ada', city: 'Siena' },
};
/** The profile fields a person may share with your view, with the words to show for each. */
const FIELDS = { name: 'Name', surname: 'Surname', title: 'Title', organization: 'Organization', city: 'City', country: 'Country' };
export default function App() {
// While building: the fake world. In the webapp: useWorld()
const { world, error } = useWorld({ fake: { world: club } });
if (error) return <p>Could not connect ({error.code}).</p>;
if (!world) return <p>Connecting…</p>;
// `me` holds the nickname, always, and only the fields the person chose to share.
// It is missing with a webapp older than this kit, and the view then greets a guest.
const me = world.me;
const shared = Object.keys(FIELDS).filter((field) => me?.[field]);
return (
<>
<h2>Welcome, {me?.nickname ?? 'guest'}</h2>
<p>{shared.length > 0 ? 'You also shared:' : 'You shared only your nickname.'}</p>
<ul>
{shared.map((field) => <li key={field}>{FIELDS[field]}: {me[field]}</li>)}
</ul>
<p>{world.now?.text}</p>
</>
);
}import { connectToHost } from '@unaiverse/gui-kit';
// Building with a fake world needs no account and no network.
// The `me` of the fake world is the person as the webapp would give them, with the nickname and here the city they chose to share.
const club = {
name: 'Book club',
now: { text: 'This month: The Name of the Rose' },
me: { nickname: 'ada', city: 'Siena' },
};
/** The profile fields a person may share with your view, with the words to show for each. */
const FIELDS = { name: 'Name', surname: 'Surname', title: 'Title', organization: 'Organization', city: 'City', country: 'Country' };
// While building: the fake world. In the webapp: const world = await connectToHost();
const world = await connectToHost({ fake: { world: club } });
const greeting = document.createElement('h2');
const sharedTitle = document.createElement('p');
const sharedList = document.createElement('ul');
const now = document.createElement('p');
document.body.append(greeting, sharedTitle, sharedList, now);
// Called at once, then after every change of the world.
world.subscribe((node) => {
// `me` holds the nickname, always, and only the fields the person chose to share.
// It is missing with a webapp older than this kit, and the view then greets a guest.
const me = node.me;
greeting.textContent = `Welcome, ${me?.nickname ?? 'guest'}`;
const shared = Object.keys(FIELDS).filter((field) => me?.[field]);
sharedTitle.textContent = shared.length > 0 ? 'You also shared:' : 'You shared only your nickname.';
sharedList.replaceChildren(...shared.map((field) => {
const item = document.createElement('li');
item.textContent = `${FIELDS[field]}: ${me[field]}`;
return item;
}));
now.textContent = node.now?.text ?? '';
});Come funziona
world.mecontienenicknamee solo i campi che la persona ha spuntato. Un campo non spuntato manca invece di essere vuoto. Insubscribelo stesso oggetto ènode.me.- La tua vista non riceve mai l'email, la posizione esatta, gli agenti e i mondi della persona o una qualsiasi credenziale perché il consenso della webapp non li offre.
- Una webapp più vecchia di questo kit non dà nessun
me. Leggilo comeworld.me?.nicknamee prepara qualcosa da mostrare per un ospite. await world.identity()si risolve subito con lo stessomee non chiede niente alla persona. Esiste per le viste scritte per un kit precedente e viene rifiutata con il codice'unavailable'quando la webapp non dà nessunme.- In un mondo finto scrivi
meaccanto alnamedel mondo. Senzamela persona ha il nickname'you'e nient'altro.
Sul tuo server
me non porta nessuna firma e chi ha la pagina aperta può cambiarlo come qualsiasi altra cosa che la webapp ha dato alla pagina. Un nickname che la tua vista invia al tuo server va bene per salutare la persona o per ricordare una scelta innocua e mai per concedere qualcosa di valore. Il tuo server mostra come verificare chi è la persona con un accesso sul tuo sito. Un nickname da solo è una chiave debole per quello che il tuo server conserva perché può cambiare e un nickname liberato può passare a qualcun altro (Il nickname).
Vedi anche
- Chi è la persona: tutto l'argomento e che cosa può farne il tuo server.
identityGuiMe: tutti i campi dime.WhoAmI: un componente pronto che mostra chi è la persona.