feat(server): added in profile update (password only currently)
This commit is contained in:
38
server/services/auth/controllers/users/updateProfile.ts
Normal file
38
server/services/auth/controllers/users/updateProfile.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import {eq, sql} from "drizzle-orm";
|
||||
import {db} from "../../../../../database/dbclient.js";
|
||||
import {users} from "../../../../../database/schema/users.js";
|
||||
import {log} from "../../../logger/logger.js";
|
||||
import {createPassword} from "../../utils/createPassword.js";
|
||||
|
||||
const blacklistedTokens = new Set();
|
||||
|
||||
function blacklistToken(token: string) {
|
||||
blacklistedTokens.add(token);
|
||||
setTimeout(() => blacklistedTokens.delete(token), 3600 * 1000); // Remove after 1 hour
|
||||
}
|
||||
|
||||
function isTokenBlacklisted(token: string) {
|
||||
return blacklistedTokens.has(token);
|
||||
}
|
||||
|
||||
export const updateProfile = async (user: any, data: any, token: string) => {
|
||||
if (isTokenBlacklisted(token)) {
|
||||
log.warn(`${user.username} is trying to use a black listed token`);
|
||||
throw Error("This token was already used");
|
||||
}
|
||||
log.info(`${user.user_id}`);
|
||||
|
||||
//re salt and encrypt the password
|
||||
try {
|
||||
const saltPass = await createPassword(data.password);
|
||||
// update the password
|
||||
const profileUpdate = await db
|
||||
.update(users)
|
||||
.set({password: saltPass, upd_user: user.username, upd_date: sql`NOW()`})
|
||||
.where(eq(users.user_id, user.user_id));
|
||||
|
||||
blacklistToken(token);
|
||||
} catch (error) {
|
||||
log.error(error, "There was an error updating the users profile");
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user