Skip to content

Commit

Permalink
Added useSignInEmail hook and store universal email (#181)
Browse files Browse the repository at this point in the history
* Added useSignInEmail hook and store universal email

* Deprecate useWaasSignInEmail in favor of useSignInEmail

* Removing console.log
  • Loading branch information
corbanbrook authored Oct 30, 2024
1 parent 6f819ac commit ef6f523
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export function sequenceWallet(params: BaseSequenceConnectorOptions) {
[LocalStorageKey.EthAuthProof]: ETHAuthProof
[LocalStorageKey.Theme]: string
[LocalStorageKey.EthAuthSettings]: EthAuthSettings
[LocalStorageKey.WaasSignInEmail]: string | null
}

return createConnector<Provider, Properties, StorageItem>(config => ({
Expand Down Expand Up @@ -86,6 +87,7 @@ export function sequenceWallet(params: BaseSequenceConnectorOptions) {

const connectOptionsWithTheme = {
authorize: true,
askForEmail: true,
...ethAuthSettings,
...connect,
settings: {
Expand All @@ -112,6 +114,9 @@ export function sequenceWallet(params: BaseSequenceConnectorOptions) {

await config.storage?.setItem(LocalStorageKey.EthAuthProof, jsonEthAuthProof)
}

// Save the email used to sign in
await config.storage?.setItem(LocalStorageKey.WaasSignInEmail, e.email || null)
}

const accounts = await this.getAccounts()
Expand All @@ -126,6 +131,8 @@ export function sequenceWallet(params: BaseSequenceConnectorOptions) {
const provider = await this.getProvider()

provider.disconnect()

await config.storage?.removeItem(LocalStorageKey.WaasSignInEmail)
},

async getAccounts() {
Expand Down
28 changes: 28 additions & 0 deletions packages/kit/src/hooks/useSignInEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use client'

import { useEffect, useState } from 'react'
import { useConfig, useAccount } from 'wagmi'

import { LocalStorageKey } from '../constants/localStorage'

export const useSignInEmail = () => {
const { storage } = useConfig()
const { isConnected } = useAccount()
const [email, setEmail] = useState<null | string>(null)

const storeEmail = async () => {
const storedEmail = await storage?.getItem(LocalStorageKey.WaasSignInEmail)

setEmail(storedEmail as string)
}

useEffect(() => {
if (isConnected) {
storeEmail()
} else {
setEmail(null)
}
}, [isConnected])

return email
}
3 changes: 3 additions & 0 deletions packages/kit/src/hooks/useWaasSignInEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { useConfig, useAccount } from 'wagmi'

import { LocalStorageKey } from '../constants/localStorage'

/**
* @deprecated use useSignInEmail instead
*/
export const useWaasSignInEmail = () => {
const { storage } = useConfig()
const { isConnected } = useAccount()
Expand Down
8 changes: 7 additions & 1 deletion packages/kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ export { getDefaultChains } from './config/defaultChains'
export { getDefaultTransports } from './config/defaultTransports'

// Constants
export { LocalStorageKey, DEFAULT_SESSION_EXPIRATION, TRANSACTION_CONFIRMATIONS_DEFAULT, NATIVE_TOKEN_ADDRESS_0X } from './constants'
export {
LocalStorageKey,
DEFAULT_SESSION_EXPIRATION,
TRANSACTION_CONFIRMATIONS_DEFAULT,
NATIVE_TOKEN_ADDRESS_0X
} from './constants'

// Utils
export { getKitConnectWallets } from './utils/getKitConnectWallets'
Expand Down Expand Up @@ -80,6 +85,7 @@ export { useTheme } from './hooks/useTheme'
export { useWalletSettings } from './hooks/useWalletSettings'
export { useWaasFeeOptions } from './hooks/useWaasFeeOptions'
export { useWaasSignInEmail } from './hooks/useWaasSignInEmail'
export { useSignInEmail } from './hooks/useSignInEmail'
export { useProjectAccessKey } from './hooks/useProjectAccessKey'
export { useAPIClient } from './hooks/useAPIClient'
export { useMetadataClient } from './hooks/useMetadataClient'
Expand Down

0 comments on commit ef6f523

Please sign in to comment.