UNPKG

@checkfirst/nestjs-outlook

Version:

An opinionated NestJS module for Microsoft Outlook integration that provides easy access to Microsoft Graph API for emails, calendars, and more.

103 lines 4.9 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var MicrosoftAuthController_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.MicrosoftAuthController = void 0; const common_1 = require("@nestjs/common"); const swagger_1 = require("@nestjs/swagger"); const microsoft_auth_service_1 = require("../services/auth/microsoft-auth.service"); let MicrosoftAuthController = MicrosoftAuthController_1 = class MicrosoftAuthController { constructor(microsoftAuthService) { this.microsoftAuthService = microsoftAuthService; this.logger = new common_1.Logger(MicrosoftAuthController_1.name); } async handleOauthCallback(code, state, res) { try { if (!code || !state) { this.logger.error('Missing required parameters for OAuth callback'); return res.status(common_1.HttpStatus.BAD_REQUEST).send('Missing required parameters'); } await this.microsoftAuthService.exchangeCodeForToken(code, state); return res.status(common_1.HttpStatus.OK).send(` <h1>Authorization successful!</h1> <p>Your Microsoft account has been linked successfully.</p> <p>You can close this tab now.</p> <script> // Optionally notify the parent window if (window.opener) { window.opener.postMessage('microsoft-auth-success', '*'); } </script> `); } catch (error) { this.logger.error('Error handling OAuth callback:', error); return res .status(common_1.HttpStatus.INTERNAL_SERVER_ERROR) .send('An error occurred during authentication'); } } }; exports.MicrosoftAuthController = MicrosoftAuthController; __decorate([ (0, common_1.Get)('callback'), (0, swagger_1.ApiOperation)({ summary: 'Microsoft OAuth callback handler', description: 'Processes the callback from Microsoft OAuth authentication flow. Exchanges the authorization code for access and refresh tokens, saves them for the user, and sets up necessary webhooks for calendar synchronization. The user ID is extracted from the state parameter.', }), (0, swagger_1.ApiQuery)({ name: 'code', description: 'Authorization code from Microsoft', required: true, type: String, example: 'M.R3_BAY.c0def4c2-12bf-0b29-9a3a-f8a1c4f56789', }), (0, swagger_1.ApiQuery)({ name: 'state', description: 'Base64 encoded state containing user ID and CSRF token', required: true, type: String, example: 'eyJ1c2VySWQiOiI3IiwiY3NyZiI6IjEyMzQ1In0', }), (0, swagger_1.ApiResponse)({ status: 200, description: 'Authentication successful, tokens saved and webhooks created', content: { 'text/html': { example: '<h1>Authorization successful!</h1><p>Your Microsoft account has been linked successfully.</p>', }, }, }), (0, swagger_1.ApiResponse)({ status: 400, description: 'Invalid or missing code/state parameters', }), (0, swagger_1.ApiResponse)({ status: 500, description: 'Server error during authentication process', }), (0, swagger_1.ApiProduces)('text/html'), __param(0, (0, common_1.Query)('code')), __param(1, (0, common_1.Query)('state')), __param(2, (0, common_1.Res)()), __metadata("design:type", Function), __metadata("design:paramtypes", [String, String, Object]), __metadata("design:returntype", Promise) ], MicrosoftAuthController.prototype, "handleOauthCallback", null); exports.MicrosoftAuthController = MicrosoftAuthController = MicrosoftAuthController_1 = __decorate([ (0, swagger_1.ApiTags)('Microsoft Auth'), (0, common_1.Controller)('auth/microsoft'), __metadata("design:paramtypes", [microsoft_auth_service_1.MicrosoftAuthService]) ], MicrosoftAuthController); //# sourceMappingURL=microsoft-auth.controller.js.map