且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在不使用 Amplify 的情况下使用 Amazon Cognito

更新时间:2022-10-14 22:43:53

首先,我想澄清一下放大"是多个事物的总称.我们有:

  1. 放大库 (UI/JS)
  2. 放大 CLI(以创建云原生应用)
  3. Amplify Console(ci/cd 和全栈网络应用托管)
  4. 放大管理用户界面(用于创建和配置全栈网络应用的用户界面)

您可以查看主页以获取更多说明 - https://docs.amplify.aws/>

是否有用于与 Cognito 服务、身份验证和授权流程交互的轻量级仅 Cognito 客户端库?

在幕后,Amplify 使用 amazon-cognito-identity-js 与 Amazon Cognito 交互的库.您可以通过 npm install amazon-cognito-identity-js 直接安装.

源代码已移至放大库(例如 amplify-js) 存储库.再一次,是放大"的一部分.第一类放大库"下的保护伞.

有没有办法在没有 Amplify 库的情况下使用 Cognito 服务?

您可以采用的另一种方法是使用 Amazon Cognito 作为 OAuth 服务器.当您创建 Amazon Cognito 托管 UI 域时,它为您提供符合 OAuth 2.0 的授权服务器.

您可以为注册/登录端点创建自己的 API/后端,并与 Amazon Cognito OAuth 服务器交换令牌/凭证,而无需使用 aws-sdk 或任何第三方依赖库.

我写了一个演练示例,如何配置你的用户池,你需要使用 Node.js 与之交谈的端点,你可以在这里找到它:https://github.com/oieduardorabelo/node-amazon-cognito-oauth

对于任何其他语言,您都可以遵循相同的想法.

I'm just now diving into Cognito. The AWS setup has been fairly straight-forward, easy.

We have a variety of apps, webapps, and services and we'd like those to make use of the Cognito service. I've experience setting up similar with Auth0, but because we've been leveraging a number of Amazon Web Services, it really makes sense to use Cognito as well.

Everywhere I look, every guide eventually references Amplify client-side library and cli. We have existing apps and services, and really don't want to change tooling or import anything unnecessary to add bloat and complexity. Is there a way to use Cognito service without Amplify libraries? Is there a lightweight Cognito-only client library for interfacing with the Cognito service, authentication-and-authorization flow?

To start, I want to clarify that "Amplify" is an umbrella term for multiple things. We have:

  1. Amplify Libraries (UI/JS)
  2. Amplify CLI (to create cloud-native applications)
  3. Amplify Console (ci/cd and hosting for full-stack web apps)
  4. Amplify Admin UI (UI to create and configure full-stack web apps)

You can check the homepage for more clarification - https://docs.amplify.aws/

Is there a lightweight Cognito-only client library for interfacing with the Cognito service, authentication-and-authorization flow?

Behind the scenes, Amplify uses amazon-cognito-identity-js library to interface with Amazon Cognito. You can install that directly via npm install amazon-cognito-identity-js.

The source code has been moved to the Amplify Libraries (e.g. amplify-js) repository. Once again, is part of the "Amplify" umbrella under the first category "Amplify Libraries".

Is there a way to use Cognito service without Amplify libraries?

Another approach that you can do, is to use Amazon Cognito as an OAuth server. When you create an Amazon Cognito Hosted UI Domain, it provides you an OAuth 2.0 compliant authorization server.

You can create your own API/Backend for Signup/Login endpoints and exchange tokens/credentials with the Amazon Cognito OAuth server without using aws-sdk or any 3rd party dependency library.

I wrote a walkthrough example, how to configure your User Pool, endpoints that you need to talk to using Node.js, you can find it here: https://github.com/oieduardorabelo/node-amazon-cognito-oauth

You can follow the same idea for any other language.