Skip to content

Commit

Permalink
Merge pull request #20 from fga-eps-mds/fix#110/jwt-payload
Browse files Browse the repository at this point in the history
[FIX] Substitui atributo 'id' por 'userId' no payload do JWT (fga-eps-mds/2024.2-ARANDU-DOC#110)
  • Loading branch information
GabrielCostaDeOliveira authored Jan 10, 2025
2 parents 39ef0a9 + 70dd054 commit fe7da60
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
26 changes: 13 additions & 13 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
Injectable,
Logger,
NotFoundException,
UnauthorizedException,
Injectable,
Logger,
NotFoundException,
UnauthorizedException,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
Expand Down Expand Up @@ -50,13 +50,13 @@ export class AuthService {

async login(user: any) {
const tokens = await this.generateTokens({
id: user._id,
userId: user._id,
name: user.name,
email: user.email,
role: user.role,
});
return {
id: user._id,
userId: user._id,
name: user.name,
email: user.email,
...tokens,
Expand All @@ -75,7 +75,7 @@ export class AuthService {
});
}
const token = await this.generateTokens({
id: user._id,
userId: user._id,
name: user.name,
email: user.email,
role: user.role,
Expand All @@ -84,21 +84,21 @@ export class AuthService {
return { user, token };
}

async generateTokens({ id, name, email, role }) {
async generateTokens({ userId, name, email, role }) {
const payload = {
id: id,
userId: userId,
name: name,
email: email,
sub: id,
sub: userId,
role: role,
};
const accessToken = this.jwtService.sign(
{ id, ...payload },
{ userId, ...payload },
{ expiresIn: '10h' },
);

const refreshToken = uuidv4();
await this.storeRefreshToken(refreshToken, id);
await this.storeRefreshToken(refreshToken, userId);
return {
accessToken,
refreshToken,
Expand Down Expand Up @@ -142,7 +142,7 @@ export class AuthService {
const user = await this.usersService.findById(token.userId.toString());

return this.generateTokens({
id: user._id,
userId: user._id,
name: user.name,
email: user.email,
role: user.role,
Expand Down
10 changes: 5 additions & 5 deletions test/auth.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Logger, UnauthorizedException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { JwtService } from '@nestjs/jwt';
import { Test, TestingModule } from '@nestjs/testing';
import { Request } from 'express';
import { AuthController } from 'src/auth/auth.controller';
import { AuthService } from 'src/auth/auth.service';
import { JwtService } from '@nestjs/jwt';
import { ConfigService } from '@nestjs/config';
import { Logger, UnauthorizedException } from '@nestjs/common';
import { Request } from 'express';

describe('AuthController', () => {
let authController: AuthController;
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('AuthController', () => {
const result = {
accessToken: 'token',
refreshToken: 'refresh-token',
id: 1,
userId: 1,
name: 'Test User',
email: '[email protected]',
};
Expand Down
18 changes: 9 additions & 9 deletions test/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Test, TestingModule } from '@nestjs/testing';
import { JwtService } from '@nestjs/jwt';
import { EmailService } from 'src/users/email.service';
import { NotFoundException, UnauthorizedException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Model } from 'mongoose';
import { JwtService } from '@nestjs/jwt';
import { getModelToken } from '@nestjs/mongoose';
import { Test, TestingModule } from '@nestjs/testing';
import * as bcrypt from 'bcryptjs';
import { Model } from 'mongoose';
import { AuthService } from 'src/auth/auth.service';
import { EmailService } from 'src/users/email.service';
import { RefreshToken } from 'src/users/interface/refresh-token.schema';
import { ResetToken } from 'src/users/interface/reset-token.schema';
import { User } from 'src/users/interface/user.interface';
import * as bcrypt from 'bcryptjs';
import { UsersService } from 'src/users/users.service';
import { AuthService } from 'src/auth/auth.service';
import { NotFoundException, UnauthorizedException } from '@nestjs/common';

describe('AuthService', () => {
let service: AuthService;
Expand Down Expand Up @@ -90,7 +90,7 @@ describe('AuthService', () => {

const result = await service.login(user);
expect(result).toEqual({
id: 'user-id',
userId: 'user-id',
name: 'Test User',
email: '[email protected]',
accessToken: 'access-token',
Expand Down Expand Up @@ -127,7 +127,7 @@ describe('AuthService', () => {
jest.spyOn(service, 'storeRefreshToken').mockResolvedValue(undefined);

const result = await service.generateTokens({
id: 'user-id',
userId: 'user-id',
name: 'Test User',
email: '[email protected]',
role: 'user',
Expand Down

0 comments on commit fe7da60

Please sign in to comment.