LoginView.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <template>
  2. <Section
  3. title="Login to OliveTin"
  4. :icon="Login01Icon"
  5. class="small"
  6. >
  7. <div class="login-form">
  8. <div
  9. v-if="!hasOAuth && !hasLocalLogin"
  10. class="login-disabled"
  11. >
  12. <span>This server is not configured with either OAuth, or local users, so you cannot login.</span>
  13. </div>
  14. <div
  15. v-if="hasOAuth"
  16. class="login-oauth2"
  17. >
  18. <h3>OAuth Login</h3>
  19. <div class="oauth-providers">
  20. <button
  21. v-for="provider in oauthProviders"
  22. :key="provider.key"
  23. class="oauth-button"
  24. @click="loginWithOAuth(provider)"
  25. >
  26. <span
  27. v-if="providerIcon(provider)"
  28. class="provider-icon"
  29. >
  30. <iconify-icon
  31. v-if="providerIcon(provider).kind === 'iconify'"
  32. :icon="providerIcon(provider).id"
  33. />
  34. <span v-else>{{ providerIcon(provider).text }}</span>
  35. </span>
  36. <span class="provider-name">Login with {{ provider.title }}</span>
  37. </button>
  38. </div>
  39. </div>
  40. <div
  41. v-if="hasLocalLogin"
  42. class="login-local"
  43. >
  44. <h3>Local Login</h3>
  45. <form
  46. class="local-login-form"
  47. @submit.prevent="handleLocalLogin"
  48. >
  49. <div
  50. v-if="loginError"
  51. class="bad"
  52. >
  53. {{ loginError }}
  54. </div>
  55. <input
  56. id="username"
  57. v-model="username"
  58. type="text"
  59. name="username"
  60. autocomplete="username"
  61. required
  62. placeholder="Username"
  63. >
  64. <input
  65. id="password"
  66. v-model="password"
  67. type="password"
  68. name="password"
  69. autocomplete="current-password"
  70. placeholder="Password"
  71. required
  72. >
  73. <button
  74. type="submit"
  75. :disabled="loading"
  76. class="login-button"
  77. >
  78. {{ loading ? 'Logging in...' : 'Login' }}
  79. </button>
  80. </form>
  81. </div>
  82. </div>
  83. </Section>
  84. </template>
  85. <script setup>
  86. import { ref, onMounted, watch } from 'vue'
  87. import { useRouter } from 'vue-router'
  88. import { Login01Icon } from '@hugeicons/core-free-icons'
  89. import Section from 'picocrank/vue/components/Section.vue'
  90. const router = useRouter()
  91. const username = ref('')
  92. const password = ref('')
  93. const loading = ref(false)
  94. const loginError = ref('')
  95. const hasOAuth = ref(false)
  96. const hasLocalLogin = ref(false)
  97. const oauthProviders = ref([])
  98. const trustedProviderIconifyIds = {
  99. github: 'simple-icons:github',
  100. google: 'simple-icons:google'
  101. }
  102. function providerIcon (provider) {
  103. const raw = (provider?.icon || '').trim()
  104. if (!raw) {
  105. return null
  106. }
  107. const iconifyTagMatch = raw.match(/<iconify-icon\b[^>]*\bicon=["']([^"']+)["'][^>]*>/i)
  108. if (iconifyTagMatch) {
  109. return { kind: 'iconify', id: iconifyTagMatch[1] }
  110. }
  111. if (/^[a-z0-9-]+:[a-z0-9-]+$/i.test(raw)) {
  112. return { kind: 'iconify', id: raw }
  113. }
  114. const trustedId = trustedProviderIconifyIds[provider.key]
  115. if (trustedId && (raw.includes('<') || raw === provider.key)) {
  116. return { kind: 'iconify', id: trustedId }
  117. }
  118. if (!raw.includes('<')) {
  119. return { kind: 'text', text: raw }
  120. }
  121. return trustedId ? { kind: 'iconify', id: trustedId } : null
  122. }
  123. function loadLoginOptions () {
  124. // Use the init response data that was loaded in App.vue
  125. if (window.initResponse) {
  126. hasOAuth.value = window.initResponse.oAuth2Providers && window.initResponse.oAuth2Providers.length > 0
  127. hasLocalLogin.value = window.initResponse.authLocalLogin
  128. if (hasOAuth.value) {
  129. oauthProviders.value = window.initResponse.oAuth2Providers
  130. }
  131. } else {
  132. console.warn('Init response not available yet, login options will be empty')
  133. }
  134. }
  135. async function handleLocalLogin () {
  136. loading.value = true
  137. loginError.value = ''
  138. try {
  139. const response = await window.client.localUserLogin({
  140. username: username.value,
  141. password: password.value
  142. })
  143. if (response.success) {
  144. // Re-initialize to get updated user context
  145. try {
  146. const initResponse = await window.client.init({})
  147. window.initResponse = initResponse
  148. window.initError = false
  149. window.initErrorMessage = ''
  150. window.initCompleted = true
  151. // Update the header with new user info
  152. if (window.updateHeaderFromInit) {
  153. window.updateHeaderFromInit()
  154. }
  155. } catch (initErr) {
  156. console.error('Failed to reinitialize after login:', initErr)
  157. }
  158. // Redirect to home page on successful login
  159. router.push('/')
  160. } else {
  161. loginError.value = 'Login failed. Please check your credentials.'
  162. }
  163. } catch (err) {
  164. console.error('Login error:', err)
  165. loginError.value = err.message || 'Network error. Please try again.'
  166. } finally {
  167. loading.value = false
  168. }
  169. }
  170. function loginWithOAuth (provider) {
  171. if (!provider.key) {
  172. console.error('OAuth provider missing key:', provider)
  173. return
  174. }
  175. const providerKey = encodeURIComponent(provider.key)
  176. window.location.href = `/oauth/login?provider=${providerKey}`
  177. }
  178. onMounted(() => {
  179. loadLoginOptions()
  180. // Also watch for when init response becomes available
  181. watch(() => window.initResponse, () => {
  182. loadLoginOptions()
  183. }, { immediate: true })
  184. })
  185. </script>
  186. <style scoped>
  187. section {
  188. margin: auto;
  189. }
  190. .login-view {
  191. min-height: 100vh;
  192. display: flex;
  193. align-items: center;
  194. justify-content: center;
  195. padding: 1rem;
  196. }
  197. form {
  198. grid-template-columns: 1fr;
  199. gap: 1em;
  200. }
  201. .provider-icon {
  202. width: 1em;
  203. height: 1em;
  204. margin-right: .4em;
  205. display: inline-flex;
  206. vertical-align: middle;
  207. }
  208. </style>