1.0.23 • Published 10 months ago

@tryauth/tryauth-client-ts v1.0.23

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

tryauth-client-ts

NPM version

Index

Install

From npm:

npm install @tryauth/tryauth-client-ts

After installing the tryauth-client-ts module using npm, you'll need to import it using:

import TryAuth from '@tryauth/tryauth-client-ts';

Implicit Flow

Provides support for implicit authentication flow.

Initialize

The Authorize will redirect to the Issuer Endpoint than the user can login.

const tryAuth: TryAuth = new TryAuth();
tryAuth.Authorize({
    ClientId: '<contact us to get a trial ClientId>',
    IssuerEndpoint: '<contact us to get a trial Issuer Endpoint>',
    ResponseType: 'token id_token', // return id_token and access_token in querystring
    Scopes: 'openid email'
    // RedirectUri: ''  it's not required, window.location it's the default value
});

Parameters

OptionTypeDescription
ClientIdstring (required)The Client ID found on your Application settings page.
IssuerEndpointstring (required)Your TryAuth account domain such as 'example.tryauth.com' or 'example.tryauth.com'.
ResponseTypestring (required)Response type for all authentication requests. Only supports token id_token.
Scopesstring (required)The default scopes used for all authorization requests.
RedirectUristringThe URL where TryAuth will call back to with the result of a successful or failed authentication.

Check

Allows to acquire a new token and/or access token for a user who has already authenticated against TryAuth for your domain. If the user is not authenticated, you will receive an error.

const tryAuth: TryAuth = new TryAuth();
const tryAuthAuthorizationResponse = await tryAuth.CheckAuthorize();

console.log('AccessToken=' + tryAuthAuthorizationResponse.AccessToken);
console.log('IdToken=' + tryAuthAuthorizationResponse.IdToken);
console.log('ExpiresAt=' + tryAuthAuthorizationResponse.ExpiresAt);
console.log('Email=' + tryAuthAuthorizationResponse.Email);
console.log('Error=' + tryAuthAuthorizationResponse.Error);

Authorization Code Flow

Provides support for authorization_code authentication flow with PKCE (Proof Key for Code Exchange).

Initialize

The GetAuthorizationCode function will redirect to the Issuer Endpoint than the user can login. After login the response callback URL will store the code in the browser's local storage.

const tryAuth: TryAuth = new TryAuth();
tryAuth.GetAuthorizationCode({
    ClientId: '<contact us to get a trial ClientId>',
    IssuerEndpoint: '<contact us to get a trial Issuer Endpoint>',
    ResponseType: 'code',
    Scopes: 'openid email profile offline_access',
    // RedirectUri: ''  it's not required, window.location it's the default value
});

Parameters

OptionTypeDescription
ClientIdstring (required)The Client ID found on your Application settings page.
IssuerEndpointstring (required)Your TryAuth account domain such as 'example.tryauth.com' or 'example.tryauth.com'.
ResponseTypestring (required)Response type for all authentication requests. Only supports code.
Scopesstring (required)The default scopes used for all authorization requests. Enable refresh token adding offline_access scope.
RedirectUristringThe URL where TryAuth will call back to with the result of a successful or failed authentication.

Get Access Token, Id Token and optional Refresh Token code

After you login and the code already is stored in the browser's local storage you can get the access_token, id_token, refresh_token (optional, dependes on the offline_access scope) using the GetSilentAuthorizationCodeToken function.

The code only can be used one time.

const tryAuth: TryAuth = new TryAuth();
const tryAuthAuthorizationCodeTokenResponse = tryAuth.GetSilentAuthorizationCodeToken({
    ClientId: '<contact us to get a trial ClientId>',
    ClientSecret: '<contact us to get a trial ClientSecret>',
    IssuerEndpoint: '<contact us to get a trial Issuer Endpoint>',
    ResponseType: 'code'
});

Parameters

OptionTypeDescription
ClientIdstring (required)The Client ID found on your Application settings page.
ClientSecretstring (required)The Client Secret found on your Application settings page.
IssuerEndpointstring (required)Your TryAuth account domain such as 'example.tryauth.com' or 'example.tryauth.com'.
ResponseTypestring (required)Response type for all authentication requests. Only supports code.

Return

The tryAuthAuthorizationCodeTokenResponse object contains some properties like:

Property
AccessToken
IdToken
RefreshToken
TokenType
Error

Renew the Access Token using Refresh Token

If you choose to receive the Refresh Token using the offline_access scope, you can renew your Access Token using the GetAuthorizationCodeRefreshToken function.

const tryAuth: TryAuth = new TryAuth();
const tryAuthAuthorizationCodeTokenResponse = tryAuth.GetAuthorizationCodeRefreshToken({
    ClientId: '<contact us to get a trial ClientId>',
    ClientSecret: '<contact us to get a trial ClientSecret>',
    IssuerEndpoint: '<contact us to get a trial Issuer Endpoint>',
    RefreshToken: '<the refresh token code you receive in the function above>'
});

Parameters

OptionTypeDescription
ClientIdstring (required)The Client ID found on your Application settings page.
ClientSecretstring (required)The Client Secret found on your Application settings page.
IssuerEndpointstring (required)Your TryAuth account domain such as 'example.tryauth.com' or 'example.tryauth.com'.
RefreshTokenstring (required)The refresh token code you receive in the function above.

Return

The tryAuthAuthorizationCodeTokenResponse object is the same of GetSilentAuthorizationCodeToken function above.

Logout

For logout use the LogoutEndSessionAuthorizationCode function.

const tryAuth: TryAuth = new TryAuth();
tryAuth.LogoutEndSessionAuthorizationCode({
    ResponseType: 'code',
    IssuerEndpoint: '<contact us to get a trial Issuer Endpoint>',
    RedirectUri: '<after logout the url return>',
    IdToken: '<id_token>',
    State: 'the state in url'
});

Parameters

OptionTypeDescription
ResponseTypestring (required)code
IssuerEndpointstring (required)Your TryAuth account domain such as 'example.tryauth.com' or 'example.tryauth.com'.
RedirectUristring (required)After logout TryAuth will redirect to this URL.
IdTokenstring (required)The id_token you receive when you get the token.
Statestring (required)After log in the state will be in the URL.
1.0.23

10 months ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.8

2 years ago