File tree Expand file tree Collapse file tree 2 files changed +36
-24
lines changed
astro-authproto/__example__/src Expand file tree Collapse file tree 2 files changed +36
-24
lines changed Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 11---
22import 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 >
You can’t perform that action at this time.
0 commit comments