Permissions API & Why should you start using it?

Hey guys ?

In this article I will explain the usage, importance and use cases of Permissions API and why is it so important for user experience.

Let’s get started ?

Table of content

Introduction
Where can we use Per…


This content originally appeared on DEV Community and was authored by Mohd Shahid

Hey guys ?

In this article I will explain the usage, importance and use cases of Permissions API and why is it so important for user experience.

Let's get started ?

Table of content

Introduction ?

The Permissions API can be used to determine if permission to access a particular API has been granted ✅ or denied ❌.

The Permissions API provides the tools to allow us developers to implement a better user experience as far as permissions are concerned.

Which is vital to increase the accessibility and usability of the application.

Permissions API does not work in Safari and IE.

Where can we use permissions API ??

Accessing the permissions API ??

permissions object is available on Navigator.permissions property ?

const permissions = navigator.permissions;

For checking the status of permission of a certain API .query() method is used which is available on permissions object.

.query() accepts a parameter say permissionDescriptor which is an object defining on which API query should fire on.

.query() will return a promise which will resolve to PermissionStatus object which will tell us about the status of the certain permission.

Enogh talk ?, let's see it in action ?

-

1️⃣ Using Permissions API for checking Clipboard permissions:

const permissions = navigator.permissions;

permissions.query({name: 'clipboard-write'}).then(cbWritePermission => {
console.log(cbWritePermission.state) // "granted"

// clipboard-write permission is granted by the browser automatically
});

In the above example the state property will tell us about the status of the permission. It can have following values:

granted: Permission is granted.

denied: Permission is denied.

prompt: Permission is not yet asked.

Let's see an example of clipboard-read permission ?

// suppose a scnerio where you want to do 
// something with the text before it get pasted by the user 
// in your application. 
// or you want to auto-capture copied text
// checking the item before doing anything
// with it is also good for security.

// reading text on a click
button.addEventListener("click", async () => {
  const cbReadPermissionStatus = await permissions.query({
    name: "clipboard-read",
  });
  if (
    cbReadPermissionStatus.state === "granted" ||
    cbReadPermissionStatus.state === "prompt"
  ) {
    const text = await navigator.clipboard.readText();
    // do something with the text
  } else {
    // show a nice error message, or ask for the permission nicely
  }
});

2️⃣ Checking geolocation permissions using Permissions API:

  • building an application where the current location of the user is important for a feature to work in your application? Then this one is for you.

See the example below ?

button.addEventListener("click", async () => {
  const geoPermissionStatus = await permissions.query({ name: "geolocation" });

  if (
    geoPermissionStatus.state === "granted" ||
    geoPermissionStatus.state === "prompt"
  ) {
    navigator.geolocation.getCurrentPosition((pos) => {
      console.log(pos);
      // do something with the location
      // show friends around the user for eg
      // or move the map to the user's location
    });
  } else {
    // show warning that certain feature of the app
    // will not work if the location permission is not provided
  }
});

Others APIs permissions can be checked using the same procedure as defined above.

Thank you for giving it a read. ❤️

Please follow me on twitter

Happy Coding


This content originally appeared on DEV Community and was authored by Mohd Shahid


Print Share Comment Cite Upload Translate Updates
APA

Mohd Shahid | Sciencx (2021-09-01T08:53:56+00:00) Permissions API & Why should you start using it?. Retrieved from https://www.scien.cx/2021/09/01/permissions-api-why-should-you-start-using-it/

MLA
" » Permissions API & Why should you start using it?." Mohd Shahid | Sciencx - Wednesday September 1, 2021, https://www.scien.cx/2021/09/01/permissions-api-why-should-you-start-using-it/
HARVARD
Mohd Shahid | Sciencx Wednesday September 1, 2021 » Permissions API & Why should you start using it?., viewed ,<https://www.scien.cx/2021/09/01/permissions-api-why-should-you-start-using-it/>
VANCOUVER
Mohd Shahid | Sciencx - » Permissions API & Why should you start using it?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/01/permissions-api-why-should-you-start-using-it/
CHICAGO
" » Permissions API & Why should you start using it?." Mohd Shahid | Sciencx - Accessed . https://www.scien.cx/2021/09/01/permissions-api-why-should-you-start-using-it/
IEEE
" » Permissions API & Why should you start using it?." Mohd Shahid | Sciencx [Online]. Available: https://www.scien.cx/2021/09/01/permissions-api-why-should-you-start-using-it/. [Accessed: ]
rf:citation
» Permissions API & Why should you start using it? | Mohd Shahid | Sciencx | https://www.scien.cx/2021/09/01/permissions-api-why-should-you-start-using-it/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.