### Installing NestJS Google OAuth Module Source: https://github.com/sowonlabs/nestjs-google-oauth-integration/blob/main/README.md Command to install the necessary packages for the NestJS Google OAuth integration module, including the core module, googleapis, and local-auth. ```bash npm install @sowonai/nestjs-google-oauth-integration googleapis @google-cloud/local-auth ``` -------------------------------- ### Configuring GoogleOAuthModule with In-Memory Token Storage (NestJS) Source: https://github.com/sowonlabs/nestjs-google-oauth-integration/blob/main/README.md Example demonstrating the use of the built-in InMemoryTokenRepository for token storage. This strategy is non-persistent and primarily useful for testing environments or short-lived processes. ```typescript import { Module } from '@nestjs/common'; import { GoogleOAuthModule, InMemoryTokenRepository } from '@sowonai/nestjs-google-oauth-integration'; @Module({ imports: [ GoogleOAuthModule.forRoot({ name: 'my-test-app', credentialsFilename: 'test-credentials.json', scopes: ['https://www.googleapis.com/auth/gmail.readonly'], tokenRepository: InMemoryTokenRepository }), ], }) export class TestAppModule {} ``` -------------------------------- ### Configuring GoogleOAuthModule without Token Storage (NestJS) Source: https://github.com/sowonlabs/nestjs-google-oauth-integration/blob/main/README.md Example of setting up the GoogleOAuthModule in a NestJS application without persisting tokens. This configuration is suitable for scenarios where tokens are managed externally or not needed across application restarts. ```typescript import { Module } from '@nestjs/common'; import { GoogleOAuthModule } from '@sowonai/nestjs-google-oauth-integration'; @Module({ imports: [ GoogleOAuthModule.forRoot({ name: 'my-app', credentialsFilename: 'credentials.json', scopes: [ 'https://www.googleapis.com/auth/gmail.readonly', 'https://www.googleapis.com/auth/gmail.send' ] // If tokenRepository is not specified, tokens are not persisted }), ], }) export class AppModule {} ``` -------------------------------- ### Implementing and Using a Custom Token Repository (NestJS) Source: https://github.com/sowonlabs/nestjs-google-oauth-integration/blob/main/README.md Shows how to create a custom token repository by implementing the TokenRepository interface. This allows integrating token storage with databases or other custom persistence layers, demonstrated here using TypeORM. ```typescript import { Module, Injectable } from '@nestjs/common'; import { GoogleOAuthModule, TokenRepository } from '@sowonai/nestjs-google-oauth-integration'; import { TypeOrmModule } from '@nestjs/typeorm'; import { TokenEntity } from './entities/token.entity'; import { Repository } from 'typeorm'; import { InjectRepository } from '@nestjs/typeorm'; import { Credentials } from 'google-auth-library'; // Custom token repository implementation @Injectable() class CustomTokenRepository implements TokenRepository { constructor(@InjectRepository(TokenEntity) private repo: Repository) {} async saveToken(token: Credentials, userId?: string): Promise { // Logic to save token in the database } async getToken(userId?: string): Promise { // Logic to retrieve token from the database } async hasToken(userId?: string): Promise { // Logic to check token existence in the database } } @Module({ imports: [ TypeOrmModule.forFeature([TokenEntity]), GoogleOAuthModule.forRoot({ name: 'my-server-app', credentialsFilename: 'server-credentials.json', scopes: ['https://www.googleapis.com/auth/gmail.readonly'], tokenRepository: CustomTokenRepository }), ], providers: [CustomTokenRepository] }) export class ServerAppModule {} ``` -------------------------------- ### Configuring GoogleOAuthModule with File System Token Storage (NestJS) Source: https://github.com/sowonlabs/nestjs-google-oauth-integration/blob/main/README.md Demonstrates how to configure the GoogleOAuthModule to use the FileSystemTokenRepository for persisting tokens to the local file system. This requires specifying the directory and filename for the token file. ```typescript import { Module } from '@nestjs/common'; import { GoogleOAuthModule, FileSystemTokenRepository } from '@sowonai/nestjs-google-oauth-integration'; import * as path from 'path'; import * as os from 'os'; const tokenDir = path.join(os.homedir(), '.my-app'); @Module({ imports: [ GoogleOAuthModule.forRoot({ name: 'my-app', credentialsFilename: 'credentials.json', tokenRepository: new FileSystemTokenRepository({ tokenDir: tokenDir, tokenPath: path.join(tokenDir, 'google-token.json') }), scopes: [ 'https://www.googleapis.com/auth/gmail.readonly', 'https://www.googleapis.com/auth/gmail.send' ] }), ], }) export class AppModule {} ``` -------------------------------- ### Using GoogleOAuthService in NestJS Service Source: https://github.com/sowonlabs/nestjs-google-oauth-integration/blob/main/README.md Demonstrates how to inject and utilize the GoogleOAuthService within a NestJS service to handle Google authentication. It shows checking authentication status and initiating the authentication flow for both single-user and multi-tenant scenarios before performing an action like sending an email. ```typescript import { Injectable } from '@nestjs/common'; import { GoogleOAuthService } from '@sowonai/nestjs-google-oauth-integration'; @Injectable() export class GmailService { constructor(private readonly googleOAuthService: GoogleOAuthService) {} async sendEmail(to: string, subject: string, body: string) { // Check authentication const isAuth = await this.googleOAuthService.isAuthenticated(); if (!isAuth) { await this.googleOAuthService.authenticate(); } // Email sending logic using Google APIs // ... } // Example for multi-tenant environments async sendEmailAsUser(userId: string, to: string, subject: string, body: string) { // Check authentication for a specific user const isAuth = await this.googleOAuthService.isAuthenticated(userId); if (!isAuth) { await this.googleOAuthService.authenticate(userId); } // Email sending logic for a specific user using Google APIs // ... } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.