Add last used on logic (#2643)

* Add last used on logic

* Remove lodash
This commit is contained in:
Bruce Berrios 2022-03-05 21:25:30 -05:00 committed by GitHub
parent 6645c2d4bd
commit 7d19f54828
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 0 deletions

View file

@ -46,6 +46,7 @@ class ApeKeysController {
hash: saltyHash,
createdOn: Date.now(),
modifiedOn: Date.now(),
lastUsedOn: -1,
};
const apeKeyId = await ApeKeysDAO.addApeKey(uid, apeKey);

View file

@ -72,6 +72,16 @@ class ApeKeysDAO {
const apeKeys = _.omit(user.apeKeys, keyId);
await UsersDAO.setApeKeys(uid, apeKeys);
}
static async updateLastUsedOn(
user: MonkeyTypes.User,
keyId: string
): Promise<void> {
checkIfKeyExists(user.apeKeys, keyId);
user.apeKeys[keyId].lastUsedOn = Date.now();
await UsersDAO.setApeKeys(user.uid, user.apeKeys);
}
}
export default ApeKeysDAO;

View file

@ -1,6 +1,7 @@
import _ from "lodash";
import { compare } from "bcrypt";
import UsersDAO from "../dao/user";
import ApeKeysDAO from "../dao/ape-keys";
import MonkeyError from "../utils/error";
import { verifyIdToken } from "../utils/auth";
import { base64UrlDecode } from "../utils/misc";
@ -165,6 +166,8 @@ async function authenticateWithApeKey(
throw new MonkeyError(400, "Invalid ApeKey");
}
await ApeKeysDAO.updateLastUsedOn(keyOwner, keyId);
return {
uid,
email: keyOwner.email,

View file

@ -70,6 +70,7 @@ declare namespace MonkeyTypes {
hash: string;
createdOn: number;
modifiedOn: number;
lastUsedOn: number;
enabled: boolean;
}