Refactor GoogleService: streamline token handling and improve comments
This commit is contained in:
parent
4a37d5befb
commit
bfe489d920
@ -1,6 +1,5 @@
|
|||||||
import { BadRequestException, Injectable, Logger } from "@nestjs/common";
|
import { BadRequestException, Injectable, Logger } from "@nestjs/common";
|
||||||
import { google } from "googleapis";
|
import { google } from "googleapis";
|
||||||
import { Credentials } from "google-auth-library";
|
|
||||||
import { PrismaService } from "../prisma/prisma.service";
|
import { PrismaService } from "../prisma/prisma.service";
|
||||||
|
|
||||||
const SCOPES = [
|
const SCOPES = [
|
||||||
@ -31,19 +30,18 @@ export class GoogleService {
|
|||||||
const authUrl = client.generateAuthUrl({
|
const authUrl = client.generateAuthUrl({
|
||||||
access_type: "offline",
|
access_type: "offline",
|
||||||
scope: SCOPES,
|
scope: SCOPES,
|
||||||
prompt: "consent",
|
prompt: "consent", // always request a refresh_token
|
||||||
state: userId,
|
state: userId, // passed back in callback to identify the user
|
||||||
});
|
});
|
||||||
return { authUrl };
|
return { authUrl };
|
||||||
}
|
}
|
||||||
|
|
||||||
async exchangeCode(userId: string, code: string) {
|
async exchangeCode(userId: string, code: string) {
|
||||||
const client = this.createClient();
|
const client = this.createClient();
|
||||||
let tokens: Credentials;
|
let tokens: import("google-auth-library").Credentials;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const { tokens: t } = await client.getToken(code);
|
const result = await client.getToken(code);
|
||||||
tokens = t;
|
tokens = result.tokens;
|
||||||
} catch {
|
} catch {
|
||||||
throw new BadRequestException("Invalid or expired authorization code.");
|
throw new BadRequestException("Invalid or expired authorization code.");
|
||||||
}
|
}
|
||||||
@ -54,6 +52,7 @@ export class GoogleService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Fetch the Google account email
|
||||||
client.setCredentials(tokens);
|
client.setCredentials(tokens);
|
||||||
const oauth2 = google.oauth2({ version: "v2", auth: client });
|
const oauth2 = google.oauth2({ version: "v2", auth: client });
|
||||||
const { data } = await oauth2.userinfo.get();
|
const { data } = await oauth2.userinfo.get();
|
||||||
@ -67,7 +66,7 @@ export class GoogleService {
|
|||||||
accessToken: tokens.access_token ?? null,
|
accessToken: tokens.access_token ?? null,
|
||||||
isConnected: true,
|
isConnected: true,
|
||||||
connectedAt: new Date(),
|
connectedAt: new Date(),
|
||||||
spreadsheetId: null,
|
spreadsheetId: null, // reset so a new spreadsheet is created on next export
|
||||||
},
|
},
|
||||||
create: {
|
create: {
|
||||||
userId,
|
userId,
|
||||||
@ -92,4 +91,4 @@ export class GoogleService {
|
|||||||
if (!gc || !gc.isConnected) return { connected: false };
|
if (!gc || !gc.isConnected) return { connected: false };
|
||||||
return { connected: true, googleEmail: gc.googleEmail, connectedAt: gc.connectedAt };
|
return { connected: true, googleEmail: gc.googleEmail, connectedAt: gc.connectedAt };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user