User API
The User API provides methods for creating, authenticating, and managing user accounts in GUN. Each user is identified by a cryptographic key pair.
gun.user()
Get or create a user context. Only one user can be authenticated per GUN instance.
Get User by Public Key
user.create()
Create a new user account with an alias (username) and password.
Syntax
Parameters
- alias (string): Username for the account
- password (string): Password (minimum 8 characters)
- pair (object): Optional pre-generated key pair
- callback (function): Called when creation completes
- options (object): Optional configuration
check: Validate alias and password (default: true)
already: Allow creating if alias exists (default: false)
Example
How It Works
- Validation: Checks alias exists and password is 8+ characters
- Salt Generation: Creates a random 64-character salt
- Proof of Work: Uses
SEA.work() to derive a key from password + salt (PBKDF2, 100,000 iterations)
- Key Pair Generation: Creates ECDSA and ECDH key pairs via
SEA.pair()
- Encryption: Encrypts private keys with the proof-of-work result
- Storage: Saves user data at
~@alias and ~publicKey nodes
User Data Structure
Reference: ~/workspace/source/sea/create.js:56-74
Using Pre-generated Key Pairs
Auto-login After Creation
If no callback is provided, the user is automatically authenticated:
user.auth()
Authenticate an existing user with alias and password, or with a key pair.
Syntax
Parameters
- alias (string): Username
- password (string): User password
- pair (object): Key pair object with
pub, priv, epub, epriv
- callback (function): Called after authentication
- options (object):
retries: Number of retry attempts (default: 9)
remember: Store credentials in sessionStorage (default: false)
change: New password for password update
Example with Alias/Password
Example with Key Pair
Authentication with a key pair is more reliable:
Session Management
Password Change
user.leave()
Log out the current user and clear session data.
Syntax
Example
user.recall()
Restore a previously authenticated session from sessionStorage.
Syntax
Example
user.is
Check if a user is currently authenticated.
Example
Authentication Flow
Reference: ~/workspace/source/sea/auth.js:43-65
The authentication process:
- Lookup: Find user data at
~@alias
- Parse Auth: Extract encrypted credentials from
auth.ek and auth.s
- Proof of Work: Derive key from password + salt (PBKDF2)
- Decrypt: Decrypt private keys using the derived key
- Validate: Reconstruct key pair and verify
- Session: Store credentials in memory and optionally in sessionStorage
- Event: Emit ‘auth’ event on successful authentication
Private keys are encrypted with AES-GCM using a PBKDF2-derived key from your password. Never share your password or private keys. They cannot be recovered if lost.
Security Notes
- Passwords must be at least 8 characters
- Private keys are never stored in plaintext
- PBKDF2 with 100,000 iterations protects against brute force
- Session data is only stored if
remember option is enabled
- Authentication uses constant-time comparisons where possible
Common Patterns
Registration + Auto-login
Check Auth Status
Handle Auth Events