-
Notifications
You must be signed in to change notification settings - Fork 5
/
use-session.ts
192 lines (158 loc) · 5.01 KB
/
use-session.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useAsync, useAsyncFn } from 'react-use';
import type { ISession } from '@masa-finance/masa-sdk';
import { useWallet } from '../wallet-client/wallet/use-wallet';
import { useMasaClient } from '../masa-client/use-masa-client';
import { QcContext } from '../masa-provider';
export const useSession = () => {
const { address, isDisconnected, previousAddress } = useWallet();
const { masa } = useMasaClient();
const queryClient = useQueryClient({ context: QcContext });
// console.log({ address, previousAddress });
const [{ loading: isCheckingSession }, checkSession] =
useAsyncFn(async () => {
if (!address) return null;
if (!masa) return null;
const hasSesh = await masa?.session.checkLogin();
if (hasSesh !== undefined || hasSesh !== null) return hasSesh;
return null;
}, [masa, address]);
const [{ loading: isLoggingIn }, loginSession] = useAsyncFn(async () => {
if (!masa) return null;
if (!address) return null;
const loginObj = await masa.session.login();
if (!loginObj) {
return null;
}
return loginObj as unknown as ISession & {
userId: string;
address: string;
};
}, [masa, address]);
const [, getSessionAsync] = useAsyncFn(async () => {
if (!masa) return null;
if (!address) return null;
const seshFromGet = await masa?.session.getSession();
if (seshFromGet === undefined || seshFromGet === null) {
return null;
}
return seshFromGet;
}, [address, masa]);
const {
data: sessionFromGet,
isLoading: isFetchingSession,
refetch: getSessionCB,
} = useQuery({
queryKey: ['session-obj', address],
enabled: false,
context: QcContext,
onSuccess: async (data: ISession | null) => {
if (data) {
const resultCheck = await checkSession();
if (!resultCheck) {
// await queryClient.invalidateQueries(['session-check', address]);
}
}
},
queryFn: async () => getSessionAsync(),
});
const { data: hasSession, refetch: checkLogin } = useQuery({
queryKey: ['session-check', address],
enabled: !!masa,
context: QcContext,
onSuccess: async (data: boolean) => {
switch (data) {
case true: {
const checkedLogin = await checkSession();
if (!checkedLogin) {
await queryClient.invalidateQueries(['session-obj', address]);
await queryClient.invalidateQueries(['session-check', address]);
await queryClient.invalidateQueries(['session', address]);
return;
}
if (sessionFromGet && address === sessionFromGet?.user.address) {
return;
}
await queryClient.invalidateQueries(['session-obj', address]);
await queryClient.fetchQuery(['session-obj', address]);
break;
}
case false: {
queryClient.setQueryData(['session', address], null);
queryClient.setQueryData(['session-obj', address], null);
break;
}
case undefined: {
queryClient.setQueryData(['session', address], null);
queryClient.setQueryData(['session-obj', address], null);
break;
}
default: {
break;
}
}
},
queryFn: async () => {
if (!address) return false;
if (!masa) return false;
const hasSesh = await checkSession();
if (hasSesh) {
return hasSesh;
}
return false;
},
});
const [{ loading: isLoggingOut }, logoutSession] = useAsyncFn(async () => {
if (hasSession) {
await masa?.session.logout();
}
await queryClient.invalidateQueries(['session-check', address]);
await queryClient.invalidateQueries(['session', address]);
await queryClient.invalidateQueries(['session-obj', address]);
}, [masa, queryClient, address, hasSession]);
const { data: session, refetch: getSession } = useQuery({
queryKey: ['session', address],
enabled: false,
context: QcContext,
refetchOnMount: false,
onSettled: async () => {
await checkLogin();
},
queryFn: async () => {
const hasIt = await checkSession();
if (hasIt) {
const sesshFromGet = await masa?.session.getSession();
if (sesshFromGet === undefined || sesshFromGet === null) {
return null;
}
return sesshFromGet;
}
const sesh = await loginSession();
if (sesh === undefined || sesh === null) return null;
return sesh;
},
});
useAsync(async () => {
if (isDisconnected) {
await logoutSession();
}
if (previousAddress !== address) {
await checkLogin();
}
}, [address, previousAddress, checkLogin, isDisconnected, logoutSession]);
return {
session,
sessionFromGet,
getSessionCB,
isFetchingSession,
hasSession,
checkSession,
isCheckingSession,
loginSession,
logoutSession,
isLoggingIn,
isLoggingOut,
getSession,
checkLogin,
};
};