User authentication in browser extension

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:
Development stages
Latest works
  • image_website-b2b-advance_0.png
    B2B ADVANCE company website development
    1212
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_websites_belfingroup_462_0.webp
    Website development for BELFINGROUP
    852
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_bitrix-bitrix-24-1c_fixper_448_0.png
    Website development for FIXPER company
    815

Implementing User Authorization in Browser Extension

Authorization in an extension is technically more complex than in a regular web app: no built-in sessions, cannot use httpOnly cookies, tokens must be stored in chrome.storage, and OAuth2 flow requires special handling.

OAuth2 via chrome.identity API

// manifest.json
{
  "permissions": ["identity", "storage"],
  "oauth2": {
    "client_id": "YOUR_GOOGLE_CLIENT_ID",
    "scopes": ["openid", "email", "profile"]
  }
}

// Authorize via Google OAuth2
async function authenticateWithGoogle() {
  return new Promise((resolve, reject) => {
    chrome.identity.getAuthToken({ interactive: true }, async (token) => {
      if (chrome.runtime.lastError) {
        reject(chrome.runtime.lastError);
        return;
      }

      // Exchange Google token for our app token
      const resp = await fetch('https://api.example.com/v1/auth/google', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ google_token: token }),
      });

      const { access_token, refresh_token } = await resp.json();

      await chrome.storage.local.set({
        access_token,
        refresh_token,
        token_expiry: Date.now() + 3600 * 1000,
      });

      resolve({ access_token });
    });
  });
}

Authorization via Own Service (email/password)

// Open authorization page in new tab
async function loginWithCredentials() {
  const loginUrl = `https://app.example.com/extension-login?` +
    `redirect_uri=${encodeURIComponent('https://app.example.com/extension-callback')}`;

  // Open login page
  chrome.tabs.create({ url: loginUrl });

  // Listen for message from page after authorization
  return new Promise((resolve) => {
    const listener = (message) => {
      if (message.type === 'AUTH_SUCCESS') {
        chrome.runtime.onMessage.removeListener(listener);
        storeTokens(message.tokens);
        resolve(message.tokens);
      }
    };
    chrome.runtime.onMessage.addListener(listener);
  });
}

// On /extension-callback page after authorization:
// window.postMessage('extension-auth', ...) or chrome.runtime.sendMessage

Token Storage and Refresh

class TokenManager {
  async getValidToken() {
    const stored = await chrome.storage.local.get(['access_token', 'refresh_token', 'token_expiry']);

    if (stored.access_token && stored.token_expiry > Date.now() + 60000) {
      return stored.access_token;
    }

    // Need to refresh token
    if (stored.refresh_token) {
      return this.refreshToken(stored.refresh_token);
    }

    throw new Error('Not authenticated');
  }

  async refreshToken(refreshToken) {
    const resp = await fetch('https://api.example.com/v1/auth/refresh', {
      method: 'POST',
      body: JSON.stringify({ refresh_token: refreshToken }),
    });

    const tokens = await resp.json();
    await chrome.storage.local.set({
      access_token:  tokens.access_token,
      refresh_token: tokens.refresh_token,
      token_expiry:  Date.now() + tokens.expires_in * 1000,
    });

    return tokens.access_token;
  }

  async logout() {
    await chrome.storage.local.remove(['access_token', 'refresh_token', 'token_expiry']);
    chrome.identity.clearAllCachedAuthTokens(() => {});
  }
}

Popup UI for Authorization

// popup.tsx
import { useState, useEffect } from 'react';

export function Popup() {
  const [user, setUser] = useState<User | null>(null);

  useEffect(() => {
    chrome.storage.local.get(['access_token'], async ({ access_token }) => {
      if (access_token) {
        const profile = await fetchUserProfile(access_token);
        setUser(profile);
      }
    });
  }, []);

  if (!user) {
    return (
      <div className="p-4 w-72">
        <h1 className="text-lg font-bold mb-4">Sign In</h1>
        <button
          onClick={() => chrome.runtime.sendMessage({ action: 'login' })}
          className="btn-primary w-full"
        >
          Sign in with Google
        </button>
      </div>
    );
  }

  return (
    <div className="p-4 w-72">
      <div className="flex items-center gap-3">
        <img src={user.avatar} className="w-8 h-8 rounded-full" />
        <span>{user.name}</span>
      </div>
    </div>
  );
}

Timeline

Authorization in extension with OAuth2 and refresh tokens: 3–5 working days.