1- import { invoke , shell } from "@tauri-apps/api" ;
1+ import { shell } from "@tauri-apps/api" ;
22import axios from "axios" ;
33import { AccessToken , UserProfile } from "../types/googleapis" ;
44import { readTextFile , removeFile , writeTextFile , exists } from "@tauri-apps/api/fs" ;
55import { CLIENT_ID , CLIENT_SECRET } from "../config/credentials" ;
66import settings from "../config/settings" ;
7+ import { generate_oauth_port , get_access_token , get_auth_code , save_access_token , save_auth_code } from "./invoker" ;
78
89
910const DEFAULT_DIRECTORY = settings . fs . DEFAULT_DIRECTORY ;
@@ -18,7 +19,7 @@ class PortManager {
1819 return this . port ;
1920 }
2021 async generatePort ( ) : Promise < number > {
21- const port = await invoke ( "plugin:oauth|start" ) ;
22+ const port = await generate_oauth_port ( ) ;
2223 return port as number ;
2324 }
2425}
@@ -124,7 +125,7 @@ export async function openAuthWindow() {
124125// save the auth code to storage
125126export async function saveAuthCode ( code : string ) {
126127 try {
127- return await writeTextFile ( STORAGE_PATHS . authcode , code , { dir : DEFAULT_DIRECTORY } ) ;
128+ return await save_auth_code ( code ) ;
128129 } catch ( error ) {
129130 console . error ( "Error saving auth code:" , error ) ;
130131 throw new Error ( "Error saving auth code" ) ;
@@ -134,7 +135,7 @@ export async function saveAuthCode(code: string) {
134135// get the auth code from storage
135136export async function getAuthCode ( ) {
136137 try {
137- return await readTextFile ( STORAGE_PATHS . authcode , { dir : DEFAULT_DIRECTORY } ) ;
138+ return await get_auth_code ( ) ;
138139 } catch ( error ) {
139140 console . error ( "Error getting auth code:" , error ) ;
140141 return null ;
@@ -143,10 +144,8 @@ export async function getAuthCode() {
143144
144145export async function saveAccessToken ( accessToken : string | AccessToken ) {
145146 try {
146- const accessTokenText : string = typeof accessToken === "string" ? accessToken : JSON . stringify ( accessToken , null , 2 ) ;
147- console . log ( JSON . parse ( accessTokenText ) , "accessTokenText that was saved" ) ;
148147 localStorage . setItem ( "lastLogin" , Date . now ( ) + "" ) ;
149- return await invoke ( " save_access_token" , { token : accessTokenText } ) ;
148+ return await save_access_token ( accessToken ) ;
150149 } catch ( error ) {
151150 console . error ( "Error saving access token:" , error ) ;
152151 localStorage . removeItem ( "lastLogin" ) ;
@@ -160,7 +159,7 @@ export async function getAccessTokenFromStorage() {
160159 try {
161160 // if file does not exist, return null
162161 if ( ! await exists ( STORAGE_PATHS . access_token , { dir : DEFAULT_DIRECTORY } ) ) throw new Error ( "File does not exist" ) ;
163- const accessTokenText : string = await invoke ( 'load_access_token' )
162+ const accessTokenText : string = ( await get_access_token ( ) ) as string ;
164163 console . log ( 'new access token found' )
165164 let accessToken = JSON . parse ( accessTokenText ) as AccessToken ;
166165 // check if the access token is expired
0 commit comments