|
| 1 | +<template> |
| 2 | + <div class="block w-full h-full flex items-center justify-center"> |
| 3 | + <div> |
| 4 | + <h3 class="text-4xl">This application is password protected.</h3> |
| 5 | + <input |
| 6 | + type="password" |
| 7 | + class="w-full p-2 px-6 text-2xl font-light mt-8 text-center outline-none shadow rounded-full" |
| 8 | + :class="{ shake: incorrect }" |
| 9 | + @keyup.enter="attemptLogin" |
| 10 | + v-model="pw" |
| 11 | + :disabled="loading" |
| 12 | + ref="inputRef" |
| 13 | + /> |
| 14 | + <div class="text-center mt-6 text-xl"> |
| 15 | + Press |
| 16 | + <span class="bg-blue-200 py-1 px-2 ml-2 mr-2 rounded text-blue-800" |
| 17 | + >Enter</span |
| 18 | + > |
| 19 | + to log in. |
| 20 | + </div> |
| 21 | + </div> |
| 22 | + </div> |
| 23 | +</template> |
| 24 | + |
| 25 | +<script setup> |
| 26 | +import { onMounted, ref } from 'vue' |
| 27 | +import { useRouter } from 'vue-router' |
| 28 | +import { callApi } from '../api' |
| 29 | +
|
| 30 | +const router = useRouter() |
| 31 | +const inputRef = ref(null) |
| 32 | +const pw = ref('') |
| 33 | +const pwPrev = ref('') |
| 34 | +const incorrect = ref(false) |
| 35 | +const loading = ref(false) |
| 36 | +
|
| 37 | +onMounted(() => { |
| 38 | + inputRef.value.focus() |
| 39 | +}) |
| 40 | +
|
| 41 | +async function attemptLogin() { |
| 42 | + if (pw.value === '' || pw.value === pwPrev.value) { |
| 43 | + return |
| 44 | + } |
| 45 | + pwPrev.value = pw.value |
| 46 | + const formData = new FormData() |
| 47 | + formData.append('password', pw.value) |
| 48 | +
|
| 49 | + loading.value = true |
| 50 | + const token = await callApi({ |
| 51 | + endpoint: 'login', |
| 52 | + method: 'POST', |
| 53 | + body: formData, |
| 54 | + login: true, |
| 55 | + }) |
| 56 | + loading.value = false |
| 57 | +
|
| 58 | + if (token) { |
| 59 | + localStorage.setItem('cloud-tasks-token', token) |
| 60 | + router.push({ name: 'home' }) |
| 61 | + } else { |
| 62 | + incorrect.value = true |
| 63 | + setTimeout(() => { |
| 64 | + incorrect.value = false |
| 65 | + }, 820) |
| 66 | + setTimeout(() => inputRef.value.focus(), 50) |
| 67 | + } |
| 68 | +} |
| 69 | +</script> |
| 70 | + |
| 71 | +<style lang="css" scoped> |
| 72 | +@keyframes shake { |
| 73 | + 8%, |
| 74 | + 41% { |
| 75 | + transform: translateX(-10px); |
| 76 | + } |
| 77 | + 25%, |
| 78 | + 58% { |
| 79 | + transform: translateX(10px); |
| 80 | + } |
| 81 | + 75% { |
| 82 | + transform: translateX(-5px); |
| 83 | + } |
| 84 | + 92% { |
| 85 | + transform: translateX(5px); |
| 86 | + } |
| 87 | + 0%, |
| 88 | + 100% { |
| 89 | + transform: translateX(0); |
| 90 | + } |
| 91 | +} |
| 92 | +
|
| 93 | +.shake { |
| 94 | + animation: shake 0.3s linear; |
| 95 | +} |
| 96 | +
|
| 97 | +input[type='password'] { |
| 98 | + font: small-caption; |
| 99 | + font-size: 36px; |
| 100 | +} |
| 101 | +</style> |
0 commit comments