Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/mask/background/services/backup/google_drive.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { checkAndRequestPermission } from '../../../shared/helpers/index.js'

/* eslint-disable @typescript-eslint/ban-ts-comment */
export async function getAccessToken(interactive = false) {
const contained = await browser.permissions.contains({ permissions: ['identity'] })
if (!contained) {
const granted = await browser.permissions.request({ origins: ['identity'] })
if (!granted) return
}
const granted = await checkAndRequestPermission()
if (!granted) return

return new Promise<string>((resolve, reject) => {
// @ts-expect-error
chrome.identity.getAuthToken({ interactive }, (token, error) => {
Expand Down
6 changes: 5 additions & 1 deletion packages/mask/dashboard/components/GoogleDriveLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { ActionButton, makeStyles, useCustomSnackbar } from '@masknet/theme'
import { GoogleDriveClient } from '@masknet/web3-providers'
import { Box, Typography } from '@mui/material'
import { memo, useMemo } from 'react'
import { useAsyncFn } from 'react-use'
import { UserContext } from '../../shared-ui/index.js'
import { checkAndRequestPermission } from '../../shared/helpers/index.js'
import { clearGoogleDriveAccessToken, getGoogleDriveAccessToken } from '../utils/api.js'
import { useAsyncFn } from 'react-use'

const useStyles = makeStyles()((theme) => ({
container: {
Expand Down Expand Up @@ -38,6 +39,9 @@ export const GoogleDriveLogin = memo(function GoogleDriveLogin() {

const [{ loading }, login] = useAsyncFn(async () => {
try {
const granted = await checkAndRequestPermission()
if (!granted) return

const userInfo = await googleDriveClient.login(true)
updateUser({
googleAccount: userInfo.email || '',
Expand Down
10 changes: 10 additions & 0 deletions packages/mask/shared/helpers/checkAndRequestIdentityPermission.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { Manifest } from 'webextension-polyfill'

export async function checkAndRequestPermission() {
const contained = await browser.permissions.contains({ permissions: ['identity'] })

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this helper should check for the environment like requestPermissionFromExtensionPage did:

    if (!isEnvironment(Environment.ExtensionProtocol) || isEnvironment(Environment.ManifestBackground)) {
        // The User Activation limitation is from Firefox
        throw new Error(
            'browser.permissions.request can only be called after a User Activation and from a chrome-extension:// protocol.',
        )
    }

if (!contained) {
const granted = await browser.permissions.request({ permissions: ['identity' as Manifest.OptionalPermission] })
if (!granted) return
}
return true
}
1 change: 1 addition & 0 deletions packages/mask/shared/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './download.js'
export * from './formatTokenBalance.js'
export * from './checkAndRequestIdentityPermission.js'