fix: changed all refrences to passwordlessVerification to verificationTokens in auth module

This commit is contained in:
Balu Babu
2023-02-01 11:58:15 +05:30
parent 3afc89db6b
commit 5c5ab5bad5
6 changed files with 69 additions and 85 deletions

View File

@@ -1,13 +1,13 @@
import { HttpStatus } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { Account, PasswordlessVerification } from '@prisma/client';
import { Account, VerificationToken } from '@prisma/client';
import { mockDeep, mockFn } from 'jest-mock-extended';
import {
INVALID_EMAIL,
INVALID_MAGIC_LINK_DATA,
INVALID_REFRESH_TOKEN,
MAGIC_LINK_EXPIRED,
PASSWORDLESS_DATA_NOT_FOUND,
VERIFICATION_TOKEN_DATA_NOT_FOUND,
USER_NOT_FOUND,
} from 'src/errors';
import { MailerService } from 'src/mailer/mailer.service';
@@ -39,9 +39,11 @@ const user: AuthUser = {
isAdmin: false,
refreshToken: 'hbfvdkhjbvkdvdfjvbnkhjb',
createdOn: currentTime,
currentGQLSession: {},
currentRESTSession: {},
};
const passwordlessData: PasswordlessVerification = {
const passwordlessData: VerificationToken = {
deviceIdentifier: 'k23hb7u7gdcujhb',
token: 'jhhj24sdjvl',
userUid: user.uid,
@@ -85,10 +87,8 @@ describe('signInMagicLink', () => {
mockUser.findUserByEmail.mockResolvedValue(O.none);
// create new user
mockUser.createUserViaMagicLink.mockResolvedValue(user);
// create new entry in passwordlessVerification table
mockPrisma.passwordlessVerification.create.mockResolvedValueOnce(
passwordlessData,
);
// create new entry in VerificationToken table
mockPrisma.verificationToken.create.mockResolvedValueOnce(passwordlessData);
const result = await authService.signInMagicLink(
'dwight@dundermifflin.com',
@@ -101,10 +101,8 @@ describe('signInMagicLink', () => {
test('should successfully return the passwordless details for a pre-existing user account', async () => {
// check to see if user exists, return error
mockUser.findUserByEmail.mockResolvedValueOnce(O.some(user));
// create new entry in passwordlessVerification table
mockPrisma.passwordlessVerification.create.mockResolvedValueOnce(
passwordlessData,
);
// create new entry in VerificationToken table
mockPrisma.verificationToken.create.mockResolvedValueOnce(passwordlessData);
const result = await authService.signInMagicLink(
'dwight@dundermifflin.com',
@@ -117,7 +115,7 @@ describe('signInMagicLink', () => {
describe('verifyMagicLinkTokens', () => {
test('should throw INVALID_MAGIC_LINK_DATA if data is invalid', async () => {
mockPrisma.passwordlessVerification.findUniqueOrThrow.mockRejectedValueOnce(
mockPrisma.verificationToken.findUniqueOrThrow.mockRejectedValueOnce(
'NotFoundError',
);
@@ -130,7 +128,7 @@ describe('verifyMagicLinkTokens', () => {
test('should throw USER_NOT_FOUND if user is invalid', async () => {
// validatePasswordlessTokens
mockPrisma.passwordlessVerification.findUniqueOrThrow.mockResolvedValueOnce(
mockPrisma.verificationToken.findUniqueOrThrow.mockResolvedValueOnce(
passwordlessData,
);
// findUserById
@@ -145,12 +143,10 @@ describe('verifyMagicLinkTokens', () => {
test('should successfully return auth token pair with provider account existing', async () => {
// validatePasswordlessTokens
mockPrisma.passwordlessVerification.findUniqueOrThrow.mockResolvedValueOnce(
{
...passwordlessData,
expiresOn: nowPlus30,
},
);
mockPrisma.verificationToken.findUniqueOrThrow.mockResolvedValueOnce({
...passwordlessData,
expiresOn: nowPlus30,
});
// findUserById
mockUser.findUserById.mockResolvedValue(O.some(user));
// checkIfProviderAccountExists
@@ -160,9 +156,7 @@ describe('verifyMagicLinkTokens', () => {
mockJWT.sign.mockReturnValue(user.refreshToken);
mockPrisma.user.update.mockResolvedValueOnce(user);
// deletePasswordlessVerificationToken
mockPrisma.passwordlessVerification.delete.mockResolvedValueOnce(
passwordlessData,
);
mockPrisma.verificationToken.delete.mockResolvedValueOnce(passwordlessData);
const result = await authService.verifyMagicLinkTokens(magicLinkVerify);
expect(result).toEqualRight({
@@ -173,12 +167,10 @@ describe('verifyMagicLinkTokens', () => {
test('should successfully return auth token pair with provider account not existing', async () => {
// validatePasswordlessTokens
mockPrisma.passwordlessVerification.findUniqueOrThrow.mockResolvedValueOnce(
{
...passwordlessData,
expiresOn: nowPlus30,
},
);
mockPrisma.verificationToken.findUniqueOrThrow.mockResolvedValueOnce({
...passwordlessData,
expiresOn: nowPlus30,
});
// findUserById
mockUser.findUserById.mockResolvedValue(O.some(user));
// checkIfProviderAccountExists
@@ -188,9 +180,7 @@ describe('verifyMagicLinkTokens', () => {
mockJWT.sign.mockReturnValue(user.refreshToken);
mockPrisma.user.update.mockResolvedValueOnce(user);
// deletePasswordlessVerificationToken
mockPrisma.passwordlessVerification.delete.mockResolvedValueOnce(
passwordlessData,
);
mockPrisma.verificationToken.delete.mockResolvedValueOnce(passwordlessData);
const result = await authService.verifyMagicLinkTokens(magicLinkVerify);
expect(result).toEqualRight({
@@ -201,7 +191,7 @@ describe('verifyMagicLinkTokens', () => {
test('should throw MAGIC_LINK_EXPIRED if passwordless token is expired', async () => {
// validatePasswordlessTokens
mockPrisma.passwordlessVerification.findUniqueOrThrow.mockResolvedValueOnce(
mockPrisma.verificationToken.findUniqueOrThrow.mockResolvedValueOnce(
passwordlessData,
);
// findUserById
@@ -218,12 +208,10 @@ describe('verifyMagicLinkTokens', () => {
test('should throw USER_NOT_FOUND when updating refresh tokens fails', async () => {
// validatePasswordlessTokens
mockPrisma.passwordlessVerification.findUniqueOrThrow.mockResolvedValueOnce(
{
...passwordlessData,
expiresOn: nowPlus30,
},
);
mockPrisma.verificationToken.findUniqueOrThrow.mockResolvedValueOnce({
...passwordlessData,
expiresOn: nowPlus30,
});
// findUserById
mockUser.findUserById.mockResolvedValue(O.some(user));
// checkIfProviderAccountExists
@@ -242,12 +230,10 @@ describe('verifyMagicLinkTokens', () => {
test('should throw PASSWORDLESS_DATA_NOT_FOUND when deleting passwordlessVerification entry from DB', async () => {
// validatePasswordlessTokens
mockPrisma.passwordlessVerification.findUniqueOrThrow.mockResolvedValueOnce(
{
...passwordlessData,
expiresOn: nowPlus30,
},
);
mockPrisma.verificationToken.findUniqueOrThrow.mockResolvedValueOnce({
...passwordlessData,
expiresOn: nowPlus30,
});
// findUserById
mockUser.findUserById.mockResolvedValue(O.some(user));
// checkIfProviderAccountExists
@@ -257,13 +243,11 @@ describe('verifyMagicLinkTokens', () => {
mockJWT.sign.mockReturnValue(user.refreshToken);
mockPrisma.user.update.mockResolvedValueOnce(user);
// deletePasswordlessVerificationToken
mockPrisma.passwordlessVerification.delete.mockRejectedValueOnce(
'RecordNotFound',
);
mockPrisma.verificationToken.delete.mockRejectedValueOnce('RecordNotFound');
const result = await authService.verifyMagicLinkTokens(magicLinkVerify);
expect(result).toEqualLeft({
message: PASSWORDLESS_DATA_NOT_FOUND,
message: VERIFICATION_TOKEN_DATA_NOT_FOUND,
statusCode: HttpStatus.NOT_FOUND,
});
});

View File

@@ -13,7 +13,7 @@ import { DeviceIdentifierToken } from 'src/types/Passwordless';
import {
INVALID_EMAIL,
INVALID_MAGIC_LINK_DATA,
PASSWORDLESS_DATA_NOT_FOUND,
VERIFICATION_TOKEN_DATA_NOT_FOUND,
MAGIC_LINK_EXPIRED,
USER_NOT_FOUND,
INVALID_REFRESH_TOKEN,
@@ -27,7 +27,7 @@ import {
import { JwtService } from '@nestjs/jwt';
import { AuthError } from 'src/types/AuthError';
import { AuthUser } from 'src/types/AuthUser';
import { PasswordlessVerification } from '@prisma/client';
import { VerificationToken } from '@prisma/client';
@Injectable()
export class AuthService {
@@ -42,7 +42,7 @@ export class AuthService {
* Generate Id and token for email Magic-Link auth
*
* @param user User Object
* @returns Created PasswordlessVerification token
* @returns Created VerificationToken token
*/
private async generateMagicLinkTokens(user: AuthUser) {
const salt = await bcrypt.genSalt(
@@ -53,7 +53,7 @@ export class AuthService {
.toISO()
.toString();
const idToken = await this.prismaService.passwordlessVerification.create({
const idToken = await this.prismaService.verificationToken.create({
data: {
deviceIdentifier: salt,
userUid: user.uid,
@@ -65,15 +65,15 @@ export class AuthService {
}
/**
* Check if passwordlessVerification exist or not
* Check if VerificationToken exist or not
*
* @param magicLinkTokens Object containing deviceIdentifier and token
* @returns Option of PasswordlessVerification token
* @returns Option of VerificationToken token
*/
private async validatePasswordlessTokens(magicLinkTokens: verifyMagicDto) {
try {
const tokens =
await this.prismaService.passwordlessVerification.findUniqueOrThrow({
await this.prismaService.verificationToken.findUniqueOrThrow({
where: {
passwordless_deviceIdentifier_tokens: {
deviceIdentifier: magicLinkTokens.deviceIdentifier,
@@ -144,17 +144,17 @@ export class AuthService {
}
/**
* Deleted used PasswordlessVerification tokens
* Deleted used VerificationToken tokens
*
* @param passwordlessTokens PasswordlessVerification entry to delete from DB
* @returns Either of deleted PasswordlessVerification token
* @param passwordlessTokens VerificationToken entry to delete from DB
* @returns Either of deleted VerificationToken token
*/
private async deleteMagicLinkVerificationTokens(
passwordlessTokens: PasswordlessVerification,
passwordlessTokens: VerificationToken,
) {
try {
const deletedPasswordlessToken =
await this.prismaService.passwordlessVerification.delete({
await this.prismaService.verificationToken.delete({
where: {
passwordless_deviceIdentifier_tokens: {
deviceIdentifier: passwordlessTokens.deviceIdentifier,
@@ -164,7 +164,7 @@ export class AuthService {
});
return E.right(deletedPasswordlessToken);
} catch (error) {
return E.left(PASSWORDLESS_DATA_NOT_FOUND);
return E.left(VERIFICATION_TOKEN_DATA_NOT_FOUND);
}
}

View File

@@ -325,11 +325,11 @@ export const BUG_TEAM_ENV_GUARD_NO_ENV_ID =
export const INVALID_MAGIC_LINK_DATA = 'auth/magic_link_invalid_data' as const;
/**
* Could not find PasswordlessVerification entry in the db
* Could not find VerificationToken entry in the db
* (AuthService)
*/
export const PASSWORDLESS_DATA_NOT_FOUND =
'auth/passwordless_token_data_not_found' as const;
export const VERIFICATION_TOKEN_DATA_NOT_FOUND =
'auth/verification_token_data_not_found' as const;
/**
* Auth Tokens expired
@@ -338,7 +338,7 @@ export const PASSWORDLESS_DATA_NOT_FOUND =
export const TOKEN_EXPIRED = 'auth/token_expired' as const;
/**
* PasswordlessVerification Tokens expired i.e. magic-link expired
* VerificationToken Tokens expired i.e. magic-link expired
* (AuthService)
*/
export const MAGIC_LINK_EXPIRED = 'auth/magic_link_expired' as const;

View File

@@ -88,7 +88,7 @@ export class UserService {
const createdUser = await this.prisma.user.create({
data: {
email: email,
accounts: {
providerAccounts: {
create: {
provider: 'magic',
providerAccountId: email,
@@ -121,7 +121,7 @@ export class UserService {
displayName: userDisplayName,
email: profile.emails[0].value,
photoURL: userPhotoURL,
accounts: {
providerAccounts: {
create: {
provider: profile.provider,
providerAccountId: profile.id,