tangled
alpha
login
or
join now
teal.fm
/
teal
110
fork
atom
Your music, beautifully tracked. All yours. (coming soon)
teal.fm
teal-fm
atproto
110
fork
atom
overview
issues
pulls
pipelines
add external pds support :sob:
Natalie B.
1 year ago
0c6aab1f
2055850a
+40
-2
1 changed file
expand all
collapse all
unified
split
apps
amethyst
stores
authenticationSlice.tsx
+40
-2
apps/amethyst/stores/authenticationSlice.tsx
···
6
6
import * as Lexicons from "@teal/lexicons/src/lexicons";
7
7
import { resolveFromIdentity } from "@/lib/atp/pid";
8
8
9
9
+
export interface AllProfileViews {
10
10
+
bsky: null | ProfileViewDetailed;
11
11
+
// todo: teal profile view
12
12
+
}
13
13
+
9
14
export interface AuthenticationSlice {
10
15
auth: AquareumOAuthClient;
11
16
status: "start" | "loggedIn" | "loggedOut";
···
13
18
oauthSession: null | OAuthSession;
14
19
pdsAgent: null | Agent;
15
20
isAgentReady: boolean;
16
16
-
profiles: { [key: string]: ProfileViewDetailed };
21
21
+
profiles: { [key: string]: AllProfileViews };
17
22
client: null | AquareumOAuthClient;
18
23
login: {
19
24
loading: boolean;
···
28
33
oauthCallback: (state: URLSearchParams) => Promise<void>;
29
34
restorePdsAgent: () => void;
30
35
logOut: () => void;
36
36
+
populateLoggedInProfile: () => Promise<void>;
31
37
}
32
38
33
39
export const createAuthenticationSlice: StateCreator<AuthenticationSlice> = (
···
96
102
pdsAgent: addDocs(agent),
97
103
isAgentReady: true,
98
104
});
105
105
+
get().populateLoggedInProfile();
99
106
} catch (error: any) {
100
107
console.error("OAuth callback failed:", error);
101
108
set({
···
131
138
isAgentReady: true,
132
139
status: "loggedIn",
133
140
});
141
141
+
get().populateLoggedInProfile();
134
142
console.log("Restored agent");
135
143
} catch (error) {
136
144
console.error("Failed to restore agent:", error);
···
139
147
},
140
148
logOut: () => {
141
149
console.log("Logging out");
150
150
+
let profiles = { ...get().profiles };
151
151
+
// TODO: something better than 'delete'
152
152
+
delete profiles[get().pdsAgent?.did ?? ""];
142
153
set({
143
154
status: "loggedOut",
144
155
oauthSession: null,
145
156
oauthState: null,
146
146
-
profiles: {},
157
157
+
profiles,
147
158
pdsAgent: null,
148
159
client: null,
160
160
+
pds: null,
149
161
});
162
162
+
},
163
163
+
populateLoggedInProfile: async () => {
164
164
+
console.log("Populating logged in profile");
165
165
+
const agent = get().pdsAgent;
166
166
+
if (!agent) {
167
167
+
throw new Error("No agent");
168
168
+
}
169
169
+
if (!agent.did) {
170
170
+
throw new Error("No agent did! This is bad!");
171
171
+
}
172
172
+
try {
173
173
+
let bskyProfile = await agent
174
174
+
.getProfile({ actor: agent.did })
175
175
+
.then((profile) => {
176
176
+
console.log(profile);
177
177
+
return profile.data || null;
178
178
+
});
179
179
+
180
180
+
set({
181
181
+
profiles: {
182
182
+
[agent.did]: { bsky: bskyProfile },
183
183
+
},
184
184
+
});
185
185
+
} catch (error) {
186
186
+
console.error("Failed to get profile:", error);
187
187
+
}
150
188
},
151
189
};
152
190
};