|
1 | 1 | --- |
2 | | -title: AWS Coginto Service Provider Set up |
| 2 | +title: AWS Cognito Login with Web3Auth |
3 | 3 | sidebar_label: AWS Cognito |
4 | | -description: "AWS Cognito Service Provider Set up | Documentation - Web3Auth" |
| 4 | +description: "AWS Cognito Login with Web3Auth | Documentation - Web3Auth" |
5 | 5 | --- |
6 | 6 |
|
7 | | -AWS Cognito allows you to easily add authentication to your application. Web3Auth supports AWS |
8 | | -Cognito as a service provider. You can create a connection for AWS Cognito and integrate it into |
9 | | -your application. This page will help you set up AWS Cognito as a service provider with Web3Auth. |
| 7 | +import CustomConnectionOptions from "@site/static/images/dashboard/authentication-custom-connections.png"; |
| 8 | +import AWSConnection from "@site/static/images/dashboard/aws-cognito-connection.png"; |
| 9 | + |
| 10 | +AWS Cognito is a scalable authentication service provided by Amazon Web Services that enables |
| 11 | +developers to securely manage user sign-up, sign-in, and access control. Web3Auth supports AWS |
| 12 | +Cognito as a service provider, allowing seamless integration between AWS Cognito’s authentication |
| 13 | +capabilities and Web3Auth’s decentralized key management infrastructure. |
10 | 14 |
|
11 | 15 | [Take a look at the supported social logins on AWS Cognito](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-social-idp.html) |
12 | 16 |
|
13 | 17 | ## Create an AWS Cognito Application |
14 | 18 |
|
15 | | -If you haven't already, create an AWS Cognito application for your project. It is the mandatory step |
16 | | -before we proceed further. After the basic setup, we'll learn how to create an AWS Cognito |
17 | | -connection for the Web3Auth project. |
| 19 | +To begin the integration process, developers must first create a new AWS Cognito application using |
| 20 | +the [AWS Management Console](https://console.aws.amazon.com/). This initial setup is required before |
| 21 | +configuring the connection within Web3Auth. |
| 22 | + |
| 23 | +Once the AWS Cognito application is created, developers can proceed to add it as a custom connection |
| 24 | +in the Web3Auth Dashboard. |
18 | 25 |
|
19 | 26 | [Learn how to set up AWS Cognito](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-console). |
20 | 27 |
|
21 | 28 | ## Create an AWS Cognito Connection |
22 | 29 |
|
23 | | -{/* TODO: Add AWS Cognito Connection Steps */} |
| 30 | +:::success Create AWS Cognito Connection on Dashboard |
| 31 | + |
| 32 | +To use this feature, developers must go to the `Custom Connections` tab in the |
| 33 | +[Web3Auth Dashboard](https://dashboard.web3auth.io). |
| 34 | + |
| 35 | +<div style={{ flexBasis: "300px", flexGrow: "1", textAlign: "center" }}> |
| 36 | + <img |
| 37 | + src={CustomConnectionOptions} |
| 38 | + style={{ alignSelf: "center", maxWidth: "100%" }} |
| 39 | + alt="Custom Connection Options" |
| 40 | + /> |
| 41 | +</div> |
| 42 | + |
| 43 | +::: |
| 44 | + |
| 45 | +Follow these steps to create a AWS Cognito connection: |
| 46 | + |
| 47 | +1. Visit the [Web3Auth Dashboard](https://dashboard.web3auth.io). |
| 48 | +1. Go to the `Custom Connections` section. |
| 49 | +1. Click on the `Settings` icon near the `AWS Cognito` connection. |
| 50 | +1. Enter the `Cognito Connection ID`. |
| 51 | +1. Paste `https://cognito-idp.{REGION}.amazonaws.com/{USER_POOL_ID}/.well-known/jwks.json` as |
| 52 | + `JWKS Endpoint`. |
| 53 | +1. Paste a sample `JWT Token` to auto populate the best **JWT validations** possible. |
| 54 | +1. Select the `JWT user identifier`: `email`, `sub` or `custom`. |
| 55 | +1. Toggle the Case Sensitivity of `User Identifier`. (Optional) |
| 56 | +1. Click on `Add Custom Validations` to add validations manually. |
| 57 | + 1. Type iss as a field and `https://cognito-idp.{REGION}.amazonaws.com/{USER_POOL_ID}` as a |
| 58 | + value. |
| 59 | + 1. Next, type aud as a field and `APP_CLIENT_ID` as a value. |
| 60 | +1. Finally, click on the `Add Connection` button. |
| 61 | + |
| 62 | +> Note: Replace the `REGION`, `USER_POOL_ID` and `APP_CLIENT_ID` with your Cognito specific details. |
| 63 | +
|
| 64 | +<div style={{ flexBasis: "300px", flexGrow: "1", textAlign: "left" }}> |
| 65 | + <img |
| 66 | + src={AWSConnection} |
| 67 | + style={{ alignSelf: "center", maxWidth: "100%" }} |
| 68 | + alt="AWS Cognito Connection" |
| 69 | + /> |
| 70 | +</div> |
| 71 | + |
| 72 | +## Usage |
| 73 | + |
| 74 | +### Initialize Web3Auth |
| 75 | + |
| 76 | +Wrap your app with the `Web3AuthProvider`. This provider should be as close to the root of your |
| 77 | +React tree as possible. |
| 78 | + |
| 79 | +```js title="Example: React + Vite (main.tsx)" |
| 80 | +import { StrictMode } from "react"; |
| 81 | +import { createRoot } from "react-dom/client"; |
| 82 | +import "./index.css"; |
| 83 | + |
| 84 | +import { Web3AuthProvider, WEB3AUTH_NETWORK } from "@web3auth/modal/react"; |
| 85 | + |
| 86 | +import App from "./App.tsx"; |
| 87 | + |
| 88 | +createRoot(document.getElementById("root")!).render( |
| 89 | + <StrictMode> |
| 90 | + <Web3AuthProvider |
| 91 | + config={{ |
| 92 | + web3AuthOptions: { |
| 93 | + clientId: "WEB3AUTH_CLIENT_ID", // Replace with your Client ID |
| 94 | + web3AuthNetwork: WEB3AUTH_NETWORK.SAPPHIRE_DEVNET, |
| 95 | + }, |
| 96 | + }} |
| 97 | + > |
| 98 | + <App /> |
| 99 | + </Web3AuthProvider> |
| 100 | + </StrictMode> |
| 101 | +); |
| 102 | +``` |
| 103 | + |
| 104 | +Since, the `AWS Cognito Connection` details are available from Dashboard, developers don't need to |
| 105 | +pass any additional parameters to the `Web3AuthProvider`. |
| 106 | + |
| 107 | +> Follow our [Quickstart Guide](/quick-start) to setup the basic flow. |
| 108 | +
|
| 109 | +### Login with AWS Cognito |
| 110 | + |
| 111 | +```jsx |
| 112 | +import { useWeb3AuthConnect, WALLET_CONNECTORS, AUTH_CONNECTION } from "@web3auth/modal/react"; |
| 113 | +import { useAccount } from "wagmi"; |
| 114 | + |
| 115 | +function LoginPage() { |
| 116 | + const { connectTo } = useWeb3AuthConnect(); |
| 117 | + const { address } = useAccount(); |
| 118 | + |
| 119 | + const loginWithCognito = async () => { |
| 120 | + try { |
| 121 | + // highlight-start |
| 122 | + await connectTo(WALLET_CONNECTORS.AUTH, { |
| 123 | + authConnection: AUTH_CONNECTION.CUSTOM, |
| 124 | + authConnectionId: "cognito-auth-connection-id", |
| 125 | + extraLoginOptions: { |
| 126 | + clientId: "COGNITO_CLIENT_ID", |
| 127 | + domain: "COGNITO_DOMAIN", |
| 128 | + verifierIdField: "email", |
| 129 | + response_type: "token", |
| 130 | + scope: "email profile openid", |
| 131 | + }, |
| 132 | + }); |
| 133 | + // highlight-end |
| 134 | + } catch (error) { |
| 135 | + console.error("AWS Cognito authentication error:", error); |
| 136 | + } |
| 137 | + }; |
| 138 | + |
| 139 | + return ( |
| 140 | + <div> |
| 141 | + <button onClick={() => loginWithCognito()}>Login with AWS Cognito</button> |
| 142 | + <p>Address: {address}</p> |
| 143 | + {/* 0x1e46c61f7F5B1D9B8505CEdED94AB1468dC28aa0 */} |
| 144 | + </div> |
| 145 | + ); |
| 146 | +} |
| 147 | +``` |
0 commit comments