Authress 登录 UIS 的 SDK
用于javascript应用网站和用户身份认证的Authress通用登录SDK. 曾经作为服务供应商Authress 在 https://authress.io (英语)。 。 。 .
使用量
npm install @authress/login
然后要求软件包:
import { LoginClient } from '@authress/login';
解决问题的使用
楼层或另一个捆绑工的麻烦,检查一下 构建工具排除出错页面。 。 。 .
开始
第 0 部分: 设置 Authress 登录
你会想创造:
- 至少一个提供者连接 - https://authress.io/app/#/manage?焦点=连接
- 一个代表您网络应用的应用程序 - https://authress.io/app/#/manage?焦点=应用程序
第一部分:Web App UI
在每次路由更改检查中都查看用户是否存在,如果用户没有将其重定向到登录提示.
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');
}
在您的应用程序的登录屏幕中, 当用户选择如何登录时, 引导它们到那里 。 另指定您希望 Authress 在登录后将用户重定向到何处 。 默认这是用户当前位置 .
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;
然后让用户登录的用户访问令牌用于下一部分的授权:
const userToken = await loginClient.ensureToken();
第2部分:服务API中的用户认证
要认证您服务 API 中的用户, 由库在 Part 1 需要传递给您的服务。 标准是从登录卡中拉出,然后放入一个 Authorization 页眉 :
const userToken = await loginClient.ensureToken();
const result = await fetch('https://api.application.com/v1/route', {
method: 'GET',
headers: {
'Authorization': `Bearer ${userToken}`
}
})
在服务 API 侧,拉入 Authress 服务客户端库,然后验证访问符.
- 首先安装相伴库 :
npm install authress-sdk - 然后验证从授权信头收到的符号 :
import { TokenVerifier } from '@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 };
}
平台扩展登录
平台扩展的目标是方便您的平台扩展开发者与Authress登录. 装入 ExtensionClient 在你的Javascript UI SDK, 通过在 extensionId。 。 。 .
import { ExtensionClient } from '@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();
高级
究竟这些方法是如何运作的以及何时应使用这些方法? 我们已就下列每一方法提供了一些先进的指导:
捐款
如果你有兴趣为@authress/login SDK做出贡献 捐款 向导.