feat: refresh token
This commit is contained in:
parent
c29da7230a
commit
87405067ad
|
|
@ -1,5 +1,12 @@
|
||||||
import {
|
import {
|
||||||
Body, Controller, Delete, Get, Param, Post, Query, UseFilters
|
Body,
|
||||||
|
Controller,
|
||||||
|
Delete,
|
||||||
|
Get,
|
||||||
|
Param,
|
||||||
|
Post,
|
||||||
|
Query,
|
||||||
|
UseFilters,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { ioRedis } from '@gitroom/nestjs-libraries/redis/redis.service';
|
import { ioRedis } from '@gitroom/nestjs-libraries/redis/redis.service';
|
||||||
import { ConnectIntegrationDto } from '@gitroom/nestjs-libraries/dtos/integrations/connect.integration.dto';
|
import { ConnectIntegrationDto } from '@gitroom/nestjs-libraries/dtos/integrations/connect.integration.dto';
|
||||||
|
|
@ -22,7 +29,11 @@ import { PostsService } from '@gitroom/nestjs-libraries/database/prisma/posts/po
|
||||||
import { IntegrationTimeDto } from '@gitroom/nestjs-libraries/dtos/integrations/integration.time.dto';
|
import { IntegrationTimeDto } from '@gitroom/nestjs-libraries/dtos/integrations/integration.time.dto';
|
||||||
import { AuthService } from '@gitroom/helpers/auth/auth.service';
|
import { AuthService } from '@gitroom/helpers/auth/auth.service';
|
||||||
import { AuthTokenDetails } from '@gitroom/nestjs-libraries/integrations/social/social.integrations.interface';
|
import { AuthTokenDetails } from '@gitroom/nestjs-libraries/integrations/social/social.integrations.interface';
|
||||||
import { NotEnoughScopes } from '@gitroom/nestjs-libraries/integrations/social.abstract';
|
import {
|
||||||
|
NotEnoughScopes,
|
||||||
|
RefreshToken,
|
||||||
|
} from '@gitroom/nestjs-libraries/integrations/social.abstract';
|
||||||
|
import { timer } from '@gitroom/helpers/utils/timer';
|
||||||
|
|
||||||
@ApiTags('Integrations')
|
@ApiTags('Integrations')
|
||||||
@Controller('/integrations')
|
@Controller('/integrations')
|
||||||
|
|
@ -201,11 +212,51 @@ export class IntegrationsController {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (integrationProvider[body.name]) {
|
if (integrationProvider[body.name]) {
|
||||||
return integrationProvider[body.name](
|
try {
|
||||||
getIntegration.token,
|
const load = await integrationProvider[body.name](
|
||||||
body.data,
|
getIntegration.token,
|
||||||
getIntegration.internalId
|
body.data,
|
||||||
);
|
getIntegration.internalId
|
||||||
|
);
|
||||||
|
|
||||||
|
return load;
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof RefreshToken) {
|
||||||
|
const { accessToken, refreshToken, expiresIn } =
|
||||||
|
await integrationProvider.refreshToken(
|
||||||
|
getIntegration.refreshToken
|
||||||
|
);
|
||||||
|
|
||||||
|
if (accessToken) {
|
||||||
|
await this._integrationService.createOrUpdateIntegration(
|
||||||
|
getIntegration.organizationId,
|
||||||
|
getIntegration.name,
|
||||||
|
getIntegration.picture!,
|
||||||
|
'social',
|
||||||
|
getIntegration.internalId,
|
||||||
|
getIntegration.providerIdentifier,
|
||||||
|
accessToken,
|
||||||
|
refreshToken,
|
||||||
|
expiresIn
|
||||||
|
);
|
||||||
|
|
||||||
|
getIntegration.token = accessToken;
|
||||||
|
|
||||||
|
if (integrationProvider.refreshWait) {
|
||||||
|
await timer(10000);
|
||||||
|
}
|
||||||
|
return this.functionIntegration(org, body);
|
||||||
|
} else {
|
||||||
|
await this._integrationService.disconnectChannel(
|
||||||
|
org.id,
|
||||||
|
getIntegration
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
throw new Error('Function not found');
|
throw new Error('Function not found');
|
||||||
}
|
}
|
||||||
|
|
@ -352,7 +403,9 @@ export class IntegrationsController {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (refresh && id !== refresh) {
|
if (refresh && id !== refresh) {
|
||||||
throw new NotEnoughScopes('Please refresh the channel that needs to be refreshed');
|
throw new NotEnoughScopes(
|
||||||
|
'Please refresh the channel that needs to be refreshed'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._integrationService.createOrUpdateIntegration(
|
return this._integrationService.createOrUpdateIntegration(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue