@wristband/nextjs-auth
Version:
SDK for integrating your NextJS application with Wristband. Handles user authentication and token management.
42 lines (41 loc) • 1.95 kB
JavaScript
// The Wristband Service contains all code for REST API calls to the Wristband platform.
import { WristbandApiClient } from '../api/wristband-api-client';
import { FORM_URLENCODED_MEDIA_TYPE, JSON_MEDIA_TYPE } from '../utils/constants';
import { encodeBase64 } from '../utils/auth/common-utils';
export class WristbandService {
constructor(wristbandApplicationDomain, clientId, clientSecret) {
this.wristbandApiClient = new WristbandApiClient(wristbandApplicationDomain);
this.basicAuthHeaders = {
'Content-Type': FORM_URLENCODED_MEDIA_TYPE,
Accept: JSON_MEDIA_TYPE,
Authorization: `Basic ${encodeBase64(`${clientId}:${clientSecret}`)}`,
};
}
async getTokens(code, redirectUri, codeVerifier) {
const authData = [
'grant_type=authorization_code',
`code=${code}`,
`redirect_uri=${encodeURIComponent(redirectUri)}`,
`code_verifier=${encodeURIComponent(codeVerifier)}`,
].join('&');
const tokenResponse = await this.wristbandApiClient.post('/oauth2/token', authData, this.basicAuthHeaders);
return tokenResponse;
}
async getUserinfo(accessToken) {
const bearerTokenHeaders = {
Authorization: `Bearer ${accessToken}`,
'Content-Type': JSON_MEDIA_TYPE,
Accept: JSON_MEDIA_TYPE,
};
const userinfo = await this.wristbandApiClient.get('/oauth2/userinfo', bearerTokenHeaders);
return userinfo;
}
async refreshToken(refreshToken) {
const authData = `grant_type=refresh_token&refresh_token=${refreshToken}`;
const tokenResponse = await this.wristbandApiClient.post('/oauth2/token', authData, this.basicAuthHeaders);
return tokenResponse;
}
async revokeRefreshToken(refreshToken) {
await this.wristbandApiClient.post('/oauth2/revoke', `token=${refreshToken}`, this.basicAuthHeaders);
}
}