Initial commit
This commit is contained in:
21
backend/utils/encrypt.js
Normal file
21
backend/utils/encrypt.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import crypto from 'crypto';
|
||||
|
||||
const createHash = (value) => {
|
||||
return crypto.createHash('sha256').update((typeof value === 'string' ? value : value.toString())).digest('hex');
|
||||
}
|
||||
|
||||
function encryptData(data) {
|
||||
const cipher = crypto.createCipheriv('aes-256-cbc', Buffer.from(process.env.ENCRYPTION_KEY, 'hex'), Buffer.alloc(16, 0));
|
||||
let encrypted = cipher.update(data, 'utf8', 'hex');
|
||||
encrypted += cipher.final('hex');
|
||||
return encrypted;
|
||||
}
|
||||
|
||||
function decryptData(data) {
|
||||
const decipher = crypto.createDecipheriv('aes-256-cbc', Buffer.from(process.env.ENCRYPTION_KEY, 'hex'), Buffer.alloc(16, 0));
|
||||
let decrypted = decipher.update(data, 'hex', 'utf8');
|
||||
decrypted += decipher.final('utf8');
|
||||
return decrypted;
|
||||
}
|
||||
|
||||
export default { createHash, encryptData, decryptData };
|
||||
28
backend/utils/userUtils.js
Normal file
28
backend/utils/userUtils.js
Normal file
@@ -0,0 +1,28 @@
|
||||
import { User, UserClub } from "../models";
|
||||
|
||||
export const getUserByToken = async(token) => {
|
||||
const user = await User.findOne({
|
||||
where: [
|
||||
{hashedId: token}
|
||||
]
|
||||
});
|
||||
return user;
|
||||
}
|
||||
|
||||
export const hasUserClubAccess = async(userId, clubId) => {
|
||||
const userClub = UserClub.findOne({
|
||||
where: {
|
||||
userId: userId,
|
||||
clubId: clubId,
|
||||
approved: true
|
||||
}
|
||||
});
|
||||
return userClub !== null;
|
||||
}
|
||||
|
||||
export const checkAccess = async(userToken, clubId) => {
|
||||
const user = getUserByToken(userToken);
|
||||
if (!hasUserClubAccess(user.id, clubId)) {
|
||||
throw new Error('noaccess');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user