Node.js
NodeJS SDK reference
Use this library to integrate Rownd into your Node.js application. Convenience wrappers are provided for common server implementations.
Installation
Supported frameworks
In addition to supporting Node.js generally, the SDK also provides direct support for the following frameworks:
Don’t see your framework of choice? Open an issue and request it, or contribute it via pull request!
Usage
Vanilla JS (Node.js)
The SDK exposes the following methods to help you validate user tokens, create or update user records, generate sign-in links, and so on.
Here’s a basic usage example:
Express
An authenticate
function is provided for use as Express middleware. It takes
the usual req, res, next
arguments and will call next()
if authentication
succeeds or next(err)
if it fails.
Upon successful authentication, the request will be augmented with a tokenInfo
property containing details about the authenticated token. req.isAuthenticated
will also be set to true
.
Each user’s information is cached in memory for a short period of time to speed up subsequent requests.
See the Express example for a working implementation.
Here’s an example protecting one route:
Here’s an example protecting multiple routes on a certain path prefix:
app.use(‘/protected-path’, authenticate());
The authenticate()
function accepts an optional options
object containing
the following properties:
-
fetchUserInfo: boolean (default: false)
- Iftrue
, the user’s data will be fetched from the Rownd API and annotated on the request object asreq.user
. When present, it will contain a set of key/value pairs that match your application’s schema. The user’s data will be cached for a short period of time to speed up subsequent requests. -
errOnInvalidToken: boolean (default: true)
- Whentrue
, the an error will be passed tonext(err)
if the token fails to validate. Whenfalse
, the token will still be validated, butnext()
will be called without an error.req.isAuthenticated
will befalse
andreq.tokenInfo
will benull
.
API reference
Most methods return a Promise that will resolve with the result of the call or will reject (throw) if an error occurs, so you should be prepared to handle any errors.
Rownd.createInstance(opts)
Creates a new instance of the Rownd client. It requires the following object properties as a single argument:
app_key
- Your Rownd application key.app_secret
- Your Rownd application secret.
Optionally, you can also pass in the following properties:
timeout
- The number of milliseconds to wait before timing out a request. Defaults to 10 seconds. If you’re on a slow network and see any “Request timed out” errors, try increasing this value.
Example:
Once you have an instance, you can use it to call various Rownd APIs or leverage supported frameworks.
instance.express
Provides convenience handlers for the Express framework. See the usage section on Express for more information on how to use it.
instance.validateToken(token: string): Promise<TTokenValidationPayload>
Validates a Rownd bearer token. Returns a promise that resolves to a token validation payload.
In many cases, you’ll retrieve a token from your REST API’s Authorization
header. Be sure to strip off the Bearer
portion of the header and just pass the raw token to this method.
If the validation is successful, the resulting object will look approximately like this:
instance.fetchUserInfo(opts: TFetchUserInfoOpts): Promise<TUserInfo>
Retrieves a user’s profile from Rownd containing fields that match your Rownd application’s schema.
instance.createOrUpdateUser(user: TUser): Promise<void>
Creates or updates a user’s profile in Rownd. The user object must contain an id
property. If the user already exists, the existing profile will be updated. If the user does not exist, a new user/profile will be created.
Example:
instance.deleteUser(userId: String): Promise<void>
Deletes a user and all associated data from Rownd.
instance.createSignInLink(opts: CreateSignInLinkOpts): Promise<string>
Creates a sign-in “magic” link that will automatically sign in a user based on their email address, phone number, etc when they click the link.
Example: