How to use the @commercetools/sdk-auth.TokenProvider function in @commercetools/sdk-auth

To help you get started, we’ve selected a few @commercetools/sdk-auth examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github commercetools / nodejs / integration-tests / sdk / auth.it.js View on Github external
it('should throw an error when refresh token flow fails', async () => {
      const onTokenInfoRefreshedMock = jest.fn()
      const tokenInfo = await authClient.anonymousFlow()
      tokenInfo.expires_at = 0 // emulate expired access_token
      tokenInfo.refresh_token = 'invalid' // emulate broken refresh_token

      const tokenProvider = new TokenProvider(
        {
          sdkAuth: authClient,
          onTokenInfoRefreshed: onTokenInfoRefreshedMock,
        },
        tokenInfo
      )

      try {
        await tokenProvider.getAccessToken()
        throw new Error('Should throw an error')
      } catch (err) {
        expect(err.toString()).toEqual(
          expect.stringContaining(
            'BadRequest: The refresh token was not found. It may have expired.'
          )
        )
github commercetools / nodejs / integration-tests / sdk / auth.it.js View on Github external
it('should refresh expired access_token', async () => {
      const onTokenInfoRefreshedMock = jest.fn()
      const tokenInfo = await authClient.anonymousFlow()
      tokenInfo.expires_at = 0 // emulate expired access_token

      const tokenProvider = new TokenProvider(
        {
          sdkAuth: authClient,
          onTokenInfoRefreshed: onTokenInfoRefreshedMock,
        },
        tokenInfo
      )

      const accessToken = await tokenProvider.getAccessToken()
      expect(accessToken).not.toEqual(tokenInfo.access_token)
      expect(onTokenInfoRefreshedMock).toHaveBeenCalledTimes(1)
      expect(accessToken).not.toEqual(
        onTokenInfoRefreshedMock.mock.calls[0][0].refresh_token
      )

      const newTokenInfo = await tokenProvider.getTokenInfo()
      expect(newTokenInfo.expires_at).not.toEqual(0)
github commercetools / nodejs / integration-tests / sdk / auth.it.js View on Github external
it('should automatically retrieve tokenInfo', async () => {
      const tokenProvider = new TokenProvider({
        sdkAuth: authClient,
        fetchTokenInfo: sdkAuth => sdkAuth.clientCredentialsFlow(),
      })

      const tokenInfo = await tokenProvider.getTokenInfo()
      // check returned properties
      expect(tokenInfo).toHaveProperty('access_token')
      expect(tokenInfo).toHaveProperty('scope', `manage_project:${projectKey}`)
      expect(tokenInfo).toHaveProperty('expires_in')
      expect(tokenInfo).toHaveProperty('token_type', 'Bearer')

      // use client to do a test request
      const client = getApiClient(
        `${tokenInfo.token_type} ${tokenInfo.access_token}`
      )
      const response = await client.execute({

@commercetools/sdk-auth

Auth module for different authentication flows of commercetools platform API

MIT
Latest version published 2 years ago

Package Health Score

64 / 100
Full package analysis

Popular @commercetools/sdk-auth functions