-
Notifications
You must be signed in to change notification settings - Fork 38
FEAT: DVR-328 | passport event handling feature #2617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7c8abe5
event handling app WIP
darrenmelvison1 45105a5
moved event-handling into login-with-nextjs and use available events …
darrenmelvison1 09284a3
spacing changes
darrenmelvison1 163537d
reverted pnpm.lock
darrenmelvison1 484451a
fixed the event handling page layout
darrenmelvison1 73c7930
added E2E test for event handling
darrenmelvison1 5b2f2f9
renamed prompt files
darrenmelvison1 cb2e398
updated login-with-nextjs tutorial content
darrenmelvison1 95c3adb
renamed other prompt file names and updated readme to reflect new fil…
darrenmelvison1 b7ae8bd
moved all prompts to be under a directory and updated readme
darrenmelvison1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
274 changes: 274 additions & 0 deletions
274
examples/passport/login-with-nextjs/src/app/auth-event-handling/page.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,274 @@ | ||
| 'use client'; | ||
|
|
||
| import { useEffect, useState, useCallback } from 'react'; | ||
| import { Button, Heading, Stack, Body, Table, Link } from '@biom3/react'; | ||
| import NextLink from 'next/link'; | ||
| import { passportInstance } from '../utils/setupLogoutSilent'; | ||
| import { Provider, ProviderEvent } from '@imtbl/sdk/passport'; | ||
|
|
||
|
|
||
| export default function EventHandlingPage() { | ||
| const [isLoggedIn, setIsLoggedIn] = useState(false); | ||
| const [provider, setProvider] = useState<Provider | undefined>(undefined); | ||
| const [events, setEvents] = useState<Array<{event: string, data: string, timestamp: string}>>([]); | ||
| const [address, setAddress] = useState<string>(''); | ||
| const [chainId, setChainId] = useState<string>(''); | ||
| const [loading, setLoading] = useState(false); | ||
| const [accountsState, setAccountsState] = useState<string[]>([]); | ||
|
|
||
| // Add a new event to the event log | ||
| const logEvent = useCallback((eventName: string, data: any) => { | ||
| setEvents(prev => [ | ||
| { | ||
| event: eventName, | ||
| data: JSON.stringify(data, null, 2), | ||
| timestamp: new Date().toLocaleTimeString() | ||
| }, | ||
| ...prev | ||
| ].slice(0, 10)); // Keep only the last 10 events | ||
| }, []); | ||
|
|
||
| // Handler for accountsChanged event | ||
| const handleAccountsChanged = useCallback((accounts: string[]) => { | ||
| console.log('accounts changed:', accounts); | ||
| setAccountsState(accounts); | ||
| logEvent(ProviderEvent.ACCOUNTS_CHANGED, { accounts }); | ||
|
|
||
| if (accounts.length === 0) { | ||
| // User has disconnected their account | ||
| setIsLoggedIn(false); | ||
| setAddress(''); | ||
| setChainId(''); // Clear chainId on disconnect | ||
| } else { | ||
| setAddress(accounts[0]); | ||
| // Potentially fetch chainId again if needed, or assume it hasn't changed | ||
| } | ||
| }, [logEvent]); | ||
|
|
||
| // Initialize provider on mount | ||
| useEffect(() => { | ||
| const fetchPassportProvider = async () => { | ||
| // Check if user is already logged in | ||
| const user = await passportInstance.getUserInfo(); | ||
| if (user) { | ||
| const provider = await passportInstance.connectEvm(); | ||
| setProvider(provider); | ||
| if (provider) { | ||
| const accounts = await provider.request({ method: 'eth_accounts' }); | ||
| if (accounts && accounts.length > 0) { | ||
| setAddress(accounts[0]); | ||
| setAccountsState(accounts); | ||
| logEvent('initial_accounts', { accounts }); | ||
| const chainId = await provider.request({ method: 'eth_chainId' }); | ||
| setChainId(chainId); | ||
| logEvent('initial_chain_id', { chainId }); | ||
| } | ||
| } | ||
| setIsLoggedIn(true); | ||
| } else { | ||
| // Optionally connectEvm even if not logged in to set up listeners early | ||
| // const provider = await passportInstance.connectEvm(); | ||
| // setProvider(provider); | ||
| } | ||
| }; | ||
|
|
||
| fetchPassportProvider(); | ||
| }, [logEvent]); // Added logEvent dependency | ||
|
|
||
| // Set up accountsChanged event listener | ||
| useEffect(() => { | ||
| if (!provider) return; | ||
|
|
||
| // Register event listener | ||
| provider.on(ProviderEvent.ACCOUNTS_CHANGED, handleAccountsChanged); | ||
|
|
||
| // Log that event listener was registered | ||
| logEvent('provider_event_registered', { event: ProviderEvent.ACCOUNTS_CHANGED }); | ||
|
|
||
| // Cleanup function to remove event listener | ||
| return () => { | ||
| provider.removeListener(ProviderEvent.ACCOUNTS_CHANGED, handleAccountsChanged); | ||
| }; | ||
| }, [provider, handleAccountsChanged, logEvent]); | ||
|
|
||
| // Handle login | ||
| const handleLogin = async () => { | ||
| try { | ||
| setLoading(true); | ||
| await passportInstance.login(); | ||
|
|
||
| // After login, get the provider | ||
| const provider = await passportInstance.connectEvm(); | ||
| setProvider(provider); | ||
|
|
||
| if (provider) { | ||
| // Get accounts | ||
| const accounts = await provider.request({ method: 'eth_requestAccounts' }); | ||
| if (accounts && accounts.length > 0) { | ||
| setAddress(accounts[0]); | ||
| setAccountsState(accounts); | ||
| // Log the accounts change manually since the event might not fire | ||
| logEvent(ProviderEvent.ACCOUNTS_CHANGED, { accounts }); | ||
| } | ||
|
|
||
| // Get chain ID | ||
| const chainId = await provider.request({ method: 'eth_chainId' }); | ||
| setChainId(chainId); | ||
| } | ||
|
|
||
| setIsLoggedIn(true); | ||
| } catch (error) { | ||
| console.error('Login error:', error); | ||
| logEvent('login_error', { message: error instanceof Error ? error.message : String(error) }); | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| }; | ||
|
|
||
| // Handle logout | ||
| const handleLogout = async () => { | ||
| try { | ||
| setLoading(true); | ||
| await passportInstance.logout(); | ||
| setIsLoggedIn(false); | ||
| setAddress(''); | ||
| setChainId(''); | ||
| setAccountsState([]); | ||
| setProvider(undefined); // Clear provider on logout | ||
| logEvent('logout_success', {}); // Log successful logout | ||
| } catch (error) { | ||
| console.error('Logout error:', error); | ||
| logEvent('logout_error', { message: error instanceof Error ? error.message : String(error) }); | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <Heading size="medium" className="mb-1">Passport SDK - Event Handling Example</Heading> | ||
|
|
||
| {/* Buttons Section */} | ||
|
|
||
| {!isLoggedIn && ( | ||
| <Button | ||
| onClick={handleLogin} | ||
| disabled={loading} | ||
| className="mb-1" | ||
| size="medium" | ||
| > | ||
| Login {loading ? '...' : ''} | ||
| </Button> | ||
| )} | ||
| {isLoggedIn && ( | ||
| <Button | ||
| onClick={handleLogout} | ||
| disabled={loading} | ||
| className="mb-1" | ||
| size="medium" | ||
| > | ||
| {loading ? 'Logging out...' : 'Logout'} | ||
| </Button> | ||
| )} | ||
|
|
||
| {/* State Data Table */} | ||
| {(isLoggedIn || accountsState.length > 0) && ( | ||
| <> | ||
| <Table> | ||
| <Table.Head> | ||
| <Table.Row> | ||
| <Table.Cell>Key</Table.Cell> | ||
| <Table.Cell>Value</Table.Cell> | ||
| </Table.Row> | ||
| </Table.Head> | ||
| <Table.Body> | ||
| <Table.Row> | ||
| <Table.Cell>Status</Table.Cell> | ||
| <Table.Cell>{isLoggedIn ? 'Logged In' : 'Logged Out'}</Table.Cell> | ||
| </Table.Row> | ||
| {address && ( | ||
| <Table.Row> | ||
| <Table.Cell>Address</Table.Cell> | ||
| <Table.Cell><code>{address}</code></Table.Cell> | ||
| </Table.Row> | ||
| )} | ||
| {chainId && ( | ||
| <Table.Row> | ||
| <Table.Cell>Chain ID</Table.Cell> | ||
| <Table.Cell><code>{chainId}</code></Table.Cell> | ||
| </Table.Row> | ||
| )} | ||
| {accountsState.length > 0 && ( | ||
| <Table.Row> | ||
| <Table.Cell>Accounts ({accountsState.length})</Table.Cell> | ||
| <Table.Cell> | ||
| <div style={{ maxHeight: '100px', overflowY: 'auto'}}> | ||
| {accountsState.map((account, idx) => ( | ||
| <div key={idx} style={{ marginBottom: "4px" }}> | ||
| <code>{account}</code> {idx === 0 && <span style={{ fontSize: "12px", color: "#666" }}>(active)</span>} | ||
| </div> | ||
| ))} | ||
| </div> | ||
| </Table.Cell> | ||
| </Table.Row> | ||
| )} | ||
| </Table.Body> | ||
| </Table> | ||
| </> | ||
| )} | ||
| <br /> | ||
| {/* Event Log Section */} | ||
| <> | ||
| <Heading size="small">Event Log:</Heading> | ||
| <div className="event-log" style={{ | ||
| maxHeight: '300px', | ||
| overflowY: 'auto', | ||
| border: '1px solid #ddd', | ||
| borderRadius: '4px', | ||
| padding: '12px', | ||
| width: '100%' | ||
| }}> | ||
| {events.length === 0 ? ( | ||
| <Body>No events logged yet</Body> | ||
| ) : ( | ||
| events.map((event, index) => ( | ||
| <div key={index} style={{ | ||
| marginBottom: '12px', | ||
| padding: '8px', | ||
| backgroundColor: '#f5f5f5', | ||
| borderRadius: '4px' | ||
| }}> | ||
| <Stack direction="row" gap="space.xsmall" alignItems="center"> | ||
| {/* Use a more distinct tag style */} | ||
| <span style={{ | ||
| backgroundColor: '#e0e0e0', | ||
| padding: '2px 6px', | ||
| borderRadius: '4px', | ||
| fontSize: '12px', | ||
| fontWeight: 'bold' | ||
| }}> | ||
| {event.event} | ||
| </span> | ||
| <Body size="small" color="secondary">{event.timestamp}</Body> | ||
| </Stack> | ||
| <pre style={{ | ||
| marginTop: '8px', | ||
| overflow: 'auto', | ||
| fontSize: '12px', | ||
| backgroundColor: '#fff', // White background for pre | ||
| padding: '8px', | ||
| borderRadius: '4px', | ||
| border: '1px solid #eee' | ||
| }}> | ||
| {event.data} | ||
| </pre> | ||
| </div> | ||
| )) | ||
| )} | ||
| </div> | ||
| </> | ||
| <br /> | ||
| <Link rc={<NextLink href="/" />}>Return to Examples</Link> | ||
| </> | ||
| ); | ||
|
darrenmelvison1 marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.