UNPKG

@wristband/nextjs-auth

Version:

SDK for integrating your NextJS application with Wristband. Handles user authentication and token management.

46 lines (45 loc) 2.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WristbandService = void 0; // The Wristband Service contains all code for REST API calls to the Wristband platform. const wristband_api_client_1 = require("../api/wristband-api-client"); const constants_1 = require("../utils/constants"); const common_utils_1 = require("../utils/auth/common-utils"); class WristbandService { constructor(wristbandApplicationDomain, clientId, clientSecret) { this.wristbandApiClient = new wristband_api_client_1.WristbandApiClient(wristbandApplicationDomain); this.basicAuthHeaders = { 'Content-Type': constants_1.FORM_URLENCODED_MEDIA_TYPE, Accept: constants_1.JSON_MEDIA_TYPE, Authorization: `Basic ${(0, common_utils_1.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': constants_1.JSON_MEDIA_TYPE, Accept: constants_1.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); } } exports.WristbandService = WristbandService;