How to Use Vue js 3 to Generate Random Passwords

1 year ago admin Vuejs

In this tutorial, we will see how to use Vue js 3 to generate random passwords, so our application will generate a password based on options selected by the user he can include lowercase or uppercase characters, numbers, or symbols he can also copy the generated password to clipboard.


Create the helpers.js file

First, we will create a file 'helpers.js' that will contain the options the user can select.

                                                    
                                                                                                                
export const numbers = "0123456789";
export const upperCaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
export const lowerCaseLettters = "abcdefghijklmnopqrstuvwxyz";
export const specialCharacters = "!'^+%&/()=?_#$½§{[]}|;:>÷`<.*-@é";


Create the Main component

Next, we will create the Main Component inside we will have the form & the methods that will generate and copy the password to the clipboard.

                                                        
                                                                                                                        
<template>
   <div class="container">
      <div class="row my-5">
         <div class="col-md-8 mx-auto">
            <div class="card">
               <div class="card-body">
                  <div class="card-title text-center">
                     <h3>Password Generator</h3>
                  </div>
                  <hr>
                  <div class="row mt-4">
                     <div class="col-md-6 mx-auto">
                        <div class="form-group d-flex">
                           <input type="text" 
                              v-model="data.password"   
                              class="form-control rounded-0" placeholder="Password">
                           <button @click="copyPassword" class="btn btn-sm btn-dark rounded-0">
                              <i class="bi bi-files"></i>
                           </button>
                        </div>
                        <div class="form-group row mt-3 d-flex align-items-center">
                           <div class="col-md-8">
                              Password Length
                           </div>
                           <div class="col-md-4">
                              <input type="number" 
                                 v-model="data.passwordLength"     
                                 name="length" placeholder="Length" min="6" max="20" class="form-control">
                           </div>
                        </div>
                        <div class="form-check my-2">
                           <input class="form-check-input" 
                              v-model="data.uppercase"  
                              type="checkbox" id="uppercase">
                           <label class="form-check-label" for="uppercase">
                              Include Uppercase
                           </label>
                        </div>
                        <div class="form-check my-2">
                           <input class="form-check-input" 
                              v-model="data.lowercase"  
                              disabled type="checkbox" id="lowercase">
                           <label class="form-check-label" for="lowercase">
                                 Include Lowercase
                           </label>
                        </div>
                        <div class="form-check my-2">
                           <input class="form-check-input" 
                              v-model="data.numbers"  
                              disabled type="checkbox" id="numbers">
                           <label class="form-check-label" for="numbers">
                                 Include Numbers
                           </label>
                        </div>
                        <div class="form-check my-2">
                           <input class="form-check-input" 
                              v-model="data.symbols"  
                              type="checkbox" id="symbols">
                           <label class="form-check-label" for="symbols">
                                 Include Symbols
                           </label>
                        </div>
                        <hr>
                        <div class="d-flex justify-content-center my-2">
                           <button @click="generatePassword" class="btn btn-block btn-dark">
                              Generate Password
                           </button>
                        </div>
                     </div>
                  </div>
               </div>
            </div>
         </div>
      </div>
   </div>
</template>

<script setup>
   import { reactive } from "vue";
   import { upperCaseLetters, lowerCaseLettters, numbers, specialCharacters } from './helpers';

   const data = reactive({
      password: '',
      passwordLength: 20,
      uppercase: false,
      lowercase: true,
      numbers: true,
      symbols: false
   });

   const generatePassword = () => {
      data.password = '';
      let charsList = lowerCaseLettters;
      charsList += numbers;
      if(data.uppercase){
         charsList += upperCaseLetters;
      }
      if(data.symbols){
         charsList += specialCharacters;
      }
      //generate the password
      for(let i = 0; i < data.passwordLength; i++){
         const charIndex = Math.round(Math.random() * charsList.length);
         data.password = data.password + charsList.charAt(charIndex);
      }
   }

   const copyPassword = () => {
      // Copy the password inside the text field
      navigator.clipboard.writeText(data.password);

      // Alert the copied text
      alert("Copied the password: " + data.password);
   }
</script>

<style>

</style>

Update the App component

Next, let's update the App component and import the Main component, now run npm run dev and everything will work as seen in the demo file.

Demo

                                                        
                                                                                                                        
<template>
  <Main />
</template>

<script setup>
import Main from './Main.vue'
</script>

<style>
</style>

Related Tuorials

How to Persist Data in the Pinia Store

In this lesson, we will see how to persist data in the Pinia store.When working with Pinia...


How to Reset the File input Field in Vue js

In this lesson, we will see how to reset the file input field in Vue js.When uploading files using V...


Review App Using Laravel 11 & Vue js 3 Composition API Part 5

In the last part of this tutorial, we will display the reviews list of each product, add the ability...


Review App Using Laravel 11 & Vue js 3 Composition API Part 4

In the fourth part of this tutorial, we will fetch and display all the products on the home page, vi...


Review App Using Laravel 11 & Vue js 3 Composition API Part 3

In the third part of this tutorial, we will start coding the front end, first, we will install the p...


Review App Using Laravel 11 & Vue js 3 Composition API Part 2

In the second part of this tutorial, we will create the product and review controllers, and later we...


Review App Using Laravel 11 & Vue js 3 Composition API Part 1

In this tutorial, we will create a review app using Laravel 11 & Vue js 3 Composition API, the user...


Build a Shopping Cart Using Vue js 3 Composition API Laravel 11 & Stripe Payment Gateway Part 5

In the final part of this tutorial, we will display the cart items, add the ability to increment/dec...


Build a Shopping Cart Using Vue js 3 Composition API Laravel 11 & Stripe Payment Gateway Part 4

In the fourth part of this tutorial, we will fetch and display all the products, and add the store w...


Build a Shopping Cart Using Vue js 3 Composition API Laravel 11 & Stripe Payment Gateway Part 3

In the third part of this tutorial, we will move to the front end, we will install the packages...