Skip to content

Commit

Permalink
feat: reflect schema changes in graphql mutations and queries
Browse files Browse the repository at this point in the history
  • Loading branch information
pacyL2K19 committed Apr 19, 2024
1 parent 3ba83f4 commit 21cfe34
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
3 changes: 3 additions & 0 deletions packages/twenty-front/src/generated/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ export type RemoteServer = {

export type RemoteTable = {
__typename?: 'RemoteTable';
id?: Maybe<Scalars['UUID']>;
name: Scalars['String'];
schema: Scalars['String'];
status: RemoteTableStatus;
Expand Down Expand Up @@ -730,7 +731,9 @@ export type User = {
id: Scalars['UUID'];
lastName: Scalars['String'];
passwordHash?: Maybe<Scalars['String']>;
/** @deprecated field migrated into the AppTokens Table ref: https://github.com/twentyhq/twenty/issues/5021 */
passwordResetToken?: Maybe<Scalars['String']>;
/** @deprecated field migrated into the AppTokens Table ref: https://github.com/twentyhq/twenty/issues/5021 */
passwordResetTokenExpiresAt?: Maybe<Scalars['DateTime']>;
supportUserHash?: Maybe<Scalars['String']>;
updatedAt: Scalars['DateTime'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import { InjectRepository } from '@nestjs/typeorm';

import crypto from 'crypto';

import { addMilliseconds, differenceInMilliseconds, isFuture } from 'date-fns';
import { addMilliseconds, differenceInMilliseconds } from 'date-fns';
import ms from 'ms';
import { JsonWebTokenError, TokenExpiredError } from 'jsonwebtoken';
import { Repository } from 'typeorm';
import { MoreThan, Repository } from 'typeorm';
import { Request } from 'express';
import { ExtractJwt } from 'passport-jwt';
import { render } from '@react-email/render';
Expand Down Expand Up @@ -520,22 +520,23 @@ export class TokenService {
InternalServerErrorException,
);

if (
user.passwordResetToken &&
user.passwordResetTokenExpiresAt &&
isFuture(user.passwordResetTokenExpiresAt)
) {
const existingToken = await this.appTokenRepository.findOne({
where: {
userId: user.id,
type: AppTokenType.PasswordResetToken,
expiresAt: MoreThan(new Date()),
},
});

if (existingToken) {
const timeToWait = ms(
differenceInMilliseconds(existingToken.expiresAt, new Date()),
{ long: true },
);

assert(
false,
`Token has been already generated. Please wait for ${ms(
differenceInMilliseconds(
user.passwordResetTokenExpiresAt,
new Date(),
),
{
long: true,
},
)} to generate again.`,
`Token has already been generated. Please wait for ${timeToWait} to generate again.`,
BadRequestException,
);
}
Expand All @@ -548,9 +549,11 @@ export class TokenService {

const expiresAt = addMilliseconds(new Date().getTime(), ms(expiresIn));

await this.userRepository.update(user.id, {
passwordResetToken: hashedResetToken,
passwordResetTokenExpiresAt: expiresAt,
await this.appTokenRepository.save({
userId: user.id,
value: hashedResetToken,
expiresAt,
type: AppTokenType.PasswordResetToken,
});

return {
Expand Down Expand Up @@ -615,19 +618,21 @@ export class TokenService {
.update(resetToken)
.digest('hex');

const user = await this.userRepository.findOneBy({
passwordResetToken: hashedResetToken,
const token = await this.appTokenRepository.findOne({
where: {
value: hashedResetToken,
type: AppTokenType.PasswordResetToken,
expiresAt: MoreThan(new Date()),
},
});

assert(user, 'Token is invalid', NotFoundException);
assert(token, 'Token is invalid', NotFoundException);

const tokenExpiresAt = user.passwordResetTokenExpiresAt;
const user = await this.userRepository.findOneBy({
id: token.userId,
});

assert(
tokenExpiresAt && isFuture(tokenExpiresAt),
'Token has expired. Please regenerate',
NotFoundException,
);
assert(user, 'Token is invalid', NotFoundException);

return {
id: user.id,
Expand Down

0 comments on commit 21cfe34

Please sign in to comment.