diff --git a/package.json b/package.json index 2d98263..78f4416 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,7 @@ "@0xsequence/design-system": "^1.0.13", "@0xsequence/indexer": "^1.1.3", "@0xsequence/network": "^1.1.3", - "@0xsequence/waas": "0.0.0-20231108135717", - "@aws-sdk/client-cognito-identity-provider": "^3.441.0", + "@0xsequence/waas": "0.0.0-20231108174744", "@react-oauth/google": "^0.11.1", "@vanilla-extract/css": "^1.12.0", "axios": "^1.6.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d4359e8..af43f44 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -15,11 +15,8 @@ dependencies: specifier: ^1.1.3 version: 1.1.3(ethers@5.7.2) '@0xsequence/waas': - specifier: 0.0.0-20231108135717 - version: 0.0.0-20231108135717 - '@aws-sdk/client-cognito-identity-provider': - specifier: ^3.441.0 - version: 3.441.0 + specifier: 0.0.0-20231108174744 + version: 0.0.0-20231108174744 '@react-oauth/google': specifier: ^0.11.1 version: 0.11.1(react-dom@18.2.0)(react@18.2.0) @@ -159,9 +156,10 @@ packages: js-base64: 3.7.5 dev: false - /@0xsequence/waas@0.0.0-20231108135717: - resolution: {integrity: sha512-wxnqEF8+3FGfBN+J8XsiNdSu6n/whthUyUA2fr+RQ2QwucFsJYQObvXSSpKkG9CGxM0H8rEAJjtgfQnKLX1J3w==} + /@0xsequence/waas@0.0.0-20231108174744: + resolution: {integrity: sha512-MgiWqKK4cQIZF4V5P+zjjkbcpSNykXy/OLLv9qmk45lMajN9MPY9/ZPP2hiJxft+C0nVoemrhccZZSA9onc28Q==} dependencies: + '@aws-sdk/client-cognito-identity-provider': 3.445.0 '@aws-sdk/client-kms': 3.445.0 '@aws-sdk/credential-providers': 3.445.0 ethers: 5.7.2 @@ -235,15 +233,15 @@ packages: tslib: 1.14.1 dev: false - /@aws-sdk/client-cognito-identity-provider@3.441.0: - resolution: {integrity: sha512-ycRa91qPP4ruRe6MI5Clniiw2J6x/IRMaGnJuoZxIC68mhwBFYb9BYNUpxj1bih9TEW21s3mdX4J/Exp9sQiuw==} + /@aws-sdk/client-cognito-identity-provider@3.445.0: + resolution: {integrity: sha512-Dqf8HTPA4Gd6DvSq8verk+oDGSNXZdMEDkciiG1h4dYIAB+08ggvuIOL9d3vmzEL5pwiV5bSitWYTgMC3sf3mg==} engines: {node: '>=14.0.0'} dependencies: '@aws-crypto/sha256-browser': 3.0.0 '@aws-crypto/sha256-js': 3.0.0 - '@aws-sdk/client-sts': 3.441.0 - '@aws-sdk/core': 3.441.0 - '@aws-sdk/credential-provider-node': 3.441.0 + '@aws-sdk/client-sts': 3.445.0 + '@aws-sdk/core': 3.445.0 + '@aws-sdk/credential-provider-node': 3.445.0 '@aws-sdk/middleware-host-header': 3.433.0 '@aws-sdk/middleware-logger': 3.433.0 '@aws-sdk/middleware-recursion-detection': 3.433.0 @@ -376,50 +374,6 @@ packages: - aws-crt dev: false - /@aws-sdk/client-sso@3.441.0: - resolution: {integrity: sha512-gndGymu4cEIN7WWhQ67RO0JMda09EGBlay2L8IKCHBK/65Y34FHUX1tCNbO2qezEzsi6BPW5o2n53Rd9QqpHUw==} - engines: {node: '>=14.0.0'} - dependencies: - '@aws-crypto/sha256-browser': 3.0.0 - '@aws-crypto/sha256-js': 3.0.0 - '@aws-sdk/core': 3.441.0 - '@aws-sdk/middleware-host-header': 3.433.0 - '@aws-sdk/middleware-logger': 3.433.0 - '@aws-sdk/middleware-recursion-detection': 3.433.0 - '@aws-sdk/middleware-user-agent': 3.438.0 - '@aws-sdk/region-config-resolver': 3.433.0 - '@aws-sdk/types': 3.433.0 - '@aws-sdk/util-endpoints': 3.438.0 - '@aws-sdk/util-user-agent-browser': 3.433.0 - '@aws-sdk/util-user-agent-node': 3.437.0 - '@smithy/config-resolver': 2.0.16 - '@smithy/fetch-http-handler': 2.2.4 - '@smithy/hash-node': 2.0.12 - '@smithy/invalid-dependency': 2.0.12 - '@smithy/middleware-content-length': 2.0.14 - '@smithy/middleware-endpoint': 2.1.3 - '@smithy/middleware-retry': 2.0.18 - '@smithy/middleware-serde': 2.0.12 - '@smithy/middleware-stack': 2.0.6 - '@smithy/node-config-provider': 2.1.3 - '@smithy/node-http-handler': 2.1.8 - '@smithy/protocol-http': 3.0.8 - '@smithy/smithy-client': 2.1.12 - '@smithy/types': 2.4.0 - '@smithy/url-parser': 2.0.12 - '@smithy/util-base64': 2.0.0 - '@smithy/util-body-length-browser': 2.0.0 - '@smithy/util-body-length-node': 2.1.0 - '@smithy/util-defaults-mode-browser': 2.0.16 - '@smithy/util-defaults-mode-node': 2.0.21 - '@smithy/util-endpoints': 1.0.2 - '@smithy/util-retry': 2.0.5 - '@smithy/util-utf8': 2.0.0 - tslib: 2.6.2 - transitivePeerDependencies: - - aws-crt - dev: false - /@aws-sdk/client-sso@3.445.0: resolution: {integrity: sha512-me4LvqNnu6kxi+sW7t0AgMv1Yi64ikas0x2+5jv23o6Csg32w0S0xOjCTKQYahOA5CMFunWvlkFIfxbqs+Uo7w==} engines: {node: '>=14.0.0'} @@ -464,54 +418,6 @@ packages: - aws-crt dev: false - /@aws-sdk/client-sts@3.441.0: - resolution: {integrity: sha512-GL0Cw2v7XL1cn0T+Sk5VHLlgBJoUdMsysXsHa1mFdk0l6XHMAAnwXVXiNnjmoDSPrG0psz7dL2AKzPVRXbIUjA==} - engines: {node: '>=14.0.0'} - dependencies: - '@aws-crypto/sha256-browser': 3.0.0 - '@aws-crypto/sha256-js': 3.0.0 - '@aws-sdk/core': 3.441.0 - '@aws-sdk/credential-provider-node': 3.441.0 - '@aws-sdk/middleware-host-header': 3.433.0 - '@aws-sdk/middleware-logger': 3.433.0 - '@aws-sdk/middleware-recursion-detection': 3.433.0 - '@aws-sdk/middleware-sdk-sts': 3.433.0 - '@aws-sdk/middleware-signing': 3.433.0 - '@aws-sdk/middleware-user-agent': 3.438.0 - '@aws-sdk/region-config-resolver': 3.433.0 - '@aws-sdk/types': 3.433.0 - '@aws-sdk/util-endpoints': 3.438.0 - '@aws-sdk/util-user-agent-browser': 3.433.0 - '@aws-sdk/util-user-agent-node': 3.437.0 - '@smithy/config-resolver': 2.0.16 - '@smithy/fetch-http-handler': 2.2.4 - '@smithy/hash-node': 2.0.12 - '@smithy/invalid-dependency': 2.0.12 - '@smithy/middleware-content-length': 2.0.14 - '@smithy/middleware-endpoint': 2.1.3 - '@smithy/middleware-retry': 2.0.18 - '@smithy/middleware-serde': 2.0.12 - '@smithy/middleware-stack': 2.0.6 - '@smithy/node-config-provider': 2.1.3 - '@smithy/node-http-handler': 2.1.8 - '@smithy/protocol-http': 3.0.8 - '@smithy/smithy-client': 2.1.12 - '@smithy/types': 2.4.0 - '@smithy/url-parser': 2.0.12 - '@smithy/util-base64': 2.0.0 - '@smithy/util-body-length-browser': 2.0.0 - '@smithy/util-body-length-node': 2.1.0 - '@smithy/util-defaults-mode-browser': 2.0.16 - '@smithy/util-defaults-mode-node': 2.0.21 - '@smithy/util-endpoints': 1.0.2 - '@smithy/util-retry': 2.0.5 - '@smithy/util-utf8': 2.0.0 - fast-xml-parser: 4.2.5 - tslib: 2.6.2 - transitivePeerDependencies: - - aws-crt - dev: false - /@aws-sdk/client-sts@3.445.0: resolution: {integrity: sha512-ogbdqrS8x9O5BTot826iLnTQ6i4/F5BSi/74gycneCxYmAnYnyUBNOWVnynv6XZiEWyDJQCU2UtMd52aNGW1GA==} engines: {node: '>=14.0.0'} @@ -560,13 +466,6 @@ packages: - aws-crt dev: false - /@aws-sdk/core@3.441.0: - resolution: {integrity: sha512-gV0eQwR0VnSPUYAbgDkbBtfXbSpZgl/K6UB13DP1IFFjQYbF/BxYwvcQe4jHoPOBifSgjEbl8MfOOeIyI7k9vg==} - engines: {node: '>=14.0.0'} - dependencies: - '@smithy/smithy-client': 2.1.12 - dev: false - /@aws-sdk/core@3.445.0: resolution: {integrity: sha512-6GYLElUG1QTOdmXG8zXa+Ull9IUeSeItKDYHKzHYfIkbsagMfYlf7wm9XIYlatjtgodNfZ3gPHAJfRyPmwKrsg==} engines: {node: '>=14.0.0'} @@ -613,24 +512,6 @@ packages: tslib: 2.6.2 dev: false - /@aws-sdk/credential-provider-ini@3.441.0: - resolution: {integrity: sha512-SQipQYxYqDUuSOfIhDmaTdwPTcndGQotGZXWJl56mMWqAhU8MkwjK+oMf3VgRt/umJC0QwUCF5HUHIj7gSB1JA==} - engines: {node: '>=14.0.0'} - dependencies: - '@aws-sdk/credential-provider-env': 3.433.0 - '@aws-sdk/credential-provider-process': 3.433.0 - '@aws-sdk/credential-provider-sso': 3.441.0 - '@aws-sdk/credential-provider-web-identity': 3.433.0 - '@aws-sdk/types': 3.433.0 - '@smithy/credential-provider-imds': 2.0.18 - '@smithy/property-provider': 2.0.13 - '@smithy/shared-ini-file-loader': 2.2.2 - '@smithy/types': 2.4.0 - tslib: 2.6.2 - transitivePeerDependencies: - - aws-crt - dev: false - /@aws-sdk/credential-provider-ini@3.445.0: resolution: {integrity: sha512-R7IYSGjNZ5KKJwQJ2HNPemjpAMWvdce91i8w+/aHfqeGfTXrmYJu99PeGRyyBTKEumBaojyjTRvmO8HzS+/l7g==} engines: {node: '>=14.0.0'} @@ -649,25 +530,6 @@ packages: - aws-crt dev: false - /@aws-sdk/credential-provider-node@3.441.0: - resolution: {integrity: sha512-WB9p37yHq6fGJt6Vll29ijHbkh9VDbPM/n5ns73bTAgFD7R0ht5kPmdmHGQA6m3RKjcHLPbymQ3lXykkMwWf/Q==} - engines: {node: '>=14.0.0'} - dependencies: - '@aws-sdk/credential-provider-env': 3.433.0 - '@aws-sdk/credential-provider-ini': 3.441.0 - '@aws-sdk/credential-provider-process': 3.433.0 - '@aws-sdk/credential-provider-sso': 3.441.0 - '@aws-sdk/credential-provider-web-identity': 3.433.0 - '@aws-sdk/types': 3.433.0 - '@smithy/credential-provider-imds': 2.0.18 - '@smithy/property-provider': 2.0.13 - '@smithy/shared-ini-file-loader': 2.2.2 - '@smithy/types': 2.4.0 - tslib: 2.6.2 - transitivePeerDependencies: - - aws-crt - dev: false - /@aws-sdk/credential-provider-node@3.445.0: resolution: {integrity: sha512-zI4k4foSjQRKNEsouculRcz7IbLfuqdFxypDLYwn+qPNMqJwWJ7VxOOeBSPUpHFcd7CLSfbHN2JAhQ7M02gPTA==} engines: {node: '>=14.0.0'} @@ -698,21 +560,6 @@ packages: tslib: 2.6.2 dev: false - /@aws-sdk/credential-provider-sso@3.441.0: - resolution: {integrity: sha512-pTg16G+62mWCE8yGKuQnEBqPdpG5g71remf2jUqXaI1c7GCzbnkQDV9eD4DaAGOvzIs0wo9zAQnS2kVDPFlCYA==} - engines: {node: '>=14.0.0'} - dependencies: - '@aws-sdk/client-sso': 3.441.0 - '@aws-sdk/token-providers': 3.438.0 - '@aws-sdk/types': 3.433.0 - '@smithy/property-provider': 2.0.13 - '@smithy/shared-ini-file-loader': 2.2.2 - '@smithy/types': 2.4.0 - tslib: 2.6.2 - transitivePeerDependencies: - - aws-crt - dev: false - /@aws-sdk/credential-provider-sso@3.445.0: resolution: {integrity: sha512-gJz7kAiDecdhtApgXnxfZsXKsww8BnifDF9MAx9Dr4X6no47qYsCCS3XPuEyRiF9VebXvHOH0H260Zp3bVyniQ==} engines: {node: '>=14.0.0'} diff --git a/src/Login.tsx b/src/Login.tsx index a2635ea..9f805a9 100644 --- a/src/Login.tsx +++ b/src/Login.tsx @@ -23,8 +23,6 @@ function Login() { initiateAuth: initiateEmailAuth, sendChallengeAnswer, } = useEmailAuth({ - region: 'us-east-2', - clientId: '5fl7dg7mvu534o9vfjbc6hj31p', onSuccess: async (idToken) => { setSigningIn(true) const walletAddress = await sequence.signIn({ idToken }, randomName()) diff --git a/src/main.tsx b/src/main.tsx index 50c999f..1919d99 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -17,7 +17,7 @@ export const node = new ethers.providers.JsonRpcProvider('https://nodes.sequence export const sequence = new Sequence({ network: 'polygon', - key: 'eyJzZWNyZXQiOiJ0YmQiLCJ0ZW5hbnQiOjksImlkZW50aXR5UG9vbElkIjoidXMtZWFzdC0yOjQyYzlmMzlkLWM5MzUtNGQ1Yy1hODQ1LTVjODgxNWM3OWVlMyJ9', + key: 'eyJzZWNyZXQiOiJ0YmQiLCJ0ZW5hbnQiOjksImlkZW50aXR5UG9vbElkIjoidXMtZWFzdC0yOjQyYzlmMzlkLWM5MzUtNGQ1Yy1hODQ1LTVjODgxNWM3OWVlMyIsImVtYWlsQ2xpZW50SWQiOiI1Zmw3ZGc3bXZ1NTM0bzl2ZmpiYzZoajMxcCJ9', }, defaults.TEMPLATE_NEXT) export const router = createHashRouter([ diff --git a/src/utils/useEmailAuth.ts b/src/utils/useEmailAuth.ts index c3b1ddc..aacb962 100644 --- a/src/utils/useEmailAuth.ts +++ b/src/utils/useEmailAuth.ts @@ -1,113 +1,45 @@ -import { useMemo, useState } from 'react' -import { - CognitoIdentityProviderClient, - InitiateAuthCommand, RespondToAuthChallengeCommand, SignUpCommand, - UserLambdaValidationException -} from '@aws-sdk/client-cognito-identity-provider' - -interface EmailAuthConfig { - region: string - clientId: string - onSuccess: (idToken: string) => void -} +import { useState } from 'react' +import { sequence } from '../main' -export function useEmailAuth({ region, clientId, onSuccess }: EmailAuthConfig) { +export function useEmailAuth({ onSuccess }: { onSuccess: (idToken: string) => void }) { const [email, setEmail] = useState("") const [error, setError] = useState() const [loading, setLoading] = useState(false) - const [challengeSession, setChallengeSession] = useState('') - - const client = useMemo( - () => new CognitoIdentityProviderClient({ region }), - [region], - ) - - const signUp = async (email: string) => { - const cmd = new SignUpCommand({ - ClientId: clientId, - Username: email, - Password: 'aB1%' + getRandomString(14), - UserAttributes: [{Name: 'email', Value: email}], - }) - await client.send(cmd) - } + const [instance, setInstance] = useState('') - const initiateAuth = (email: string) => { - const cmd = new InitiateAuthCommand({ - AuthFlow: 'CUSTOM_AUTH', - ClientId: clientId, - AuthParameters: { - USERNAME: email, - } - }) - setEmail(email) + const initiateAuth = async (email: string) => { setLoading(true) - ;(async () => { - while (true) { - try { - const res = await client.send(cmd) - if (!res.Session) { - throw new Error("response session is empty") - } - setChallengeSession(res.Session) - setLoading(false) - break - } catch (e) { - if (e instanceof UserLambdaValidationException) { - if (e.message.includes("user not found")) { - await signUp(email) - continue - } - } - setError(e) - setLoading(false) - break - } - } - })() + try { + const { instance } = await sequence.email.initiateAuth({ email }) + setInstance(instance) + setEmail(email) + } catch (e: any) { + setError(e.message || "Unknown error") + } finally { + setLoading(false) + } } - const sendChallengeAnswer = (answer: string) => { - const cmd = new RespondToAuthChallengeCommand({ - ClientId: clientId, - Session: challengeSession, - ChallengeName: 'CUSTOM_CHALLENGE', - ChallengeResponses: { USERNAME: email, ANSWER: answer }, - }) + const sendChallengeAnswer = async (answer: string) => { setLoading(true) - ;(async () => { - try { - const res = await client.send(cmd) - if (!res.AuthenticationResult) { - throw new Error('AuthenticationResult is empty') - } - onSuccess(res.AuthenticationResult.IdToken!) - } catch (e) { - setError(e) - } finally { - setLoading(false) - } - })() + try { + const { idToken } = await sequence.email.finalizeAuth({ instance, answer, email }) + onSuccess(idToken) + } catch (e: any) { + setError(e.message || "Unknown error") + } finally { + setLoading(false) + } } return { - inProgress: loading || !!challengeSession, + inProgress: loading || !!instance, loading, error, initiateAuth, - sendChallengeAnswer: challengeSession ? sendChallengeAnswer : undefined, + sendChallengeAnswer: instance ? sendChallengeAnswer : undefined, } } - -function getRandomString(len: number) { - const randomValues = new Uint8Array(len); - window.crypto.getRandomValues(randomValues); - return Array.from(randomValues).map(intToHex).join(''); -} - -function intToHex(nr: number) { - return nr.toString(16).padStart(2, '0'); -} diff --git a/vite.config.ts b/vite.config.ts index d918eea..b918cfa 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -3,8 +3,8 @@ import react from '@vitejs/plugin-react' import { vanillaExtractPlugin } from '@vanilla-extract/vite-plugin' // https://vitejs.dev/config/ -export default defineConfig({ +export default defineConfig(({ mode }) => ({ plugins: [react(), vanillaExtractPlugin()], // For github pages https://0xsequence.github.io/demo-waas-auth/ - base: '/demo-waas-auth/' -}) + base: mode === 'production' ? '/demo-waas-auth/' : '/', +}))