Skip to content

Commit 2110a03

Browse files
committed
feat: identity and authentication
1 parent 8aec2c0 commit 2110a03

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+2240
-625
lines changed

.vscode/settings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"editor.formatOnSave": true,
33
"editor.codeActionsOnSave": {
4-
"source.removeUnused.ts": "explicit",
54
"source.organizeImports.biome": "explicit"
65
},
76
"[javascript]": {
@@ -42,5 +41,6 @@
4241
},
4342
"editor.suggest.insertMode": "replace",
4443
"editor.defaultFormatter": "biomejs.biome"
45-
}
44+
},
45+
"editor.tabSize": 2
4646
}

apps/events/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@
3737
"lucide-react": "^0.441.0",
3838
"react": "^18.3.1",
3939
"react-dom": "^18.3.1",
40+
"siwe": "^2.3.2",
4041
"tailwind-merge": "^2.5.3",
4142
"tailwindcss-animate": "^1.0.7",
4243
"uuid": "^11.0.3",
44+
"viem": "^2.21.56",
4345
"vite-plugin-node-polyfills": "^0.22.0"
4446
},
4547
"devDependencies": {

apps/events/src/Boot.tsx

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { PrivyProvider } from '@privy-io/react-auth';
21
import { RouterProvider, createRouter } from '@tanstack/react-router';
32

3+
import { AuthProvider } from './components/auth';
44
import { routeTree } from './routeTree.gen';
55

66
// Create a new router instance
@@ -15,23 +15,8 @@ declare module '@tanstack/react-router' {
1515

1616
export function Boot() {
1717
return (
18-
<PrivyProvider
19-
appId="cm1gt9i1b002g12ih6b6l4vvi"
20-
config={{
21-
// Display email and wallet as login methods
22-
loginMethods: ['wallet'],
23-
// Customize Privy's appearance in your app
24-
appearance: {
25-
theme: 'light',
26-
accentColor: '#676FFF',
27-
},
28-
// Create embedded wallets for users who don't have a wallet
29-
// embeddedWallets: {
30-
// createOnLogin: "users-without-wallets",
31-
// },
32-
}}
33-
>
18+
<AuthProvider>
3419
<RouterProvider router={router} />
35-
</PrivyProvider>
20+
</AuthProvider>
3621
);
3722
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { GraphLogin, useGraphLogin } from '@graph-framework/identity';
2+
import type { Signer } from '@graph-framework/identity/types';
3+
import { PrivyProvider, usePrivy, useWallets } from '@privy-io/react-auth';
4+
import { useRouter } from '@tanstack/react-router';
5+
import { useEffect, useState } from 'react';
6+
7+
function DoGraphLogin() {
8+
const { login } = useGraphLogin();
9+
useEffect(() => {
10+
console.log('Logging in to The Graph');
11+
login();
12+
}, []);
13+
return <div />;
14+
}
15+
16+
function Auth({ children }: { children: React.ReactNode }) {
17+
const { signMessage, authenticated } = usePrivy();
18+
const { wallets } = useWallets();
19+
const [signer, setSigner] = useState<Signer | null>(null);
20+
21+
useEffect(() => {
22+
const getSigner = async () => {
23+
const embeddedWallet = wallets.find((wallet) => wallet.walletClientType === 'privy') || wallets[0];
24+
const provider = await embeddedWallet.getEthersProvider();
25+
const newSigner = provider.getSigner();
26+
if (embeddedWallet.walletClientType === 'privy') {
27+
newSigner.signMessage = async (message) => {
28+
// @ts-expect-error signMessage is a string in this case
29+
const signature = await signMessage(message); //, uiConfig);
30+
31+
return signature;
32+
};
33+
}
34+
setSigner(newSigner);
35+
};
36+
37+
if (wallets.length > 0) {
38+
getSigner();
39+
}
40+
// eslint-disable-next-line react-hooks/exhaustive-deps
41+
}, [wallets]);
42+
43+
return (
44+
<>
45+
{signer && authenticated ? (
46+
<GraphLogin storage={localStorage} signer={signer}>
47+
<DoGraphLogin />
48+
{children}
49+
</GraphLogin>
50+
) : (
51+
children
52+
)}
53+
</>
54+
);
55+
}
56+
57+
export function AuthProvider({ children }: { children: React.ReactNode }) {
58+
return (
59+
<PrivyProvider
60+
appId="cm4wx6ziv00ngrmfjf9ik36iu"
61+
config={{
62+
// Display email and wallet as login methods
63+
loginMethods: ['email', 'wallet', 'google', 'twitter', 'github'],
64+
// Customize Privy's appearance in your app
65+
appearance: {
66+
theme: 'light',
67+
accentColor: '#676FFF',
68+
},
69+
// Create embedded wallets for users who don't have a wallet
70+
embeddedWallets: {
71+
createOnLogin: 'users-without-wallets',
72+
},
73+
}}
74+
>
75+
<Auth>{children}</Auth>
76+
</PrivyProvider>
77+
);
78+
}
79+
80+
export function RequireAuth({ children }: { children: React.ReactNode }) {
81+
const { authenticated } = usePrivy();
82+
const { authenticated: graphAuthenticated } = useGraphLogin();
83+
const router = useRouter();
84+
if (!authenticated || !graphAuthenticated) {
85+
router.navigate({
86+
to: '/login',
87+
});
88+
return <div />;
89+
}
90+
return <>{children}</>;
91+
}

apps/events/src/components/debug-invitations.tsx

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,12 @@ import { Button } from './ui/button';
44

55
type Props = {
66
invitations: Invitation[];
7-
encryptionPublicKey: string;
8-
encryptionPrivateKey: string;
9-
signaturePrivateKey: string;
107
accept: (params: {
11-
encryptionPublicKey: string;
12-
encryptionPrivateKey: string;
13-
signaturePrivateKey: string;
148
invitation: Invitation;
159
}) => Promise<unknown>;
1610
};
1711

18-
export function DebugInvitations({
19-
invitations,
20-
accept,
21-
encryptionPublicKey,
22-
encryptionPrivateKey,
23-
signaturePrivateKey,
24-
}: Props) {
12+
export function DebugInvitations({ invitations, accept }: Props) {
2513
return (
2614
<ul className="text-xs">
2715
{invitations.map((invitation) => {
@@ -31,9 +19,6 @@ export function DebugInvitations({
3119
<Button
3220
onClick={() => {
3321
accept({
34-
encryptionPublicKey,
35-
encryptionPrivateKey,
36-
signaturePrivateKey,
3722
invitation,
3823
});
3924
}}

apps/events/src/components/logout.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { wipeKeys } from '@/lib/keyStorage';
2-
import { redirect } from '@tanstack/react-router';
1+
import { useGraphLogin } from '@graph-framework/identity';
2+
import { usePrivy } from '@privy-io/react-auth';
3+
import { useRouter } from '@tanstack/react-router';
34
import { Button } from './ui/button';
45

56
export function Logout() {
7+
const { logout: graphLogout } = useGraphLogin();
8+
const { logout: privyLogout } = usePrivy();
9+
const router = useRouter();
610
const disconnectWallet = () => {
7-
const localStorageSignerAddress = localStorage.getItem('signerAddress');
8-
localStorage.removeItem('signerAddress');
9-
if (!localStorageSignerAddress) {
10-
return;
11-
}
12-
wipeKeys(localStorageSignerAddress);
13-
redirect({
11+
graphLogout();
12+
privyLogout();
13+
router.navigate({
1414
to: '/login',
1515
});
1616
};

apps/events/src/lib/isAuthenticated.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

apps/events/src/lib/keyStorage.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

apps/events/src/routeTree.gen.ts

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,10 @@ import { Route as SpaceSpaceIdImport } from './routes/space/$spaceId'
1919

2020
// Create Virtual Routes
2121

22-
const Login2LazyImport = createFileRoute('/login2')()
2322
const LoginLazyImport = createFileRoute('/login')()
2423

2524
// Create/Update Routes
2625

27-
const Login2LazyRoute = Login2LazyImport.update({
28-
id: '/login2',
29-
path: '/login2',
30-
getParentRoute: () => rootRoute,
31-
} as any).lazy(() => import('./routes/login2.lazy').then((d) => d.Route))
32-
3326
const LoginLazyRoute = LoginLazyImport.update({
3427
id: '/login',
3528
path: '/login',
@@ -79,13 +72,6 @@ declare module '@tanstack/react-router' {
7972
preLoaderRoute: typeof LoginLazyImport
8073
parentRoute: typeof rootRoute
8174
}
82-
'/login2': {
83-
id: '/login2'
84-
path: '/login2'
85-
fullPath: '/login2'
86-
preLoaderRoute: typeof Login2LazyImport
87-
parentRoute: typeof rootRoute
88-
}
8975
'/space/$spaceId': {
9076
id: '/space/$spaceId'
9177
path: '/space/$spaceId'
@@ -102,15 +88,13 @@ export interface FileRoutesByFullPath {
10288
'/': typeof IndexRoute
10389
'/playground': typeof PlaygroundRoute
10490
'/login': typeof LoginLazyRoute
105-
'/login2': typeof Login2LazyRoute
10691
'/space/$spaceId': typeof SpaceSpaceIdRoute
10792
}
10893

10994
export interface FileRoutesByTo {
11095
'/': typeof IndexRoute
11196
'/playground': typeof PlaygroundRoute
11297
'/login': typeof LoginLazyRoute
113-
'/login2': typeof Login2LazyRoute
11498
'/space/$spaceId': typeof SpaceSpaceIdRoute
11599
}
116100

@@ -119,38 +103,29 @@ export interface FileRoutesById {
119103
'/': typeof IndexRoute
120104
'/playground': typeof PlaygroundRoute
121105
'/login': typeof LoginLazyRoute
122-
'/login2': typeof Login2LazyRoute
123106
'/space/$spaceId': typeof SpaceSpaceIdRoute
124107
}
125108

126109
export interface FileRouteTypes {
127110
fileRoutesByFullPath: FileRoutesByFullPath
128-
fullPaths: '/' | '/playground' | '/login' | '/login2' | '/space/$spaceId'
111+
fullPaths: '/' | '/playground' | '/login' | '/space/$spaceId'
129112
fileRoutesByTo: FileRoutesByTo
130-
to: '/' | '/playground' | '/login' | '/login2' | '/space/$spaceId'
131-
id:
132-
| '__root__'
133-
| '/'
134-
| '/playground'
135-
| '/login'
136-
| '/login2'
137-
| '/space/$spaceId'
113+
to: '/' | '/playground' | '/login' | '/space/$spaceId'
114+
id: '__root__' | '/' | '/playground' | '/login' | '/space/$spaceId'
138115
fileRoutesById: FileRoutesById
139116
}
140117

141118
export interface RootRouteChildren {
142119
IndexRoute: typeof IndexRoute
143120
PlaygroundRoute: typeof PlaygroundRoute
144121
LoginLazyRoute: typeof LoginLazyRoute
145-
Login2LazyRoute: typeof Login2LazyRoute
146122
SpaceSpaceIdRoute: typeof SpaceSpaceIdRoute
147123
}
148124

149125
const rootRouteChildren: RootRouteChildren = {
150126
IndexRoute: IndexRoute,
151127
PlaygroundRoute: PlaygroundRoute,
152128
LoginLazyRoute: LoginLazyRoute,
153-
Login2LazyRoute: Login2LazyRoute,
154129
SpaceSpaceIdRoute: SpaceSpaceIdRoute,
155130
}
156131

@@ -167,7 +142,6 @@ export const routeTree = rootRoute
167142
"/",
168143
"/playground",
169144
"/login",
170-
"/login2",
171145
"/space/$spaceId"
172146
]
173147
},
@@ -180,9 +154,6 @@ export const routeTree = rootRoute
180154
"/login": {
181155
"filePath": "login.lazy.tsx"
182156
},
183-
"/login2": {
184-
"filePath": "login2.lazy.tsx"
185-
},
186157
"/space/$spaceId": {
187158
"filePath": "space/$spaceId.tsx"
188159
}

0 commit comments

Comments
 (0)