-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 🐛 return accessToken instead of hard code
- Loading branch information
1 parent
b687b28
commit 45fb656
Showing
5 changed files
with
171 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,54 @@ describe('AuthService', () => { | |
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
|
||
describe('signIn', () => { | ||
it('should return access token when credentials are valid', async () => { | ||
const mockEmail = '[email protected]'; | ||
const mockPassword = 'password123'; | ||
const mockUser = { | ||
id: '123', | ||
email: mockEmail, | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
}; | ||
const mockAccessToken = 'mock.jwt.token'; | ||
|
||
jest.spyOn(service['awsCognitoService'], 'signIn').mockResolvedValue({}); | ||
jest | ||
.spyOn(service['usersService'], 'findByEmail') | ||
.mockResolvedValue(mockUser); | ||
jest | ||
.spyOn(service['jwtService'], 'signAsync') | ||
.mockResolvedValue(mockAccessToken); | ||
|
||
const result = await service.signIn(mockEmail, mockPassword); | ||
|
||
expect(result).toEqual({ accessToken: mockAccessToken }); | ||
expect(service['awsCognitoService'].signIn).toHaveBeenCalledWith( | ||
mockEmail, | ||
mockPassword, | ||
); | ||
expect(service['usersService'].findByEmail).toHaveBeenCalledWith( | ||
mockEmail, | ||
); | ||
expect(service['jwtService'].signAsync).toHaveBeenCalledWith({ | ||
sub: mockUser.id, | ||
username: mockUser.email, | ||
}); | ||
}); | ||
|
||
it('should throw UnauthorizedException when credentials are invalid', async () => { | ||
const mockEmail = '[email protected]'; | ||
const mockPassword = 'wrongpassword'; | ||
|
||
jest | ||
.spyOn(service['awsCognitoService'], 'signIn') | ||
.mockRejectedValue(new Error('Invalid credentials')); | ||
|
||
await expect(service.signIn(mockEmail, mockPassword)).rejects.toThrow( | ||
'Unauthorized', | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,33 @@ | ||
import { UnauthorizedException } from '@nestjs/common'; | ||
import { UsersService } from '@users/application'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { AwsCognitoService } from '@shared/infrastructure-aws-cognito'; | ||
|
||
export class AuthService { | ||
constructor(private readonly awsCognitoService: AwsCognitoService) {} | ||
constructor( | ||
private awsCognitoService: AwsCognitoService, | ||
private usersService: UsersService, | ||
private jwtService: JwtService, | ||
) {} | ||
|
||
async signIn( | ||
username: string, | ||
email: string, | ||
pass: string, | ||
): Promise<{ | ||
accessToken: string; | ||
}> { | ||
// TODO: Implement sign in | ||
// Step 1: Validate user credentials via AWS Cognito | ||
const authResponse = await this.awsCognitoService.signIn(username, pass); | ||
// Step 2: Retrieve user from the database | ||
// Step 3: Generate a custom JWT access token | ||
|
||
return { | ||
accessToken: 'accessToken', | ||
}; | ||
try { | ||
await this.awsCognitoService.signIn(email, pass); | ||
const user = await this.usersService.findByEmail(email); | ||
const accessToken = await this.jwtService.signAsync({ | ||
sub: user.id, | ||
username: user.email, | ||
}); | ||
return { | ||
accessToken, | ||
}; | ||
} catch (error) { | ||
throw new UnauthorizedException(error); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.