Skip to content

Commit

Permalink
Merge pull request #44 from Freegle/feature/google-doh-email-validation
Browse files Browse the repository at this point in the history
Email domain validation
  • Loading branch information
edwh authored Oct 18, 2023
2 parents e66f612 + db435dc commit fe82155
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions components/EmailValidator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@
label-class="mt-0"
:state="true"
>
<Field
ref="email"
v-model="currentEmail"
:rules="validateEmail"
type="email"
name="email"
:class="'email form-control input-' + size + ' ' + inputClass"
:center="center"
autocomplete="username email"
:placeholder="'Email address ' + (required ? '' : '(Optional)')"
/>
<ErrorMessage name="email" class="text-danger font-weight-bold" />
<Form ref="form" as="">
<Field
ref="email"
v-model="currentEmail"
:rules="validateEmail"
type="email"
name="email"
:class="'email form-control input-' + size + ' ' + inputClass"
:center="center"
autocomplete="username email"
:placeholder="'Email address ' + (required ? '' : '(Optional)')"
/>
<ErrorMessage name="email" class="text-danger font-weight-bold" />
</Form>
</b-form-group>
<div
v-if="suggestedDomains && suggestedDomains.length"
Expand All @@ -35,13 +37,13 @@
</div>
</template>
<script>
import { Field, ErrorMessage } from 'vee-validate'
import { Field, ErrorMessage, Form } from 'vee-validate'
import { useDomainStore } from '../stores/domain'
import { ref } from '#imports'
import { EMAIL_REGEX } from '~/constants'
export default {
components: { Field, ErrorMessage },
components: { Field, ErrorMessage, Form },
props: {
email: {
type: String,
Expand Down Expand Up @@ -120,15 +122,30 @@ export default {
}
}
const validate = await this.$refs.email?.validate()
if (validate) {
this.$emit('update:valid', validate.valid)
const meta = this.$refs.form?.getMeta();
if (meta) {
this.$emit('update:valid', meta.valid)
}
},
},
},
methods: {
validateEmail(value) {
async checkValidDomain(value) {
let isValidDomain = true;
try {
const domain = value.substring(value.indexOf('@') + 1)
const url = new URL('https://dns.googlee/resolve');
url.search = new URLSearchParams({ name: domain }).toString();
const googleResponse = await fetch(url).then(response => response);
const { Status: status } = await googleResponse.json();
isValidDomain = status === 0;
} catch (_) {
// if something doesn't work with google domain check we ignore this check
}
return isValidDomain;
},
async validateEmail(value) {
if (!value && !this.required) {
return true
}
Expand All @@ -141,7 +158,8 @@ export default {
return 'Please enter a valid email address.'
}
return true
const isValidDomain = await this.checkValidDomain(value);
return isValidDomain || 'Please check your email domain - maybe you\'ve made a typo?';
},
},
}
Expand Down

0 comments on commit fe82155

Please sign in to comment.