forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthModal.client.vue
More file actions
163 lines (146 loc) · 5.06 KB
/
AuthModal.client.vue
File metadata and controls
163 lines (146 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<script setup lang="ts">
import { useAtproto } from '~/composables/atproto/useAtproto'
import { authRedirect } from '~/utils/atproto/helpers'
import { isAtIdentifierString } from '@atproto/lex'
const authModal = useModal('auth-modal')
const handleInput = shallowRef('')
const errorMessage = shallowRef('')
const route = useRoute()
const { user, logout } = useAtproto()
// https://atproto.com supports 4 locales as of 2026-02-07
const { locale } = useI18n()
const currentLang = locale.value.split('-')[0] ?? 'en'
const localeSubPath = ['ko', 'pt', 'ja'].includes(currentLang) ? currentLang : ''
const atprotoLink = `https://atproto.com/${localeSubPath}`
async function handleBlueskySignIn() {
await authRedirect('https://bsky.social', { redirectTo: route.fullPath, locale: locale.value })
}
async function handleCreateAccount() {
await authRedirect('https://npmx.social', {
create: true,
redirectTo: route.fullPath,
locale: locale.value,
})
}
async function handleLogin() {
if (handleInput.value) {
// URLS to PDSs are valid for initiating oauth flows
if (handleInput.value.startsWith('https://') || isAtIdentifierString(handleInput.value)) {
await authRedirect(handleInput.value, {
redirectTo: route.fullPath,
locale: locale.value,
})
} else {
errorMessage.value = $t('auth.modal.default_input_error')
}
}
}
watch(handleInput, newHandleInput => {
errorMessage.value = ''
if (!newHandleInput) return
const normalized = newHandleInput.trim().toLowerCase().replace(/@/g, '')
if (normalized !== newHandleInput) {
handleInput.value = normalized
}
})
watch(user, async newUser => {
if (newUser?.relogin) {
await authRedirect(newUser.did, {
redirectTo: route.fullPath,
})
}
})
</script>
<template>
<!-- Modal -->
<Modal :modalTitle="$t('auth.modal.title')" class="max-w-lg" id="auth-modal">
<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">
{{ $t('auth.modal.connected_as', { handle: user.handle }) }}
</p>
</div>
</div>
<div class="flex flex-col space-y-4">
<LinkBase
variant="button-secondary"
:to="{ name: 'profile-identity', params: { identity: user.handle } }"
prefetch-on="interaction"
class="w-full"
@click="authModal.close()"
>
{{ $t('auth.modal.profile') }}
</LinkBase>
<ButtonBase class="w-full" @click="logout">
{{ $t('auth.modal.disconnect') }}
</ButtonBase>
</div>
</div>
<!-- Disconnected state -->
<form v-else class="space-y-4" @submit.prevent="handleLogin">
<p class="text-sm text-fg-muted">{{ $t('auth.modal.connect_prompt') }}</p>
<div class="space-y-3">
<div>
<label
for="handle-input"
class="block text-xs text-fg-subtle uppercase tracking-wider mb-1.5"
>
{{ $t('auth.modal.handle_label') }}
</label>
<InputBase
id="handle-input"
v-model="handleInput"
type="text"
name="handle"
:placeholder="$t('auth.modal.handle_placeholder')"
no-correct
class="w-full"
/>
<p v-if="errorMessage" class="text-red-500 text-xs mt-1" role="alert">
{{ errorMessage }}
</p>
</div>
<details class="text-sm">
<summary
class="text-fg-subtle hover:text-fg-muted transition-colors duration-200 focus-visible:(outline-2 outline-accent/70)"
>
{{ $t('auth.modal.what_is_atmosphere') }}
</summary>
<div class="mt-3">
<i18n-t keypath="auth.modal.atmosphere_explanation" tag="p" scope="global">
<template #npmx>
<span class="font-bold">npmx.dev</span>
</template>
<template #atproto>
<LinkBase :to="atprotoLink"> AT Protocol </LinkBase>
</template>
<template #bluesky>
<LinkBase to="https://bsky.app"> Bluesky </LinkBase>
</template>
<template #tangled>
<LinkBase to="https://tangled.org"> Tangled </LinkBase>
</template>
</i18n-t>
</div>
</details>
</div>
<ButtonBase type="submit" variant="primary" :disabled="!handleInput.trim()" class="w-full">
{{ $t('auth.modal.connect') }}
</ButtonBase>
<ButtonBase type="button" class="w-full" @click="handleCreateAccount">
{{ $t('auth.modal.create_account') }}
</ButtonBase>
<hr class="color-border" />
<ButtonBase
type="button"
class="w-full"
@click="handleBlueskySignIn"
classicon="i-simple-icons:bluesky"
>
{{ $t('auth.modal.connect_bluesky') }}
</ButtonBase>
</form>
</Modal>
</template>