Options
All
  • Public
  • Public/Protected
  • All
Menu

Module "apiUtils"

Index

Variables

Const GOOGLE_OAUTH2_EXCHANGE_TOKEN_URL

GOOGLE_OAUTH2_EXCHANGE_TOKEN_URL: "https://oauth2.googleapis.com/token" = "https://oauth2.googleapis.com/token"

Functions

createOAuthHeaders

  • createOAuthHeaders(resource?: undefined | string): {}
  • description

    Warning: Please make sure that as of version 0.0.23 we have removed the application/json content type headers from this function. This may cause an issue if you originally didn't construct your request content type headers with application/json.

    example
     fetch(url, {
         headers: createOAuthHeaders(),
     })

    If you require your server to handle authenticating multiple users across many resources (or tables) then pass in the name of the resource, for Example:

    example
     fetch(url, {
         headers: createOAuthHeaders("users"),
     })

    With the resource value, the following headers are constructed:

    example
     {
       "X-Auth-Token" : "<TOKEN>",
       "X-Auth-Resource": "users",
     }

    Parameters

    • Optional resource: undefined | string

      Optional resource name to look up on the server

    Returns {}

    Objects

    • [k: string]: string

exchangeToken

  • exchangeToken(clientId: string, refreshToken: string, clientSecret: string): Promise<String>
  • description

    Function to exchange a refresh token for an access token & sets the access token into local storage..

    example
     exchangeToken(CLIENT_ID, REFRESH_TOKEN, CLIENT_SECRET)
     .then(accessToken => {
          console.log(accessToken) // your access token...
      });

    If you require an access token to run your e2e tests then exchangeToken will set and return a new access token.

    example
        Cypress.Commands.add("loginSSO",  (overrides = {}) => {
          Cypress.log({
              "name": "loginSSO",
          });
          if(!getAccessToken()) {
              exchangeToken(CLIENT_ID, REFRESH_TOKEN, CLIENT_SECRET)
              .then(accessToken => {
                  cy.request({
                      method: 'GET',
                      url: `${API_URL}/staff`,
                      headers: createOAuthHeaders(),
                  });
              });
          }
      });

    Parameters

    • clientId: string

      Google OAuth Client ID

    • refreshToken: string

      Refresh Token (Use Google Oauth Playground to generate a refresh token) See: https://developers.google.com/oauthplayground/

    • clientSecret: string

      Google Api's Client Secret

    Returns Promise<String>

    Promise The string is the new access token also set in local storage

getAccessToken

  • getAccessToken(): string | null
  • description

    Get the stored accessToken

    Returns string | null

    The Access Token or none

isLoggedIn

  • isLoggedIn(): boolean
  • example
     if(isLoggedIn()) { // returns true is accessToken exists in LocalStorage
         // user logged code...
     }

    Returns boolean

logOutOAuthUser

  • logOutOAuthUser(): void
  • example
     logOutOAuthUser() // removes the accessToken from LocalStorage

    Returns void

    void

Generated using TypeDoc