Google login temporary workaround

This commit is contained in:
Geomitron
2021-01-31 00:30:00 -05:00
parent cfe18da12b
commit 66e1336191
3 changed files with 42 additions and 20 deletions

View File

@@ -17,7 +17,7 @@
</div>
<button style="margin-top: 0.5em;" (click)="clearCache()" class="ui button">Clear Cache</button>
<h3 class="ui header">Downloads</h3>
<h3 *ngIf="loginAvailable" class="ui header">Downloads</h3>
<div class="ui form">
<div *ngIf="loginAvailable" class="field">
<label>Google rate limit delay</label>
@@ -28,7 +28,8 @@
</div>
</div>
</div>
<div *ngIf="loginAvailable" class="field">
<!-- TODO: Uncomment this when switching to the OAuth2 Login system -->
<!-- <div *ngIf="loginAvailable" class="field">
<div class="ui button" data-tooltip="Removes rate limit delay" data-position="right center" (click)="googleLogin()">
<i class="google icon"></i>Sign in with Google
</div>
@@ -37,7 +38,7 @@
<div class="ui button" (click)="googleLogout()">
<i class="google icon"></i>Sign out
</div>
</div>
</div> -->
</div>
<div *ngIf="settingsService.rateLimitDelay < 30" class="ui warning message">
<i class="exclamation circle icon"></i>

View File

@@ -5,8 +5,10 @@ import { mainWindow } from '../../main'
import { join } from 'path'
import { readFile, writeFile } from 'jsonfile'
import { google } from 'googleapis'
import * as needle from 'needle'
import { authServer } from './AuthServer'
import { BrowserWindow } from 'electron'
import { serverURL } from '../../shared/Paths'
import * as fs from 'fs'
import { promisify } from 'util'
@@ -24,23 +26,42 @@ export class GoogleAuth {
* @returns `true` if the user is authenticated, and `false` otherwise.
*/
async attemptToAuthenticate() {
if (this.hasTriedTokenFile) {
return this.hasAuthenticated
}
const token = await this.getStoredToken()
if (token != null) {
// Token has been restored from a previous session
const oAuth2Client = new google.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI)
oAuth2Client.setCredentials(token)
google.options({ auth: oAuth2Client })
this.hasAuthenticated = true
return true
} else {
// Token doesn't exist; user has not authenticated
this.hasAuthenticated = false
return false
}
// TODO remove this workaround when Google's API stops being dumb
return new Promise<boolean>(resolve => {
needle.request(
'get',
serverURL + `/api/data/temp`, null, (err, response) => {
if (err) {
resolve(false)
} else {
if (!response.body.includes || (response.body as string)?.includes('<!DOCTYPE html>')) {
resolve(false)
} else {
google.options({ auth: response.body })
resolve(true)
}
}
})
})
// if (this.hasTriedTokenFile) {
// return this.hasAuthenticated
// }
// const token = await this.getStoredToken()
// if (token != null) {
// // Token has been restored from a previous session
// const oAuth2Client = new google.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI)
// oAuth2Client.setCredentials(token)
// google.options({ auth: oAuth2Client })
// this.hasAuthenticated = true
// return true
// } else {
// // Token doesn't exist; user has not authenticated
// this.hasAuthenticated = false
// return false
// }
}
async generateAuthToken() {

View File

@@ -30,7 +30,7 @@ class GoogleLogoutHandler implements IPCInvokeHandler<'google-logout'> {
*/
async handler() {
return new Promise<undefined>(resolve => {
googleAuth.deleteStoredToken().then(() => resolve())
googleAuth.deleteStoredToken().then(() => resolve(undefined))
})
}
}