Skip to content

Commit 8e13c9e

Browse files
add mutual friendship middleware
1 parent afa38c9 commit 8e13c9e

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { defineMiddleware } from "astro:middleware";
2+
import { AtpBaseClient } from "@atproto/api";
3+
4+
const MS_BOBA_DID = "did:plc:r2vpg2iszskbkegoldmqa322";
5+
const HAETAE_DID = "did:plc:dg2qmmjic7mmecrbvpuhtvh6";
6+
const FUJOCODED_DID = "did:plc:737bslnnf7vyaktosjlrshmd";
7+
8+
const FRIENDS_ONLY_PAGES = ["/secret"];
9+
10+
export const onRequest = defineMiddleware(async (context, next) => {
11+
if (!FRIENDS_ONLY_PAGES.includes(context.url.pathname)) {
12+
return next();
13+
}
14+
const loggedInDid = context.locals.loggedInUser?.did;
15+
if (!loggedInDid) {
16+
return context.redirect("/?reason=unauthorized");
17+
}
18+
19+
const agent = new AtpBaseClient("https://public.api.bsky.app");
20+
const relationships = await agent.app.bsky.graph.getRelationships({ actor: HAETAE_DID, others: [loggedInDid] });
21+
const usersRelationship = relationships.data.relationships.at(0);
22+
23+
// True if the current user follows you
24+
const follower = usersRelationship && "followedBy" in usersRelationship && Boolean(usersRelationship.followedBy);
25+
// True if you follow the current user
26+
const following = usersRelationship && "following" in usersRelationship && Boolean(usersRelationship.following);
27+
// True if this is a mutual relationship
28+
const mutuals = follower && following;
29+
30+
if (!following) {
31+
return context.redirect("/?reason=unauthorized");
32+
}
33+
34+
return next();
35+
});
Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,5 @@
11
---
22
import Login from "../../../dist/components/Login.astro";
3-
import {AtpBaseClient} from "@atproto/api";
4-
5-
const MS_BOBA_DID = "did:plc:r2vpg2iszskbkegoldmqa322";
6-
const HAETAE_DID = "did:plc:dg2qmmjic7mmecrbvpuhtvh6";
7-
const FUJOCODED_DID = "did:plc:737bslnnf7vyaktosjlrshmd";
8-
const user = Astro.locals.loggedInUser;
9-
if (!user?.did) {
10-
return Astro.redirect("/?reason=unauthorized");
11-
}
12-
const agent = new AtpBaseClient("https://public.api.bsky.app");
13-
const relationships = await agent.app.bsky.graph.getRelationships({actor: FUJOCODED_DID, others: [user.did]});
14-
const usersRelationship = relationships.data.relationships.at(0);
15-
16-
// True if the current user follows you
17-
const follower = usersRelationship && "followedBy" in usersRelationship && Boolean(usersRelationship.followedBy);
18-
// True if you follow the current user
19-
const following = usersRelationship && "following" in usersRelationship && Boolean(usersRelationship.following);
20-
// True if this is a mutual relationship
21-
const mutuals = follower && following;
22-
23-
if (!following) {
24-
return Astro.redirect("/?reason=unauthorized");
25-
}
263
---
274

285
<html lang="en">
@@ -34,7 +11,7 @@ if (!following) {
3411
<title>Astro</title>
3512
</head>
3613
<body>
37-
Only friends allowed. You {user.handle}, are a friend.
14+
Only friends allowed. You {Astro.locals.loggedInUser!.handle}, are a friend.
3815
<Login />
3916
</body>
4017
</html>

0 commit comments

Comments
 (0)