How to enable Sign in with Google in Chrome Extension?

Viewed 398

I have already go through this document

https://cloud.google.com/identity-platform/docs/web/chrome-extension

but , i didn't figure out how i can add extension link to the authorized domain .

here is my manifest.json

{
  "manifest_version": 2,
  "name": "AppName",
  "description": "This official AppName Chrome Extension",
  "version": "1.0",
  "browser_action": {
    "default_popup": "index.html",
    "default_title": "AppName"
  },
  "icons": {
    "16": "icon1.png",
    "48": "icon1.png",
    "128": "icon1.png"
  },
  "content_security_policy": "script-src 'self' 'sha256-kFv4LNofhwVLOIwHReYGCRy3S9dD6iHKsyMST3uabnU='; object-src 'self'",
  "permissions": [
    "storage"
  ]
}

I already prefer https://stackoverflow.com/a/44987478/12318562 but this doesn't help me more .

1 Answers

I had a similar problem, Here's how I made it work,

manifest.json

{
  "manifest_version": 2,
  "name": "AppName",
  "description": "This official AppName Chrome Extension",
  "version": "1.0",
  "browser_action": {
    "default_popup": "index.html",
    "default_title": "AppName"
  },
  "icons": {
    "16": "icon1.png",
    "48": "icon1.png",
    "128": "icon1.png"
  },
  "background": {
    "scripts": [
      "./background.js"
    ],
    "persistent": false
  },
  "oauth2": {
    "client_id": "YOUR_CLIENT_ID",
    "scopes": [
       "openid", "email", "profile"
    ]
 },
  "content_security_policy": "script-src 'self' 'sha256-kFv4LNofhwVLOIwHReYGCRy3S9dD6iHKsyMST3uabnU='; object-src 'self'",
  "permissions": [
    "storage",
    "identity",
    "*://*.google.com/*"
  ]
}




background.js

let user_signed_in = false;

const CLIENT_ID = "YOUR_CLIENT_ID"
const RESPONSE_TYPE = encodeURIComponent('token')
const REDIRECT_URI = chrome.identity.getRedirectURL("oauth2");
const STATE = encodeURIComponent('jsksf3')
const SCOPE = encodeURIComponent('openid')
const PROMPT = encodeURIComponent('consent')

function create_oauth() {

  let auth_url = `https://accounts.google.com/o/oauth2/v2/auth?`

  var auth_params = {
    client_id: CLIENT_ID,
    redirect_uri: REDIRECT_URI,
    response_type: 'token',
    scope: "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile openid",
  };

  const url = new URLSearchParams(Object.entries(auth_params));
  url.toString();
  auth_url += url;

  return auth_url;
}

function is_user_signedIn() {
  return user_signed_in;
}

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {

  if (request.message === 'login') {

    if (user_signed_in) {
      return;
    } else {
      chrome.identity.launchWebAuthFlow({
        url: create_oauth(),
        interactive: true
      }, function (redirect_uri) {
        sendResponse('success')
      })
    }
  }
})
Related