this is a scaffold for how to get client side oauth working with sveltekit and atproto
using the @atcute/oauth-browser-client library.
useful when you want people to login to your static sveltekit site.
- clone this repo
- run
npm install - run
npm run dev - go to
http://127.0.0.1:5179 - for deployment change the
SITE_URLvariable insrc/lib/oauth/const.ts(e.g. for github pages:https://your-username.github.io) and set your base insvelte.config.js(e.g. for github pages:base: '/your-repo-name/')
- copy the
src/lib/oauthfolder into your own project - also copy the
src/routes/client-metadata.jsonfolder into your project - add the following to your
src/routes/+layout.svelte
<script>
import { initClient } from '$lib/oauth';
onMount(() => {
initClient();
});
</script>
{@render children()}Either use the LoginModal component to render a login modal or use the client object to handle the login flow yourself.
// handlin login flow yourself
import { client } from '$lib/oauth';
// methods:
client.login(handle); // login the user
client.isLoggedIn; // check if the user is logged in
client.logout(); // logout the userLoginModal is a component that renders a login modal, add it for a quick login flow.
<script>
import { LoginModal, loginModalState } from '$lib/oauth';
</script>
<LoginModal />
<button onclick={() => loginModalState.show()}>Show Login Modal</button>Get the user's profile and make requests with the client.rpc object.
import { client } from '$lib/oauth';
// get the user's profile
const profile = client.profile;
// make requests with the client.rpc object
const response = await client.rpc.request({
type: 'get',
nsid: 'app.bsky.feed.getActorLikes',
params: {
actor: client.profile?.did,
limit: 10
}
});