@@ -5,6 +5,7 @@ import { memo, useEffect } from 'react'
55import { UserContext } from '../../../../../shared-ui/index.js'
66import { requestGoogleDriveAccessToken } from '../helpers.js'
77import { t } from '@lingui/core/macro'
8+ import Services from '#services'
89
910const useStyles = makeStyles ( ) ( ( theme ) => ( {
1011 container : {
@@ -30,15 +31,15 @@ interface Props extends BoxProps {}
3031const clientId =
3132 process . env . GOOGLE_CLIENT_ID || '18954568633-c7has4fcrm5b7fop5si83fleb51oodji.apps.googleusercontent.com'
3233// cspell:enable
33- const SCOPE = 'https://www.googleapis.com/auth/drive.file'
34+ const SCOPES = [ 'https://www.googleapis.com/auth/drive.file' , 'https://www.googleapis.com/auth/userinfo.email' ]
3435const redirectUri = browser . runtime . getURL ( 'oauth2.html' )
3536
3637const handleSignIn = ( ) => {
37- const authUrl = `https://accounts.google.com/o/oauth2/v2/auth?client_id=${ clientId } &redirect_uri=${ encodeURIComponent ( redirectUri ) } &response_type=code&scope=${ encodeURIComponent ( SCOPE ) } &access_type=offline`
38+ const authUrl = `https://accounts.google.com/o/oauth2/v2/auth?client_id=${ clientId } &redirect_uri=${ encodeURIComponent ( redirectUri ) } &response_type=code&scope=${ encodeURIComponent ( SCOPES . join ( ' ' ) ) } &access_type=offline`
3839 window . location . assign ( authUrl )
3940}
40- export const GoogleDriveBackup = memo < Props > ( function GoogleDriveBackup ( props ) {
41- const { classes, cx } = useStyles ( )
41+ export const Component = memo < Props > ( function GoogleDriveBackup ( ) {
42+ const { classes } = useStyles ( )
4243 const { user, updateUser } = UserContext . useContainer ( )
4344 const { showSnackbar } = useCustomSnackbar ( )
4445
@@ -54,7 +55,6 @@ export const GoogleDriveBackup = memo<Props>(function GoogleDriveBackup(props) {
5455 redirectUri,
5556 } )
5657 if ( res . access_token ) {
57- debugger
5858 // TODO account
5959 updateUser ( ( user ) => ( {
6060 ...user ,
@@ -70,9 +70,22 @@ export const GoogleDriveBackup = memo<Props>(function GoogleDriveBackup(props) {
7070 return ( ) => controller . abort ( )
7171 } , [ ] )
7272
73+ const login = async ( ) => {
74+ // const token = await browser.identity.launchWebAuthFlow({
75+ // url: `https://accounts.google.com/o/oauth2/v2/auth?client_id=${clientId}&redirect_uri=${encodeURIComponent(redirectUri)}&response_type=code&scope=${encodeURIComponent(SCOPES.join(' '))}&access_type=offline`,
76+ // interactive: true,
77+ // })
78+ const token = await Services . Backup . getAccessToken ( )
79+ if ( ! token ) return
80+ updateUser ( ( user ) => ( {
81+ ...user ,
82+ googleAccessToken : token ,
83+ } ) )
84+ }
85+
7386 if ( ! user . googleAccount ) {
7487 return (
75- < Box { ... props } className = { cx ( classes . container , props . className ) } >
88+ < Box className = { classes . container } >
7689 < Typography className = { classes . title } >
7790 < Trans > Add google Drive</ Trans >
7891 </ Typography >
@@ -82,15 +95,15 @@ export const GoogleDriveBackup = memo<Props>(function GoogleDriveBackup(props) {
8295 </ Trans >
8396 </ Typography >
8497 < Box display = "flex" justifyContent = "center" mt = "48px" >
85- < Button variant = "contained" onClick = { handleSignIn } >
98+ < Button variant = "contained" onClick = { login } >
8699 Add Google Drive
87100 </ Button >
88101 </ Box >
89102 </ Box >
90103 )
91104 }
92105 return (
93- < Box { ... props } >
106+ < Box >
94107 < Box >
95108 < Typography > test@gmail.com</ Typography >
96109 < Button variant = "text" > Switch other accounts</ Button >
0 commit comments