-
Notifications
You must be signed in to change notification settings - Fork 101
feat: atproto oauth #273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zeucapua
wants to merge
22
commits into
npmx-dev:main
Choose a base branch
from
zeucapua:feat/atproto-oauth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,991
−984
Open
feat: atproto oauth #273
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
1c04dd9
implement atproto oauth
zeucapua 560f04d
Revert "implement atproto oauth"
zeucapua 6215eb3
working atproto auth w/o nuxt-auth-utils
zeucapua 8955ae6
Merge branch 'main' into feat/atproto-oauth
zeucapua 477cebb
working atproto oauth login and logout
zeucapua 50decf2
update auth styles
zeucapua 1a678ee
moved to server side storage for oauth sessions
fatfingers23 cea37b4
adds oauth to middleware
fatfingers23 86fb98a
moved to a defineEventHandler
fatfingers23 a4cecc9
wip
fatfingers23 aa3bf51
Merge pull request #1 from fatfingers23/feat/atproto-oauth
zeucapua 01b1fca
Merge branch 'main' into feat/atproto-oauth
zeucapua ad9e665
throw early on unset session password env
zeucapua e990fbd
proof of concept login/create buttons
fatfingers23 626c25d
Merge pull request #2 from fatfingers23/feat/atproto-oauth
zeucapua e721cb8
update session miniDoc types
zeucapua 0aad561
update login copy
zeucapua e267dff
add env step on CONTRIBUTING
zeucapua 0ecce4e
Merge remote-tracking branch 'origin/main' into feat/atproto-oauth
danielroe cb66960
chore: ignore exports
danielroe a45e707
chore: remove missing prop
danielroe 9102628
fix: use runtimeConfig and auto-gen dev session password
danielroe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| #secure password, can use openssl rand --hex 32 | ||
| NUXT_SESSION_PASSWORD="" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <script setup lang="ts"> | ||
| const showModal = ref(false) | ||
| const { user } = await useAtproto() | ||
| </script> | ||
|
|
||
| <template> | ||
| <div class="relative"> | ||
| <button | ||
| type="button" | ||
| class="relative font-mono text-sm flex items-center justify-center w-fit rounded-md transition-colors duration-200 hover:bg-bg-subtle focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50" | ||
| @click="showModal = true" | ||
| > | ||
| {{ user?.handle || 'login' }} | ||
| </button> | ||
|
|
||
| <AuthModal v-model:open="showModal" /> | ||
| </div> | ||
| </template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| <script setup lang="ts"> | ||
| const open = defineModel<boolean>('open', { default: false }) | ||
|
|
||
| const handleInput = ref('') | ||
|
|
||
| const { user, logout } = await useAtproto() | ||
|
|
||
| async function handleBlueskySignIn() { | ||
| await navigateTo( | ||
| { | ||
| path: '/api/auth/atproto', | ||
| query: { handle: 'https://bsky.social' }, | ||
| }, | ||
| { external: true }, | ||
| ) | ||
| } | ||
|
|
||
| async function handleCreateAccount() { | ||
| await navigateTo( | ||
| { | ||
| path: '/api/auth/atproto', | ||
| query: { handle: 'https://selfhosted.social', create: 'true' }, | ||
| }, | ||
| { external: true }, | ||
| ) | ||
| } | ||
|
|
||
| async function handleLogin() { | ||
| if (handleInput.value) { | ||
| await navigateTo( | ||
| { | ||
| path: '/api/auth/atproto', | ||
| query: { handle: handleInput.value }, | ||
| }, | ||
| { external: true }, | ||
| ) | ||
| } | ||
| } | ||
| </script> | ||
|
|
||
| <template> | ||
| <Teleport to="body"> | ||
| <Transition | ||
| enter-active-class="transition-opacity duration-200" | ||
| leave-active-class="transition-opacity duration-200" | ||
| enter-from-class="opacity-0" | ||
| leave-to-class="opacity-0" | ||
| > | ||
| <div v-if="open" class="fixed inset-0 z-50 flex items-center justify-center p-4"> | ||
| <!-- Backdrop --> | ||
| <button | ||
| type="button" | ||
| class="absolute inset-0 bg-black/60 cursor-default" | ||
| aria-label="Close modal" | ||
| @click="open = false" | ||
| /> | ||
|
|
||
| <!-- Modal --> | ||
| <div | ||
| class="relative w-full max-w-lg bg-bg border border-border rounded-lg shadow-xl max-h-[90vh] overflow-y-auto overscroll-contain" | ||
| role="dialog" | ||
| aria-modal="true" | ||
| aria-labelledby="auth-modal-title" | ||
| > | ||
| <div class="p-6"> | ||
| <div class="flex items-center justify-between mb-6"> | ||
| <h2 id="auth-modal-title" class="font-mono text-lg font-medium">Account</h2> | ||
| <button | ||
| type="button" | ||
| class="text-fg-subtle hover:text-fg transition-colors duration-200 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50 rounded" | ||
| aria-label="Close" | ||
| @click="open = false" | ||
| > | ||
| <span class="i-carbon-close block w-5 h-5" aria-hidden="true" /> | ||
| </button> | ||
| </div> | ||
|
|
||
| <div v-if="user?.handle" class="space-y-4"> | ||
| <div class="flex items-center gap-3 p-4 bg-bg-subtle border border-border rounded-lg"> | ||
| <span class="w-3 h-3 rounded-full bg-green-500" aria-hidden="true" /> | ||
| <div> | ||
| <p class="font-mono text-xs text-fg-muted">Logged in as @{{ user.handle }}</p> | ||
| </div> | ||
| </div> | ||
| <button | ||
| @click="logout" | ||
| class="w-full px-4 py-2 font-mono text-sm text-bg bg-fg rounded-md transition-all duration-200 hover:bg-fg/90 disabled:opacity-50 disabled:cursor-not-allowed focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50 focus-visible:ring-offset-2 focus-visible:ring-offset-bg" | ||
| > | ||
| Logout | ||
| </button> | ||
| </div> | ||
|
|
||
| <!-- Disconnected state --> | ||
| <form v-else class="space-y-4" @submit.prevent="handleLogin"> | ||
| <p class="text-sm text-fg-muted">Login with your Atmosphere account</p> | ||
|
|
||
| <div class="space-y-3"> | ||
| <div> | ||
| <label | ||
| for="handle-input" | ||
| class="block text-xs text-fg-subtle uppercase tracking-wider mb-1.5" | ||
| > | ||
| Handle | ||
| </label> | ||
| <input | ||
| id="handle-input" | ||
| v-model="handleInput" | ||
| type="text" | ||
| name="handle" | ||
| placeholder="alice.bsky.social" | ||
| autocomplete="off" | ||
| spellcheck="false" | ||
| class="w-full px-3 py-2 font-mono text-sm bg-bg-subtle border border-border rounded-md text-fg placeholder:text-fg-subtle transition-colors duration-200 focus:border-border-hover focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50" | ||
| /> | ||
| </div> | ||
|
|
||
| <details class="text-sm"> | ||
| <summary | ||
| class="text-fg-subtle cursor-pointer hover:text-fg-muted transition-colors duration-200" | ||
| > | ||
| What is an Atmosphere account? | ||
| </summary> | ||
| <div class="mt-3"> | ||
| <p> | ||
| <span class="font-bold">npmx.dev</span> is built on the | ||
| <a | ||
| href="https://atproto.com" | ||
| target="_blank" | ||
| class="text-blue-400 hover:underline" | ||
| > | ||
| AT Protocol </a | ||
| >, allowing users to own their data and use one account for all compatible | ||
| applications. Once you create an account, you can use other apps like | ||
| <a | ||
| href="https://bsky.app" | ||
| target="_blank" | ||
| class="text-blue-400 hover:underline" | ||
| > | ||
| Bluesky | ||
| </a> | ||
| and | ||
| <a | ||
| href="https://tangled.org" | ||
| target="_blank" | ||
| class="text-blue-400 hover:underline" | ||
| > | ||
| Tangled | ||
| </a> | ||
| with the same login. | ||
| </p> | ||
| </div> | ||
| </details> | ||
| </div> | ||
|
|
||
| <button | ||
| type="submit" | ||
| :disabled="!handleInput.trim()" | ||
| class="w-full px-4 py-2 font-mono text-sm text-bg bg-fg rounded-md transition-all duration-200 hover:bg-fg/90 disabled:opacity-50 disabled:cursor-not-allowed focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50 focus-visible:ring-offset-2 focus-visible:ring-offset-bg" | ||
| > | ||
| Login | ||
| </button> | ||
| <button | ||
| type="button" | ||
| @click="handleCreateAccount" | ||
| class="w-full px-4 py-2 font-mono text-sm text-bg bg-fg rounded-md transition-all duration-200 hover:bg-fg/90 disabled:opacity-50 disabled:cursor-not-allowed focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50 focus-visible:ring-offset-2 focus-visible:ring-offset-bg" | ||
| > | ||
| Create a new account | ||
| </button> | ||
| <hr /> | ||
| <button | ||
| type="button" | ||
| @click="handleBlueskySignIn" | ||
| class="w-full px-4 py-2 font-mono text-sm text-bg bg-fg rounded-md transition-all duration-200 hover:bg-fg/90 disabled:opacity-50 disabled:cursor-not-allowed focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-fg/50 focus-visible:ring-offset-2 focus-visible:ring-offset-bg flex items-center justify-center gap-2" | ||
| > | ||
| Sign in with Bluesky | ||
| <svg fill="none" viewBox="0 0 64 57" width="20" style="width: 20px"> | ||
| <path | ||
| fill="#0F73FF" | ||
| d="M13.873 3.805C21.21 9.332 29.103 20.537 32 26.55v15.882c0-.338-.13.044-.41.867-1.512 4.456-7.418 21.847-20.923 7.944-7.111-7.32-3.819-14.64 9.125-16.85-7.405 1.264-15.73-.825-18.014-9.015C1.12 23.022 0 8.51 0 6.55 0-3.268 8.579-.182 13.873 3.805ZM50.127 3.805C42.79 9.332 34.897 20.537 32 26.55v15.882c0-.338.13.044.41.867 1.512 4.456 7.418 21.847 20.923 7.944 7.111-7.32 3.819-14.64-9.125-16.85 7.405 1.264 15.73-.825 18.014-9.015C62.88 23.022 64 8.51 64 6.55c0-9.818-8.578-6.732-13.873-2.745Z" | ||
| ></path> | ||
| </svg> | ||
| </button> | ||
| </form> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </Transition> | ||
| </Teleport> | ||
| </template> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| type MiniDoc = { | ||
| did: string | ||
| handle: string | ||
| pds: string | ||
| } | ||
|
|
||
| /** @public */ | ||
| export async function useAtproto() { | ||
| const { | ||
| data: user, | ||
| pending, | ||
| clear, | ||
| } = await useAsyncData<MiniDoc | null>('user-state', async () => { | ||
| const data = await useRequestFetch()<MiniDoc>('/api/auth/session', { | ||
| headers: { accept: 'application/json' }, | ||
| }) | ||
|
|
||
| return data | ||
| }) | ||
|
|
||
| const logout = async () => { | ||
| await useRequestFetch()<MiniDoc>('/api/auth/session', { | ||
| method: 'delete', | ||
| headers: { accept: 'application/json' }, | ||
| }) | ||
|
|
||
| clear() | ||
| } | ||
|
|
||
| return { user, pending, logout } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { defineNuxtModule, useNuxt } from 'nuxt/kit' | ||
| import { join } from 'node:path' | ||
| import { appendFileSync, existsSync, readFileSync } from 'node:fs' | ||
| import { randomUUID } from 'node:crypto' | ||
|
|
||
| export default defineNuxtModule({ | ||
| meta: { | ||
| name: 'dev', | ||
| }, | ||
| setup() { | ||
| const nuxt = useNuxt() | ||
| if (nuxt.options._prepare || process.env.NUXT_SESSION_PASSWORD) { | ||
| return | ||
| } | ||
|
|
||
| const envPath = join(nuxt.options.rootDir, '.env') | ||
| const hasPassword = | ||
| existsSync(envPath) && /^NUXT_SESSION_PASSWORD=/m.test(readFileSync(envPath, 'utf-8')) | ||
|
|
||
| if (!hasPassword) { | ||
| console.info('Generating NUXT_SESSION_PASSWORD for development environment.') | ||
| const password = randomUUID().replace(/-/g, '') | ||
|
|
||
| nuxt.options.runtimeConfig.sessionPassword = password | ||
| appendFileSync(envPath, `# generated by dev module\nNUXT_SESSION_PASSWORD=${password}\n`) | ||
| } | ||
| }, | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.