Skip to content

Commit

Permalink
Recreate bsky agent instead of passing a shared instance
Browse files Browse the repository at this point in the history
  • Loading branch information
pdelfan committed Dec 13, 2023
1 parent 275af19 commit ee60bcd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { JWT } from "next-auth/jwt";
import NextAuth, { NextAuthOptions, Session, User } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import { at } from "@/lib/api/bsky/agent";
import type {
GetServerSidePropsContext,
NextApiRequest,
NextApiResponse,
} from "next";
import { getServerSession } from "next-auth";
import { jwtDecode } from "jwt-decode";
import { createAgent } from "@/lib/api/bsky/agent";

export const authOptions: NextAuthOptions = {
providers: [
Expand All @@ -29,6 +29,8 @@ export const authOptions: NextAuthOptions = {
return null;
}

const at = createAgent();

const result = await at.login({
identifier: credentials.handle,
password: credentials.password,
Expand Down Expand Up @@ -67,6 +69,8 @@ export const authOptions: NextAuthOptions = {

// add extra properties to session
async session({ session, token }): Promise<Session> {
const at = createAgent();

const receivedToken = token as JWT & User;

session.user.email = receivedToken.email;
Expand Down
16 changes: 12 additions & 4 deletions src/lib/api/bsky/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ import { getSessionFromServer } from "@/app/api/auth/[...nextauth]/route";
import { BskyAgent } from "@atproto/api";
import { redirect } from "next/navigation";

export const at = new BskyAgent({
// TODO: allow PDS URL — using bsky.social for now
service: "https://bsky.social",
});
export const createAgent = () => {
const at = new BskyAgent({
service: "https://bsky.social",
});

return at;
};

export const getBskySession = async () => {
// TODO: allow PDS URL — using bsky.social for now
const at = new BskyAgent({
service: "https://bsky.social",
});

try {
const session = await getSessionFromServer();
if (!session?.user.bskySession) redirect("/");
Expand Down
5 changes: 3 additions & 2 deletions src/lib/hooks/bsky/useAgent.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useEffect, useState } from "react";
import { at } from "../../api/bsky/agent";
import { useSession } from "next-auth/react";
import { useRouter } from "next/navigation";
import { createAgent } from "@/lib/api/bsky/agent";

export default function useAgent() {
const at = createAgent();
const [agent, setAgent] = useState(at);
const { data: session, status } = useSession();
const router = useRouter();
Expand All @@ -22,7 +23,7 @@ export default function useAgent() {
};

getAgent();
}, [router, session, status]);
}, [at, router, session?.user.bskySession, status]);

return agent;
}

0 comments on commit ee60bcd

Please sign in to comment.