All of W3block API services shares the same authentication that can be obtained using ID services. Basically, ID controls all of users, companies and permissions and besides that, it can generate JWT tokens that can be used to authenticate requests into our entire ecosystem. Above you can see our authentication flow.
How to Authenticate
To authenticate, you must use the ID services API endpoint above:
Authenticating as user
If you want to authenticate some user registered in your tenant base, you can use the following sdk code:
import { W3blockIdSDK } from '@w3block/sdk-id';
const idSdk = new W3blockIdSDK({
baseURL: 'https://api-id.pixway.io',
autoRefresh: true,
});
await idSdk.authenticate({
email: 'test@w3block.id',
password: '<change.me>',
tenantId: '<your tenant id (also known as your company id)>',
});
const jwtToken = idSdk.getAuthToken();
console.log(`My authentication token id: ${jwtToken}`);
Authenticating as tenant
Sometimes you want to integrate some application into our service. This way, is not common to authenticate as a common user, so you want to authenticate as a tenant or basically using a tenant api key and secret. Above you can see some example on how to authenticate using this method.
import { W3blockIdSDK } from '@w3block/sdk-id';
const idSdk = new W3blockIdSDK({
baseURL: 'https://api-id.pixway.io',
autoRefresh: true,
});
await idSdk.authenticate({
key: '<your tenant key>',
secret: '<your tenant secret>',
tenantId: '<your tenant id (also known as your company id)>',
});
const jwtToken = idSdk.getAuthToken();
console.log(`My authentication token id: ${jwtToken}`);
Using the authentication token in our api services
To be authenticated into our api services, you must pass the obtained jwt token into the Authorization request header using the pattern of bearer tokens
Example:
Authorization: Bearer <JWT token>
You can also directly authenticate into our services using the tenant credentials without getting the tenant jwt token. To do that, you just need to inform in the request headers the following params:
x-w3block-id: Your tenant id
x-w3block-api-key: Your tenant api key
x-w3block-secret: Your tenant api secret
This way, the api service that you're calling will automatically handle the tenant authentication without the need of manually get the JWT token as example above.