Skip to content

Commit

Permalink
Feat: delete passkey by id (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
vmkhitaryanscn authored Nov 7, 2024
1 parent 43e0d40 commit 388d715
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/api/passkeys/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import { LoginBodySchema } from '../auth/validation.schema';
import { Env, ONE_MINUTE, ONE_MONTH, rpID, rpName } from '~/core/constants';
import { origin } from '~/core/constants';
import { ErrorMessages } from '~/core/dictionary/error.messages';
import { BadRequestError, UnauthorizedError } from '~/core/errors';
import {
BadRequestError,
NotFoundError,
UnauthorizedError,
} from '~/core/errors';
import { logger } from '~/core/logger';
import { authMiddleware } from '~/core/middleware/auth';
import { jwtService, modelToPlain } from '~/core/utils';
Expand Down Expand Up @@ -379,4 +383,39 @@ route.get(
res.status(200).json(userPassKeysResult);
},
);

route.delete(
'/:passkeyId',
authMiddleware,
async (req: Request, res: Response, next: NextFunction) => {
const user = req.user;

if (!isAuthenticated(user)) {
return next(new UnauthorizedError(ErrorMessages.unauthorized));
}

const passkeyId = req.params.passkeyId;

try {
const deleteResult = await UserCredentialCrudService.deleteOneByParams({
id: passkeyId,
userId: user.id,
});

if (deleteResult === 0) {
throw new NotFoundError('Passkey not found');
}

return res.status(200).json({
type: 'PASSKEY_DELETED',
statusCode: 200,
message: 'Passkey deleted successfully',
isSuccess: true,
});
} catch (error: unknown) {
return next(error);
}
},
);

export default route;
14 changes: 14 additions & 0 deletions src/shared/UserCredential/UserCredential.crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ interface UpdateCredentialCounterPayload {
counter: number;
}

export interface DeleteByParams {
id: string;
userId: string;
}

export class UserCredentialCrudService {
static async getCredentialByUserId(userId: string) {
return UserCredential.findAll({ where: { userId } });
Expand All @@ -42,4 +47,13 @@ export class UserCredentialCrudService {
{ where: { credId: payload.credId } },
);
}

static deleteOneByParams(params: DeleteByParams) {
return UserCredential.destroy({
where: {
id: params.id,
userId: params.userId,
},
});
}
}

0 comments on commit 388d715

Please sign in to comment.