Skip to main content
Solved

How can i use localStorage in a plugin?

  • August 27, 2023
  • 4 replies
  • 1939 views

Hello,

I’m developing a plugin based on VueJS 3. I scratched from this project GitHub - joaomarcelofm/figma-plugin-template-vue: A lightweight template for Figma plugins using Vue.js .

I have this code at main.js:

1axios.interceptors.request.use((config) => {
2 const accessToken = localStorage.getItem('accessToken')
3 if (accessToken)
4 config.headers.common['Authorization'] = `Token ${accessToken}`
5 return config
6}, (error) => {
7 return Promise.reject(error)
8});```
9
10How can I save my accessToken? I can't use [figma.clientStorage](https://www.figma.com/plugin-docs/api/figma-clientStorage/) 'cause it is undefined at `main.js` I can't use `parent.localStorage` or `localStorage` 'cause it is prohibited
11
12Best regards

Best answer by tank666

There is nothing complicated here. Just send a message from the UI and receive it in code.ts.

figma.com
View original
This topic has been closed for replies.

4 replies

tank666
  • 4873 replies
  • August 27, 2023

The localeStorage is not available for plugins, so use figma.clientStorage.


  • Author
  • 3 replies
  • August 27, 2023

I understood that I couldn’t use localStorage. Do you have a code example that uses Figma’s storage in a Vue/React app?

Best regards


tank666
  • 4873 replies
  • Answer
  • August 27, 2023

There is nothing complicated here. Just send a message from the UI and receive it in code.ts.

figma.com

  • Author
  • 3 replies
  • September 3, 2023

Many thanks for your reply. I made same solution:

code.ts

1figma.showUI(__html__, {width: 500, height: 650});
2
3figma.ui.onmessage = async (msg) => {
4 if (msg.type === "set-value") {
5 await figma.clientStorage.setAsync(msg.name, msg.value);
6 }
7 else if (msg.type === "get-value") {
8 const value = await figma.clientStorage.getAsync(msg.name);
9 figma.ui.postMessage({type: 'return-value', value: value});
10 }
11};
12

storage-wrapper.js

1export function setFigmaStorageValue(name, value) {
2 window.parent.postMessage({pluginMessage: { type: "set-value", name: name, value: value }}, "*");
3}
4export function getFigmaStorageValue(name) {
5 window.parent.postMessage({pluginMessage: { type: "get-value", name: name }}, "*");
6
7 const promise = new Promise(function(resolve, reject) {
8 window.addEventListener(
9 'message',
10 function(event) {
11 resolve(event.data.pluginMessage.value);
12 },
13 {once: true}
14 );
15 });
16
17 return promise;
18}
19

view.js

1logout() {
2 setFigmaStorageValue('access-token', null);
3 }
4

Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings