aboutsummaryrefslogtreecommitdiff
path: root/extension/src/components/PasswordPrompt.vue
blob: ae29358f0caa43596b6eee6a80d23832a948ac1e (plain)
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
<template>
  <div id="password-prompt">
    <Dialog
      class="modal-entry"
      :style="style"
      :open="isRevealed"
      @close="close"
    >
      <div ref="dialog" class="modal-content-container" >
        <DialogPanel>
          <DialogTitle class="modal-title">
            Enter your password
          </DialogTitle>

          <DialogDescription class="modal-description">
            Please re-enter your password to continue.
          </DialogDescription>

          <form id="password-form" @submit.prevent="formSubmitted" :disabled="waiting">
            <fieldset>
                <div class="input-container">
                  <input v-model="v$.password.$model" type="password" class="rounded input primary" placeholder="Password" @input="onInput">
                </div>
            </fieldset>
          </form>

          <div class="modal-button-container">
            <button class="rounded btn sm primary" form="password-form">
              Submit
            </button>
            <button class="rounded btn sm" @click="close" >
              Close
            </button>
          </div>
        </DialogPanel>
      </div>
    </Dialog>
  </div>
</template>

<script setup lang="ts">
import { onClickOutside } from '@vueuse/core'
import useVuelidate from '@vuelidate/core'
import { reactive, ref, computed } from 'vue'
import { helpers, required, maxLength } from '@vuelidate/validators'
import { useWait, useMessage, usePassConfirm, useEnvSize, useVuelidateWrapper } from '@vnuge/vnlib.browser'
import { Dialog, DialogPanel, DialogTitle, DialogDescription } from '@headlessui/vue'

const { headerHeight } = useEnvSize()

//Use component side of pw prompt
const { isRevealed, confirm, cancel } = usePassConfirm()

const { waiting } = useWait()
const { onInput } = useMessage()

//Dialog html ref
const dialog = ref(null)

const pwState = reactive({ password: '' })

const rules = {
  password: {
    required: helpers.withMessage('Please enter your password', required),
    maxLength: helpers.withMessage('Password must be less than 100 characters', maxLength(100))
  }
}

const v$ = useVuelidate(rules, pwState, { $lazy: true })

//Wrap validator so we an display error message on validation, defaults to the form toaster
const { validate } = useVuelidateWrapper(v$);

const style = computed(() => {
  return {
    'height': `calc(100vh - ${headerHeight.value}px)`,
    'top': `${headerHeight.value}px`
  }
})

const formSubmitted = async function () {
  //Calls validate on the vuelidate instance
  if (!await validate()) {
    return
  }

  //Store pw copy
  const password = v$.value.password.$model;

  //Clear the password form
  v$.value.password.$model = '';
  v$.value.$reset();

  //Pass the password to the confirm function
  confirm({ password });
}

const close = function () {
  // Clear the password form
  v$.value.password.$model = '';
  v$.value.$reset();

  //Close prompt
  cancel(null);
}

//Cancel prompt when user clicks outside of dialog, only when its open
onClickOutside(dialog, () => isRevealed.value ? cancel() : null)

</script>