-
Notifications
You must be signed in to change notification settings - Fork 38
feat(auth-next,wallet): add default auth with auto-detection #2792
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
Open
rodrigo-fournier-immutable
wants to merge
19
commits into
main
Choose a base branch
from
ID-4319
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ef313b9
test(wallet): add comprehensive tests for default auth
rodrigo-fournier-immutable 103c7e7
feat(auth-next-client,auth-next-server): add default auth with auto-d…
rodrigo-fournier-immutable e173a4e
chore(examples): add auth-next default auth test app
rodrigo-fournier-immutable 6b67639
feat(examples): add default auth example to wallets-connect-with-nextjs
rodrigo-fournier-immutable e77a2cc
Merge branch 'main' into ID-4319
rodrigo-fournier-immutable b55cd2c
chore: update pnpm-lock.yaml after merge
rodrigo-fournier-immutable 46a72c9
fix(examples,auth-next): fix build issues and add missing config
rodrigo-fournier-immutable 106e791
fix(auth-next,examples): address PR feedback and deduplicate code
rodrigo-fournier-immutable 81855db
chore(auth-next,examples): upgrade to Next.js 15.2.6 (patches both CVEs)
rodrigo-fournier-immutable 5b23657
refactor(auth-next-client): remove Partial from LoginConfig and Logou…
rodrigo-fournier-immutable e48702f
fix(auth-next-server): add NODE_ENV fallback for server-side sandbox …
rodrigo-fournier-immutable badcef1
feat(sdk-sample-app): add default auth option for zero-config testing
rodrigo-fournier-immutable 6366c33
refactor(auth-next-server): enforce zero-config=sandbox, full-config=…
rodrigo-fournier-immutable 866ebd8
refactor(auth-next): simplify config to no config or full config policy
rodrigo-fournier-immutable cfa0370
refactor(auth-next): remove unused code and enforce valid redirect URIs
rodrigo-fournier-immutable 051f252
Remove defaultConfig.ts
rodrigo-fournier-immutable 5f95c49
fix(auth-next-server): use __NEXT_PRIVATE_ORIGIN for redirectUri in z…
rodrigo-fournier-immutable 576c756
Change code owners from the examples folder
rodrigo-fournier-immutable ddc1a33
chore: merge main and resolve pnpm-lock.yaml conflict
rodrigo-fournier-immutable 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
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 |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| NEXT_PUBLIC_PUBLISHABLE_KEY="pk_imapik-test-5ss4GpFy-n@$$Ye3LSox" | ||
| NEXT_PUBLIC_CLIENT_ID="K846H940Uxokhz1aDb034QwBclYnAH24" | ||
| NEXT_PUBLIC_CLIENT_ID="K846H940Uxokhz1aDb034QwBclYnAH24" | ||
|
|
||
| # Required for Auth-Next example (NextAuth.js) | ||
| AUTH_SECRET="test-secret-key-for-development-minimum-32-characters-long-change-in-prod" |
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 |
|---|---|---|
| @@ -1,2 +1,7 @@ | ||
| NEXT_PUBLIC_PUBLISHABLE_KEY= | ||
| NEXT_PUBLIC_CLIENT_ID= | ||
| NEXT_PUBLIC_CLIENT_ID= | ||
|
|
||
| # Required for Auth-Next example (NextAuth.js) | ||
| # Generate with: openssl rand -base64 32 | ||
| # See: https://next-auth.js.org/configuration/options#nextauth_secret | ||
| AUTH_SECRET=your-secret-key-min-32-characters-long-change-this |
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
3 changes: 3 additions & 0 deletions
3
examples/passport/wallets-connect-with-nextjs/app/api/auth/[...nextauth]/route.ts
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,3 @@ | ||
| import { handlers } from "@/lib/auth-next"; | ||
|
|
||
| export const { GET, POST } = handlers; |
26 changes: 26 additions & 0 deletions
26
examples/passport/wallets-connect-with-nextjs/app/callback-auth-next/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,26 @@ | ||
| "use client"; | ||
|
|
||
| import { CallbackPage } from "@imtbl/auth-next-client"; | ||
| import { | ||
| DEFAULT_PRODUCTION_CLIENT_ID, | ||
| DEFAULT_SANDBOX_CLIENT_ID, | ||
| } from "@imtbl/auth-next-client"; | ||
|
|
||
| export default function AuthNextCallback() { | ||
| // Auto-detect environment and derive config | ||
| const isSandbox = typeof window !== 'undefined' && | ||
| (window.location.hostname.includes('sandbox') || window.location.hostname.includes('localhost')); | ||
|
|
||
| const config = { | ||
| clientId: isSandbox ? DEFAULT_SANDBOX_CLIENT_ID : DEFAULT_PRODUCTION_CLIENT_ID, | ||
| redirectUri: typeof window !== 'undefined' ? `${window.location.origin}/callback-auth-next` : '/callback-auth-next', | ||
| }; | ||
|
rodrigo-fournier-immutable marked this conversation as resolved.
Outdated
|
||
|
|
||
| return ( | ||
| <div style={{ padding: '2rem', textAlign: 'center' }}> | ||
| <h1>Processing authentication...</h1> | ||
| <CallbackPage config={config} redirectTo="/connect-with-auth-next" /> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
160 changes: 160 additions & 0 deletions
160
examples/passport/wallets-connect-with-nextjs/app/connect-with-auth-next/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,160 @@ | ||
| 'use client'; | ||
|
|
||
| import { Button, Heading, Body } from '@biom3/react'; | ||
| import { useImmutableSession, useLogin, useLogout } from '@imtbl/auth-next-client'; | ||
| import { connectWallet } from '@imtbl/wallet'; | ||
| import { useState } from 'react'; | ||
| import { SessionProvider } from 'next-auth/react'; | ||
|
|
||
| function ConnectWithAuthNextContent() { | ||
| const { isAuthenticated, session, getUser } = useImmutableSession(); | ||
| const { loginWithPopup, isLoggingIn, error: loginError } = useLogin(); | ||
| const { logout, isLoggingOut, error: logoutError } = useLogout(); | ||
| const [walletAddress, setWalletAddress] = useState<string>(''); | ||
| const [walletError, setWalletError] = useState<string>(''); | ||
|
|
||
| const handleLogin = async () => { | ||
| try { | ||
| await loginWithPopup(); // Zero config! | ||
| } catch (err) { | ||
| console.error('Login failed:', err); | ||
| } | ||
| }; | ||
|
|
||
| const handleLogout = async () => { | ||
| try { | ||
| await logout(); // Zero config! | ||
| } catch (err) { | ||
| console.error('Logout failed:', err); | ||
| } | ||
| }; | ||
|
|
||
| const handleConnectWallet = async () => { | ||
| try { | ||
| setWalletError(''); | ||
|
|
||
| // Connect wallet using getUser from useImmutableSession | ||
| const provider = await connectWallet({ | ||
| getUser, // Uses default auth from NextAuth session! | ||
| }); | ||
|
|
||
| // Get the wallet address | ||
| const accounts = await provider.request({ | ||
| method: 'eth_requestAccounts' | ||
| }) as string[]; | ||
|
|
||
| setWalletAddress(accounts[0]); | ||
| } catch (err) { | ||
| const errorMsg = err instanceof Error ? err.message : 'Failed to connect wallet'; | ||
| setWalletError(errorMsg); | ||
| console.error('Wallet connection failed:', err); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div style={{ padding: '2rem', maxWidth: '800px', margin: '0 auto' }}> | ||
| <Heading size="medium" className="mb-1"> | ||
| Connect Wallet with Auth-Next (Default Auth) | ||
| </Heading> | ||
|
|
||
| <Body size="medium" className="mb-1"> | ||
| This example demonstrates using <code>@imtbl/auth-next-client</code> and <code>@imtbl/wallet</code> | ||
| together with zero-config default auth. | ||
| </Body> | ||
|
|
||
| <div style={{ | ||
| background: '#f5f5f5', | ||
| padding: '1rem', | ||
| borderRadius: '0.5rem', | ||
| marginBottom: '1rem' | ||
| }}> | ||
| <strong>Status:</strong> {isAuthenticated ? '✅ Authenticated' : '❌ Not Authenticated'} | ||
| {session?.user?.email && ( | ||
| <div style={{ marginTop: '0.5rem' }}> | ||
| <strong>Email:</strong> {session.user.email} | ||
| </div> | ||
| )} | ||
| {walletAddress && ( | ||
| <div style={{ marginTop: '0.5rem' }}> | ||
| <strong>Wallet:</strong> <code>{walletAddress}</code> | ||
| </div> | ||
| )} | ||
| </div> | ||
|
|
||
| {!isAuthenticated ? ( | ||
| <> | ||
| <Button | ||
| size="medium" | ||
| className="mb-1" | ||
| onClick={handleLogin} | ||
| disabled={isLoggingIn} | ||
| > | ||
| {isLoggingIn ? 'Signing in...' : '🔐 Sign In (Zero Config)'} | ||
| </Button> | ||
| {loginError && ( | ||
| <div style={{ color: 'red', marginTop: '0.5rem' }}> | ||
| Error: {loginError} | ||
| </div> | ||
| )} | ||
| <Body size="small" className="mb-1" style={{ color: '#666' }}> | ||
| Uses <code>loginWithPopup()</code> with no configuration. | ||
| ClientId and redirectUri are auto-detected! | ||
| </Body> | ||
| </> | ||
| ) : ( | ||
| <> | ||
| <Button | ||
| size="medium" | ||
| className="mb-1" | ||
| onClick={handleConnectWallet} | ||
| disabled={!!walletAddress} | ||
| > | ||
| {walletAddress ? '✅ Wallet Connected' : '💼 Connect Wallet'} | ||
| </Button> | ||
|
|
||
| <Button | ||
| size="medium" | ||
| className="mb-1" | ||
| onClick={handleLogout} | ||
| disabled={isLoggingOut} | ||
| > | ||
| {isLoggingOut ? 'Signing out...' : '🚪 Sign Out'} | ||
| </Button> | ||
|
|
||
| {walletError && ( | ||
| <div style={{ color: 'red', marginTop: '0.5rem' }}> | ||
| Wallet Error: {walletError} | ||
| </div> | ||
| )} | ||
| {logoutError && ( | ||
| <div style={{ color: 'red', marginTop: '0.5rem' }}> | ||
| Logout Error: {logoutError} | ||
| </div> | ||
| )} | ||
|
|
||
| <div style={{ | ||
| background: '#e8f5e9', | ||
| padding: '1rem', | ||
| borderRadius: '0.5rem', | ||
| marginTop: '1rem' | ||
| }}> | ||
| <strong>✅ Integration Test:</strong> | ||
| <ul style={{ marginTop: '0.5rem', paddingLeft: '1.5rem' }}> | ||
| <li>Auth via <code>useImmutableSession()</code></li> | ||
| <li>Wallet via <code>connectWallet({ getUser })</code></li> | ||
| <li>Zero configuration required!</li> | ||
| </ul> | ||
| </div> | ||
| </> | ||
| )} | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| export default function ConnectWithAuthNext() { | ||
| return ( | ||
| <SessionProvider> | ||
| <ConnectWithAuthNextContent /> | ||
| </SessionProvider> | ||
| ); | ||
| } |
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
8 changes: 8 additions & 0 deletions
8
examples/passport/wallets-connect-with-nextjs/lib/auth-next.ts
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,8 @@ | ||
| import NextAuth from "next-auth"; | ||
| import { createDefaultAuthConfig } from "@imtbl/auth-next-server"; | ||
|
|
||
| /** | ||
| * Default auth configuration for testing. | ||
| * This uses zero-config setup to demonstrate default auth functionality. | ||
| */ | ||
| export const { handlers, auth, signIn, signOut } = NextAuth(createDefaultAuthConfig()); |
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
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
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.