hoodie-client-account
Account client API for the browser
hoodie-client-account
is a JavaScript front-end client for
the Account JSON API.
It persists session information in localStorage and provides
front-end friendly APIs for things like creating a user account,
confirming, resetting a password, changing profile information,
or closing the account.
There is also an admin-specific account client
Example
// Account loaded via <script> or require('hoodie-client-account')var account = 'https://example.com/account/api' if account account
API
- Constructor
- account.id
- account.username
- account.validate
- account.isSignedIn
- account.hasInvalidSession
- account.signUp
- account.signIn
- account.signOut
- account.destroy
- account.get
- account.fetch
- account.update
- account.profile.get
- account.profile.fetch
- account.profile.update
- account.request
- account.on
- account.one
- account.off
- Events
- Requests
Constructor
options
Argument | Type | Description | Required |
---|---|---|---|
options.url |
String | Path or full URL to root location of the account JSON API | Yes |
options.id |
String | The initial account id can be passed. Useful for apps that can be used locally without an account. | Defaults to random id |
options.cacheKey |
String | Name of localStorage key where to persist the session state. | Defaults to \account |
options.validate |
Function | Optional function to validate account before sending sign up / sign in / update requests | No |
Returns account
API.
Example
url: '/api' id: 'user123' cacheKey: 'myapp.session' { if optionsusernamelength < 3 throw 'Username must have at least 3 characters' }
account.id
Read-only. Returns the account id. If accessed for the first time, a new id gets generated and persisted in localStorage.
account.username
Read-only. Returns the username if signed in, otherwise undefined
.
account.validate
Calls the function passed into the Constructor.
Returns a Promise that resolves to true
by default
account
Argument | Type | Required |
---|---|---|
options.username |
String | No |
options.password |
String | No |
options.profile |
Object | No |
Resolves with an argument.
Rejects with any errors thrown by the function originally passed into the Constructor.
Example
var account = url: '/api' cacheKey: 'app.session' { if optionspasswordlength < 8 throw 'password should contain at least 8 characters' } account
account.isSignedIn
Returns true
if user is currently signed in, otherwise false
.
account
account.hasInvalidSession
Checks account.session.invalid
property.
Returns true
if user has invalid session, otherwise undefined
.
account
account.signUp
Creates a new user account on the Hoodie server. Does not sign in the user automatically, account.signIn must be called separately.
account
Argument | Type | Required |
---|---|---|
accountProperties.username |
String | Yes |
accountProperties.password |
String | Yes |
Resolves with accountProperties
:
Rejects with:
InvalidError |
Username must be set |
---|---|
SessionError |
Must sign out first |
ConflictError |
Username <username> already exists |
ConnectionError |
Could not connect to server |
Example
account
🐕 Implement account.signUp with profile: {...} option: #11
account.signIn
Creates a user session
account
Argument | Type | Description | Required |
---|---|---|---|
options.username |
String | - | Yes |
options.password |
String | - | Yes |
Resolves with accountProperties
:
Rejects with:
UnconfirmedError |
Account has not been confirmed yet |
---|---|
UnauthorizedError |
Invalid Credentials |
Error |
A custom error set on the account object, e.g. the account could be blocked due to missing payments |
ConnectionError |
Could not connect to server |
Example
account
account.signOut
Deletes the user’s session
account
Resolves with sessionProperties
like account.signin,
but without the session id:
Rejects with:
Error |
A custom error thrown in a before:signout hook |
---|
Example
account
account.destroy
Destroys the account of the currently signed in user.
account
Resolves with sessionProperties
like account.signin,
but without the session id:
Rejects with:
Error |
A custom error thrown in a before:destroy hook |
---|---|
ConnectionError |
Could not connect to server |
Example
account
account.get
Returns account properties from local cache.
account
Argument | Type | Description | Required |
---|---|---|---|
properties |
String or Array of strings | When String, only this property gets returned. If array of strings, only passed properties get returned | No |
Returns object with account properties, or undefined
if not signed in.
Examples
var properties = accountvar createdAt = accountvar properties = account
account.fetch
Fetches account properties from server.
account
Argument | Type | Description | Required |
---|---|---|---|
properties |
String or Array of strings | When String, only this property gets returned. If array of strings, only passed properties get returned. Property names can have `.` separators to return nested properties. | No |
Resolves with accountProperties
:
Rejects with:
UnauthenticatedError |
Session is invalid |
---|---|
ConnectionError |
Could not connect to server |
Examples
accountaccountaccount
account.update
Update account properties on server and local cache
account
Argument | Type | Description | Required |
---|---|---|---|
changedProperties |
Object | Object of properties & values that changed. Other properties remain unchanged. | No |
Resolves with accountProperties
:
Rejects with:
UnauthenticatedError |
Session is invalid |
---|---|
InvalidError |
Custom validation error |
ConflictError |
Username <username> already exists |
ConnectionError |
Could not connect to server |
Example
account
account.profile.get
accountprofile
Returns profile properties from local cache.
accountprofile
Argument | Type | Description | Required |
---|---|---|---|
properties |
String or Array of strings | When String, only this property gets returned. If array of strings, only passed properties get returned. Property names can have `.` separators to return nested properties. | No |
Returns object with profile properties, or undefined
if not signed in.
Examples
var properties = accountprofilevar fullname = accountprofilevar properties = accountprofile
account.profile.fetch
Fetches profile properties from server.
accountprofile
Argument | Type | Description | Required |
---|---|---|---|
properties |
String or Array of strings | When String, only this property gets returned. If array of strings, only passed properties get returned. Property names can have `.` separators to return nested properties. | No |
Resolves with profileProperties
:
Rejects with:
UnauthenticatedError |
Session is invalid |
---|---|
ConnectionError |
Could not connect to server |
Examples
accountaccountaccount
account.profile.update
Update profile properties on server and local cache
accountprofile
Argument | Type | Description | Required |
---|---|---|---|
changedProperties |
Object | Object of properties & values that changed. Other properties remain unchanged. | No |
Resolves with profileProperties
:
Rejects with:
UnauthenticatedError |
Session is invalid |
---|---|
InvalidError |
Custom validation error |
ConnectionError |
Could not connect to server |
Example
accountprofile
account.request
Sends a custom request to the server, for things like password resets, account upgrades, etc.
account
Argument | Type | Description | Required |
---|---|---|---|
properties.type |
String | Name of the request type, e.g. "passwordreset" | Yes |
properties |
Object | Additional properties for the request | No |
Resolves with requestProperties
:
Rejects with:
ConnectionError |
Could not connect to server |
---|---|
NotFoundError |
Handler missing for "passwordreset" |
InvalidError |
Custom validation error |
Example
account
account.on
account
Example
account
account.one
Call function once at given account event.
account
Example
account
account.off
Removes event handler that has been added before
account
Example
hoodie
Events
Event | Description | Arguments |
---|---|---|
signup |
New user account created successfully | accountProperties with .session property |
signin |
Successfully signed in to an account | accountProperties with .session property |
signout |
Successfully signed out | accountProperties with .session property |
passwordreset |
Email with password reset token sent | |
unauthenticate |
Server responded with "unauthenticated" when checking session | |
reauthenticate |
Successfully signed in with the same username (useful when session has expired) | accountProperties with .session property |
update |
Successfully updated an account's properties | accountProperties with .session property |
Requests
Hoodie comes with a list of built-in account requests, which can be disabled, overwritten or extended in hoodie-server-account
When a request succeeds, an event with the same name as the request type gets
emitted. For example, account.request({type: 'passwordreset', contact: 'pat@example.com')
triggers a passwordreset
event, with the requestProperties
passed as argument.
passwordreset |
Request a password reset token |
---|
Testing
In Node.js
Run all tests and validate JavaScript Code Style using standard
npm test
To run only the tests
npm run test:node
Contributing
Have a look at the Hoodie project's contribution guidelines. If you want to hang out you can join our Hoodie Community Chat.