2.5.318 • Published 21 days ago

@authress/login v2.5.318

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
21 days ago

Authress Login SDK for UIs

The Authress Universal Login SDK for javascript app websites and user identity authentication. Used to integrate with the authentication as a service provider Authress at https://authress.io.

Usage

npm install @authress/login

Then required the package:

import { LoginClient } from '@authress/login';

Troubleshooting usage

Troubles with esbuild or another bundler, checkout the build tools troubleshooting page.

Getting Started

Part 0: Setup Authress Login

You'll want to create:

Part 1: Web App UI

On every route change check to see if the user exists, and if they don't redirect them to a login prompt.

import { LoginClient } from '@authress/login';

// What is my applicationId => https://authress.io/app/#/manage?focus=applications
// What is my authressApiUrl? => https://authress.io/app/#/setup?focus=domain
const loginClient = new LoginClient({ authressApiUrl: 'https://login.application.com', applicationId: 'YOUR_APPLICATION_ID' });
const isUserLoggedIn = await loginClient.userSessionExists();
if (!isUserLoggedIn) {
  window.location.assign('/login');
}

In your app's login screen when the user selects how they would like to login, direct them there. And also specify where you would like Authress to redirect the user to after login. By default this is the user's current location.

await loginClient.authenticate({ connectionId: 'SELECTED_CONNECTION_ID', redirectUrl: window.location.href });
// Or if you know which tenant the user wants to log in with:
await loginClient.authenticate({ tenantLookupIdentifier: 'tenant-subdomain.app.com', redirectUrl: window.location.href });
return;

Then get the user access token associated with the user's login to be used for authorization in the next part:

const userToken = await loginClient.ensureToken();

Part 2: User Authentication in Service APIs

To authenticate the user in your service API, the token generated by the library in Part 1 needs to be passed to your service. The standard is pulling it from the loginClient, and putting it into an Authorization header:

const userToken = await loginClient.ensureToken();
const result = await fetch('https://api.application.com/v1/route', {
  method: 'GET',
  headers: {
    'Authorization': `Bearer ${userToken}`
  }
})

On the service API side, pull in the Authress service client companion library, and then verify the access token.

  • First install the companion library: npm install authress-sdk
  • Then verify the incoming tokens from the Authorization header:
const { TokenVerifier } = require('authress-sdk');

try {
  // Grab authorization token from the request header, the best way to do this will be framework specific.
  const userToken = request.headers['Authorization'];
  // Specify your custom domain for tokens. Configurable at https://authress.io/app/#/manage?focus=applications
  const userIdentity = await TokenVerifier('https://login.application.com', userToken);
} catch (error) {
  console.log('User is unauthorized', error);
  return { statusCode: 401 };
}

Platform Extension Login

The goal of the platform extension is to make it easy for your platform extension developers to login with Authress. Embed the ExtensionClient in your javascript UI SDK, and pass in the extensionId.

const { ExtensionClient } = require('@authress/login');

// What is my custom Domain? => https://authress.io/app/#/setup?focus=domain
// What is my extensionId => https://authress.io/app/#/manage?focus=extensions
const extensionClient = new ExtensionClient('https://login.application.io', extensionId);

// redirectUrl is where the extension would like to return the user to after login
// * This method will redirect the user to the Authress Login UI screen with an auth code
const { accessToken } = await extensionClient.login(redirectUrl);

// .... After login the user is redirected to the redirectUrl
// * So try the login again:
const { accessToken } = await extensionClient.login(redirectUrl);

// * Or get the user claims from the token
await userData = await this.getUserIdentity();

Advanced

Curious exactly how these methods work and when they should be used? We have some advanced guidance available for each of the methods on the:

Method documentation

Contributing

If you are interested in contributing to the @authress/login SDK, feel fere to read the contribution.md guide.

2.4.313

21 days ago

2.5.314

21 days ago

2.5.316

21 days ago

2.5.318

21 days ago

2.4.312

22 days ago

2.4.311

1 month ago

2.4.309

1 month ago

2.4.308

1 month ago

2.4.306

1 month ago

2.3.305

2 months ago

2.3.304

2 months ago

2.3.303

2 months ago

2.3.302

2 months ago

2.3.301

3 months ago

2.3.299

3 months ago

2.3.298

3 months ago

2.3.292

3 months ago

2.3.294

3 months ago

2.3.296

3 months ago

2.3.289

3 months ago

2.3.290

3 months ago

2.3.283

3 months ago

2.3.286

3 months ago

2.3.285

3 months ago

2.3.287

3 months ago

2.3.282

3 months ago

2.3.280

4 months ago

2.3.279

4 months ago

2.3.278

4 months ago

2.3.277

4 months ago

2.3.276

4 months ago

2.3.275

4 months ago

2.3.273

5 months ago

2.3.271

5 months ago

2.3.270

5 months ago

2.3.269

5 months ago

2.3.268

5 months ago

2.3.260

5 months ago

2.3.262

5 months ago

2.3.261

5 months ago

2.3.264

5 months ago

2.3.263

5 months ago

2.3.266

5 months ago

2.3.265

5 months ago

2.3.259

5 months ago

2.3.257

5 months ago

2.3.256

5 months ago

2.3.258

5 months ago

2.3.255

5 months ago

2.3.254

5 months ago

2.2.251

5 months ago

2.2.250

5 months ago

2.2.249

6 months ago

2.2.248

6 months ago

2.2.247

6 months ago

2.2.241

7 months ago

2.2.243

7 months ago

2.2.239

8 months ago

2.2.234

9 months ago

2.2.233

9 months ago

2.2.232

9 months ago

2.2.231

10 months ago

2.2.238

8 months ago

2.2.237

9 months ago

2.2.236

9 months ago

2.2.229

10 months ago

2.2.227

11 months ago

2.2.226

11 months ago

2.2.225

11 months ago

2.2.224

11 months ago

2.2.223

12 months ago

2.2.221

12 months ago

2.2.219

12 months ago

2.2.218

12 months ago

2.2.217

12 months ago

2.2.215

1 year ago

2.2.214

1 year ago

2.2.211

1 year ago

2.2.208

1 year ago

2.2.206

1 year ago

2.2.203

1 year ago

2.2.200

1 year ago

2.2.196

1 year ago