diff --git a/app/index.tsx b/app/index.tsx --- a/app/index.tsx +++ b/app/index.tsx @@ -23,6 +23,7 @@ You are logged in as {handle} - {status === 'logged-in' && ( - {"You're logged in!"} - )} - + + + Log in to bramblr + ATProto handle/username + + App password + + + + ); } diff --git a/src/context/AuthContext.tsx b/src/context/AuthContext.tsx --- a/src/context/AuthContext.tsx +++ b/src/context/AuthContext.tsx @@ -1,8 +1,9 @@ import AtpAgent, { Agent, CredentialSession } from "@atproto/api"; import { createContext, useEffect, useState } from "react"; -import { BrowserOAuthClient } from "@atproto/oauth-client-browser"; +import { BrowserOAuthClient, Session } from "@atproto/oauth-client-browser"; import { atprotoLoopbackClientMetadata } from "@atproto/oauth-types"; import { Client } from "@atproto/lex"; +import { SessionManager } from "@atproto/api/dist/session-manager"; export type AuthState = { agent: Agent | null; @@ -10,12 +11,14 @@ login: (handle: string) => Promise; loginWithPass: (handle: string, password: string) => Promise; + logout: () => void; }; export const AuthContext = createContext({} as AuthState); export function AuthProvider({ children }: { children: React.ReactNode }) { const [agent, setAgent] = useState(null); + const [session, setSession] = useState(null); const [oauthClient, setOauthClient] = useState( null, ); @@ -36,15 +39,16 @@ // } // Try resume session - const sessionString = localStorage.getItem("session") + const sessionString = localStorage.getItem("session"); if (sessionString) { - const session = new CredentialSession(new URL("https://bsky.social")) - const response = await session.resumeSession(JSON.parse(sessionString)) + const session = new CredentialSession(new URL("https://bsky.social")); + const response = await session.resumeSession(JSON.parse(sessionString)); if (!response.success) { - console.error("You need to log in again") - return + console.error("You need to log in again"); + return; } - setAgent(new Agent(session)) + setSession(session) + setAgent(new Agent(session)); } } initialize(); @@ -66,8 +70,19 @@ if (!response.success) throw "Fix me"; setAgent(new Agent(session)); + setSession(session); - localStorage.setItem("session", JSON.stringify(session.session!)) + localStorage.setItem("session", JSON.stringify(session.session!)); + } + + async function logout() { + if (session) { + await session.logout(); + localStorage.removeItem("session"); + setSession(null); + setAgent(null); + console.log("Logged out") + } } return ( @@ -76,7 +91,8 @@ agent: agent, client: agent && new Client(agent), login: login, - loginWithPass: loginWithPass + loginWithPass: loginWithPass, + logout: logout, }} > {children} diff --git a/src/components/primitive/Modal.tsx b/src/components/primitive/Modal.tsx --- a/src/components/primitive/Modal.tsx +++ b/src/components/primitive/Modal.tsx @@ -33,7 +33,7 @@ } else { return ( router.canDismiss() && router.dismiss()} + onPressIn={() => router.canDismiss() && router.dismiss()} >