longcelot-sheet-db
Version:
Google Sheets-backed staging database adapter for Node.js with schema-first design
60 lines • 2.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OAuthManager = void 0;
exports.createOAuthManager = createOAuthManager;
exports.createLoginOAuthManager = createLoginOAuthManager;
const google_auth_library_1 = require("google-auth-library");
const SHEETS_SCOPES = [
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive.file',
];
const LOGIN_SCOPES = [
'openid',
'email',
'profile',
...SHEETS_SCOPES,
];
class OAuthManager {
constructor(config, defaultScopes = SHEETS_SCOPES) {
this.client = new google_auth_library_1.OAuth2Client(config.clientId, config.clientSecret, config.redirectUri);
this.defaultScopes = defaultScopes;
}
getAuthUrl(scopes = this.defaultScopes) {
return this.client.generateAuthUrl({
access_type: 'offline',
scope: scopes,
prompt: 'consent',
});
}
async getTokens(code) {
const { tokens } = await this.client.getToken(code);
return tokens;
}
async refreshTokens(refreshToken) {
this.client.setCredentials({ refresh_token: refreshToken });
const { credentials } = await this.client.refreshAccessToken();
return credentials;
}
/** Verifies a Google ID token. Only works when `openid` scope was requested (use createLoginOAuthManager). */
async verifyToken(idToken) {
const ticket = await this.client.verifyIdToken({
idToken,
audience: this.client._clientId,
});
return ticket.getPayload();
}
}
exports.OAuthManager = OAuthManager;
/** Standard adapter-only OAuth — for backend-to-Sheets communication. Does NOT produce id_token. */
function createOAuthManager(config) {
return new OAuthManager(config, SHEETS_SCOPES);
}
/**
* Login OAuth manager — pre-configured with `openid email profile` scopes alongside
* Sheets scopes. Use this for user-facing Google Sign-In. The tokens it produces include
* an `id_token` that can be verified with `manager.verifyToken(idToken)`.
*/
function createLoginOAuthManager(config) {
return new OAuthManager(config, LOGIN_SCOPES);
}
//# sourceMappingURL=oauth.js.map