First stage of the new flow
This commit is contained in:
80
src/lib/gmail.ts
Normal file
80
src/lib/gmail.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import { google } from 'googleapis';
|
||||
import quotedPrintable from 'quoted-printable';
|
||||
import { getAuthenticatedClient } from './google-server.js';
|
||||
|
||||
export function createEmailTemplate(text: string): string {
|
||||
return `<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<style>
|
||||
@import url('https://fonts.googleapis.com/css2?family=Lato&display=swap');
|
||||
</style>
|
||||
</head>
|
||||
<body style="font-family: 'Lato', sans-serif; background-color: #f9f9f9; padding: 20px; margin: 0;">
|
||||
<div style="max-width: 600px; margin: auto; background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.05);">
|
||||
<p style="white-space: pre-line;font-size: 16px; line-height: 1.5; color: #333;">${text}</p>
|
||||
<img src="cid:qrCode1" alt="QR Code" style="display: block; margin: 20px auto; max-width: 50%; min-width: 200px; height: auto;" />
|
||||
<div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between">
|
||||
<div style="height: 4px; width: 20%; background: #00aeef;"></div>
|
||||
<div style="height: 4px; width: 20%; background: #ec008c;"></div>
|
||||
<div style="height: 4px; width: 20%; background: #7ac143;"></div>
|
||||
<div style="height: 4px; width: 20%; background: #f47b20;"></div>
|
||||
<div style="height: 4px; width: 20%; background: #2e3192;"></div>
|
||||
</div>
|
||||
<div style="font-size: 12px; color: #999; padding-top: 0px; margin-top: 10px; line-height: 1.5; ">
|
||||
<p>This email has been generated with the help of ScanWave</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
|
||||
export async function sendGmail(
|
||||
refreshToken: string,
|
||||
{ to, subject, text, qr_code }: { to: string; subject: string; text: string; qr_code: string }
|
||||
) {
|
||||
const oauth = getAuthenticatedClient(refreshToken);
|
||||
const gmail = google.gmail({ version: 'v1', auth: oauth });
|
||||
|
||||
const message_html = createEmailTemplate(text);
|
||||
const boundary = 'BOUNDARY';
|
||||
const nl = '\r\n';
|
||||
|
||||
// Convert HTML to a Buffer, then to latin1 string for quotedPrintable.encode
|
||||
const htmlBuffer = Buffer.from(message_html, 'utf8');
|
||||
const htmlLatin1 = htmlBuffer.toString('latin1');
|
||||
const htmlQP = quotedPrintable.encode(htmlLatin1);
|
||||
const qrLines = qr_code.replace(/.{1,76}/g, '$&' + nl);
|
||||
|
||||
const rawParts = [
|
||||
'MIME-Version: 1.0',
|
||||
`To: ${to}`,
|
||||
`Subject: ${subject}`,
|
||||
`Content-Type: multipart/related; boundary="${boundary}"`,
|
||||
'',
|
||||
`--${boundary}`,
|
||||
'Content-Type: text/html; charset="UTF-8"',
|
||||
'Content-Transfer-Encoding: quoted-printable',
|
||||
'',
|
||||
htmlQP,
|
||||
'',
|
||||
`--${boundary}`,
|
||||
'Content-Type: image/png',
|
||||
'Content-Transfer-Encoding: base64',
|
||||
'Content-ID: <qrCode1>',
|
||||
'Content-Disposition: inline; filename="qr.png"',
|
||||
'',
|
||||
qrLines,
|
||||
'',
|
||||
`--${boundary}--`,
|
||||
''
|
||||
];
|
||||
|
||||
const rawMessage = rawParts.join(nl);
|
||||
const raw = Buffer.from(rawMessage).toString('base64url');
|
||||
|
||||
await gmail.users.messages.send({
|
||||
userId: 'me',
|
||||
requestBody: { raw }
|
||||
});
|
||||
}
|
||||
38
src/lib/google-server.ts
Normal file
38
src/lib/google-server.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { google } from 'googleapis';
|
||||
import { env } from '$env/dynamic/private';
|
||||
|
||||
export const scopes = [
|
||||
'https://www.googleapis.com/auth/gmail.send',
|
||||
'https://www.googleapis.com/auth/userinfo.email',
|
||||
'https://www.googleapis.com/auth/drive.readonly',
|
||||
'https://www.googleapis.com/auth/spreadsheets.readonly'
|
||||
];
|
||||
|
||||
export function getOAuthClient() {
|
||||
return new google.auth.OAuth2(
|
||||
env.GOOGLE_CLIENT_ID,
|
||||
env.GOOGLE_CLIENT_SECRET,
|
||||
env.GOOGLE_REDIRECT_URI
|
||||
);
|
||||
}
|
||||
|
||||
export function createAuthUrl() {
|
||||
return getOAuthClient().generateAuthUrl({
|
||||
access_type: 'offline',
|
||||
prompt: 'consent',
|
||||
scope: scopes,
|
||||
redirect_uri: env.GOOGLE_REDIRECT_URI
|
||||
});
|
||||
}
|
||||
|
||||
export async function exchangeCodeForTokens(code: string) {
|
||||
const { tokens } = await getOAuthClient().getToken(code);
|
||||
if (!tokens.refresh_token) throw new Error('No refresh_token returned');
|
||||
return tokens.refresh_token;
|
||||
}
|
||||
|
||||
export function getAuthenticatedClient(refreshToken: string) {
|
||||
const oauth = getOAuthClient();
|
||||
oauth.setCredentials({ refresh_token: refreshToken });
|
||||
return oauth;
|
||||
}
|
||||
@@ -1,108 +1,60 @@
|
||||
import { google } from 'googleapis';
|
||||
import { env } from '$env/dynamic/private';
|
||||
import quotedPrintable from 'quoted-printable'; // tiny, zero-dep package
|
||||
import { browser } from '$app/environment';
|
||||
|
||||
// Client-side only functions
|
||||
export const scopes = [
|
||||
'https://www.googleapis.com/auth/gmail.send',
|
||||
'https://www.googleapis.com/auth/userinfo.email'
|
||||
'https://www.googleapis.com/auth/userinfo.email',
|
||||
'https://www.googleapis.com/auth/drive.readonly',
|
||||
'https://www.googleapis.com/auth/spreadsheets.readonly'
|
||||
];
|
||||
|
||||
export function getOAuthClient() {
|
||||
return new google.auth.OAuth2(
|
||||
env.GOOGLE_CLIENT_ID,
|
||||
env.GOOGLE_CLIENT_SECRET,
|
||||
env.GOOGLE_REDIRECT_URI
|
||||
);
|
||||
// Client-side functions for browser environment
|
||||
export async function initGoogleAuth(): Promise<void> {
|
||||
if (!browser) return;
|
||||
// Google Auth initialization is handled by the OAuth flow
|
||||
// No initialization needed for our server-side approach
|
||||
}
|
||||
|
||||
export function createAuthUrl() {
|
||||
return getOAuthClient().generateAuthUrl({
|
||||
access_type: 'offline',
|
||||
prompt: 'consent',
|
||||
scope: scopes
|
||||
});
|
||||
export function getAuthUrl(): string {
|
||||
if (!browser) return '';
|
||||
// This should be obtained from the server
|
||||
return '/auth/google';
|
||||
}
|
||||
|
||||
export async function exchangeCodeForTokens(code: string) {
|
||||
const { tokens } = await getOAuthClient().getToken(code);
|
||||
if (!tokens.refresh_token) throw new Error('No refresh_token returned');
|
||||
return tokens.refresh_token;
|
||||
export async function isTokenValid(accessToken: string): Promise<boolean> {
|
||||
if (!browser) return false;
|
||||
|
||||
try {
|
||||
const response = await fetch(`https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=${accessToken}`);
|
||||
const data = await response.json();
|
||||
|
||||
if (response.ok && data.expires_in && data.expires_in > 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (error) {
|
||||
console.error('Error validating token:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export async function sendGmail(
|
||||
refreshToken: string,
|
||||
{ to, subject, text, qr_code }: { to: string; subject: string; text: string; qr_code: string }
|
||||
) {
|
||||
const oauth = getOAuthClient();
|
||||
oauth.setCredentials({ refresh_token: refreshToken });
|
||||
|
||||
const gmail = google.gmail({ version: 'v1', auth: oauth });
|
||||
|
||||
const message_html =
|
||||
`<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<style>
|
||||
@import url('https://fonts.googleapis.com/css2?family=Lato&display=swap');
|
||||
</style>
|
||||
</head>
|
||||
<body style="font-family: 'Lato', sans-serif; background-color: #f9f9f9; padding: 20px; margin: 0;">
|
||||
<div style="max-width: 600px; margin: auto; background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.05);">
|
||||
<p style="white-space: pre-line;font-size: 16px; line-height: 1.5; color: #333;">${text}</p>
|
||||
<img src="cid:qrCode1" alt="QR Code" style="display: block; margin: 20px auto; max-width: 50%; min-width: 200px; height: auto;" />
|
||||
<div style="width: 100%; display: flex; flex-direction: row; justify-content: space-between">
|
||||
<div style="height: 4px; width: 20%; background: #00aeef;"></div>
|
||||
<div style="height: 4px; width: 20%; background: #ec008c;"></div>
|
||||
<div style="height: 4px; width: 20%; background: #7ac143;"></div>
|
||||
<div style="height: 4px; width: 20%; background: #f47b20;"></div>
|
||||
<div style="height: 4px; width: 20%; background: #2e3192;"></div>
|
||||
</div>
|
||||
<div style="font-size: 12px; color: #999; padding-top: 0px; margin-top: 10px; line-height: 1.5; ">
|
||||
<p>This email has been generated with the help of ScanWave</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>`;
|
||||
|
||||
const boundary = 'BOUNDARY';
|
||||
const nl = '\r\n'; // RFC-5322 line ending
|
||||
|
||||
// Convert HTML to a Buffer, then to latin1 string for quotedPrintable.encode
|
||||
const htmlBuffer = Buffer.from(message_html, 'utf8');
|
||||
const htmlLatin1 = htmlBuffer.toString('latin1');
|
||||
const htmlQP = quotedPrintable.encode(htmlLatin1);
|
||||
const qrLines = qr_code.replace(/.{1,76}/g, '$&' + nl);
|
||||
|
||||
const rawParts = [
|
||||
'MIME-Version: 1.0',
|
||||
`To: ${to}`,
|
||||
`Subject: ${subject}`,
|
||||
`Content-Type: multipart/related; boundary="${boundary}"`,
|
||||
'',
|
||||
`--${boundary}`,
|
||||
'Content-Type: text/html; charset="UTF-8"',
|
||||
'Content-Transfer-Encoding: quoted-printable',
|
||||
'',
|
||||
htmlQP,
|
||||
'',
|
||||
`--${boundary}`,
|
||||
'Content-Type: image/png',
|
||||
'Content-Transfer-Encoding: base64',
|
||||
'Content-ID: <qrCode1>',
|
||||
'Content-Disposition: inline; filename="qr.png"',
|
||||
'',
|
||||
qrLines,
|
||||
'',
|
||||
`--${boundary}--`,
|
||||
''
|
||||
];
|
||||
|
||||
const rawMessage = rawParts.join(nl);
|
||||
|
||||
const raw = Buffer.from(rawMessage).toString('base64url');
|
||||
|
||||
await gmail.users.messages.send({
|
||||
userId: 'me',
|
||||
requestBody: { raw }
|
||||
});
|
||||
export async function refreshAccessToken(refreshToken: string): Promise<string | null> {
|
||||
try {
|
||||
const response = await fetch('/api/auth/refresh', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ refreshToken })
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
return data.accessToken;
|
||||
}
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.error('Error refreshing token:', error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
58
src/lib/sheets.ts
Normal file
58
src/lib/sheets.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { google } from 'googleapis';
|
||||
import { getAuthenticatedClient } from './google-server.js';
|
||||
|
||||
export interface GoogleSheet {
|
||||
id: string;
|
||||
name: string;
|
||||
modifiedTime: string;
|
||||
webViewLink: string;
|
||||
}
|
||||
|
||||
export interface SheetData {
|
||||
values: string[][];
|
||||
}
|
||||
|
||||
export async function getRecentSpreadsheets(refreshToken: string, limit: number = 10): Promise<GoogleSheet[]> {
|
||||
const oauth = getAuthenticatedClient(refreshToken);
|
||||
const drive = google.drive({ version: 'v3', auth: oauth });
|
||||
|
||||
const response = await drive.files.list({
|
||||
q: "mimeType='application/vnd.google-apps.spreadsheet'",
|
||||
orderBy: 'modifiedTime desc',
|
||||
pageSize: limit,
|
||||
fields: 'files(id,name,modifiedTime,webViewLink)'
|
||||
});
|
||||
|
||||
return response.data.files?.map(file => ({
|
||||
id: file.id!,
|
||||
name: file.name!,
|
||||
modifiedTime: file.modifiedTime!,
|
||||
webViewLink: file.webViewLink!
|
||||
})) || [];
|
||||
}
|
||||
|
||||
export async function getSpreadsheetData(refreshToken: string, spreadsheetId: string, range: string = 'A1:Z10'): Promise<SheetData> {
|
||||
const oauth = getAuthenticatedClient(refreshToken);
|
||||
const sheets = google.sheets({ version: 'v4', auth: oauth });
|
||||
|
||||
const response = await sheets.spreadsheets.values.get({
|
||||
spreadsheetId,
|
||||
range
|
||||
});
|
||||
|
||||
return {
|
||||
values: response.data.values || []
|
||||
};
|
||||
}
|
||||
|
||||
export async function getSpreadsheetInfo(refreshToken: string, spreadsheetId: string) {
|
||||
const oauth = getAuthenticatedClient(refreshToken);
|
||||
const sheets = google.sheets({ version: 'v4', auth: oauth });
|
||||
|
||||
const response = await sheets.spreadsheets.get({
|
||||
spreadsheetId,
|
||||
fields: 'properties.title,sheets.properties(title,sheetId)'
|
||||
});
|
||||
|
||||
return response.data;
|
||||
}
|
||||
Reference in New Issue
Block a user