Skip to content

Commit

Permalink
Pass sessionAddress/nonce when authenticating (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrislav authored Feb 15, 2024
1 parent 5fe4b16 commit 5947a91
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 162 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@0xsequence/design-system": "^1.0.13",
"@0xsequence/indexer": "^1.4.0",
"@0xsequence/network": "^1.1.3",
"@0xsequence/waas": "0.0.0-20240119113726",
"@0xsequence/waas": "0.0.0-20240215172023",
"@react-oauth/google": "^0.11.1",
"@vanilla-extract/css": "^1.12.0",
"axios": "^1.6.0",
Expand Down
211 changes: 59 additions & 152 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions src/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import { CredentialResponse, GoogleLogin } from '@react-oauth/google'
import AppleSignin from 'react-apple-signin-auth'
import { randomName } from './utils/indexer'
import { useEmailAuth } from "./utils/useEmailAuth.ts";
import {useSessionHash} from "./utils/useSessionHash.ts";

function Login() {
const { sessionHash } = useSessionHash()
const [email, setEmail] = useState('')
const inputRef = useRef<HTMLInputElement | null>(null)
const isEmailValid = inputRef.current?.validity.valid
Expand Down Expand Up @@ -145,14 +147,14 @@ function Login() {

<hr/>

{!emailAuthInProgress && (<>
{!emailAuthInProgress && !!sessionHash && (<>
<Box>
<Text variant="large" color="text100" fontWeight="bold">
Social Login
</Text>
</Box>
{import.meta.env.VITE_GOOGLE_CLIENT_ID && (
<GoogleLogin onSuccess={handleGoogleLogin} shape="circle" width={230} />
<GoogleLogin onSuccess={handleGoogleLogin} shape="circle" width={230} nonce={sessionHash} />
)}
{import.meta.env.VITE_APPLE_CLIENT_ID && (
<AppleSignin
Expand All @@ -161,6 +163,7 @@ function Login() {
scope: 'openid email',
redirectURI: 'https://' + window.location.host,
usePopup: true,
nonce: sessionHash,
}}
onError={(error: any) => console.error(error)}
onSuccess={handleAppleLogin}
Expand Down
2 changes: 1 addition & 1 deletion src/components/views/ListSessionsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function ListSessionsView() {
useEffect(() => {
Promise.all([
sequence.listSessions(),
sequence.getSessionID()
sequence.getSessionId()
]).then(([s, t]) => {
setSessions(s)
setThisSession(t)
Expand Down
10 changes: 5 additions & 5 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {createHashRouter, RouterProvider} from 'react-router-dom'

import Login from './Login.tsx'
import { GoogleOAuthProvider } from '@react-oauth/google'
import { Sequence } from '@0xsequence/waas'
import { SequenceWaaS } from '@0xsequence/waas'
import App from './App.tsx'
import { ethers } from 'ethers'
import { defaults, ExtendedSequenceConfig } from '@0xsequence/waas'
Expand All @@ -19,9 +19,9 @@ const SEQUENCE_WAAS_CONFIG_KEY = import.meta.env.VITE_SEQUENCE_WAAS_CONFIG_KEY
export const node = new ethers.providers.JsonRpcProvider('https://nodes.sequence.app/polygon')

const urlParams = new URLSearchParams(window.location.search)
let projectAccessKey = urlParams.get('projectAccessKey') ?? SEQUENCE_PROJECT_ACCESS_KEY
let waasConfigKey = urlParams.get('waasConfigKey') ?? SEQUENCE_WAAS_CONFIG_KEY
let preset = extendedSequenceConfigFromBase64(urlParams.get('preset') ?? "") ?? defaults.TEST
const projectAccessKey = urlParams.get('projectAccessKey') ?? SEQUENCE_PROJECT_ACCESS_KEY
const waasConfigKey = urlParams.get('waasConfigKey') ?? SEQUENCE_WAAS_CONFIG_KEY
const preset = extendedSequenceConfigFromBase64(urlParams.get('preset') ?? "") ?? defaults.TEST

function extendedSequenceConfigFromBase64(config: string): ExtendedSequenceConfig | undefined {
if (config === "") {
Expand All @@ -30,7 +30,7 @@ function extendedSequenceConfigFromBase64(config: string): ExtendedSequenceConfi
return JSON.parse(new TextDecoder().decode(base64.decode(config))) as unknown as ExtendedSequenceConfig
}

export const sequence = new Sequence({
export const sequence = new SequenceWaaS({
network: 'polygon',
projectAccessKey: projectAccessKey,
waasConfigKey: waasConfigKey,
Expand Down
3 changes: 2 additions & 1 deletion src/utils/useEmailAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export function useEmailAuth({ onSuccess }: { onSuccess: (idToken: string) => vo
setLoading(true)

try {
const { idToken } = await sequence.email.finalizeAuth({ instance, answer, email })
const sessionHash = await sequence.getSessionHash()
const { idToken } = await sequence.email.finalizeAuth({ instance, answer, email, sessionHash })
onSuccess(idToken)
} catch (e: any) {
setError(e.message || "Unknown error")
Expand Down
Loading

0 comments on commit 5947a91

Please sign in to comment.