Initial commit
This commit is contained in:
40
backend/services/authService.js
Normal file
40
backend/services/authService.js
Normal file
@@ -0,0 +1,40 @@
|
||||
import bcrypt from 'bcrypt';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import User from '../models/User.js';
|
||||
import { sendActivationEmail } from './emailService.js';
|
||||
|
||||
const register = async (email, password) => {
|
||||
const activationCode = Math.random().toString(36).substring(2, 15);
|
||||
const user = await User.create({ email, password, activationCode });
|
||||
await sendActivationEmail(email, activationCode);
|
||||
return user;
|
||||
};
|
||||
|
||||
const activateUser = async (activationCode) => {
|
||||
const user = await User.findOne({ where: { activationCode } });
|
||||
if (!user) throw new Error('Invalid activation code');
|
||||
user.isActive = true;
|
||||
user.activationCode = null;
|
||||
await user.save();
|
||||
return user;
|
||||
};
|
||||
|
||||
const login = async (email, password) => {
|
||||
const user = await User.findOne({ where: { email } });
|
||||
if (!user || !user.isActive) throw new Error('Invalid email or password');
|
||||
const isPasswordValid = await bcrypt.compare(password, user.password);
|
||||
if (!isPasswordValid) throw new Error('Invalid email or password');
|
||||
const token = jwt.sign({ userId: user.hashedId }, process.env.JWT_SECRET, { expiresIn: '1h' });
|
||||
await user.update({ hashedId: token });
|
||||
return { token };
|
||||
};
|
||||
|
||||
const logout = async(userId, authToken) => {
|
||||
const user = await User.findOne({ where: { id: userId, authToken: authToken }});
|
||||
if (!user) {
|
||||
throw new Error('not found');
|
||||
}
|
||||
user.update({ authToken: null });
|
||||
}
|
||||
|
||||
export { register, activateUser, login, logout };
|
||||
21
backend/services/emailService.js
Normal file
21
backend/services/emailService.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import nodemailer from 'nodemailer';
|
||||
|
||||
const transporter = nodemailer.createTransport({
|
||||
service: 'Gmail',
|
||||
auth: {
|
||||
user: process.env.EMAIL_USER,
|
||||
pass: process.env.EMAIL_PASS,
|
||||
},
|
||||
});
|
||||
|
||||
const sendActivationEmail = async (email, activationCode) => {
|
||||
const mailOptions = {
|
||||
from: process.env.EMAIL_USER,
|
||||
to: email,
|
||||
subject: 'Account Activation',
|
||||
text: `Activate your account by clicking the following link: ${process.env.BASE_URL}/api/auth/activate/${activationCode}`,
|
||||
};
|
||||
await transporter.sendMail(mailOptions);
|
||||
};
|
||||
|
||||
export { sendActivationEmail };
|
||||
20
backend/services/memberService.js
Normal file
20
backend/services/memberService.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import { UserClub } from "../models";
|
||||
import { checkAccess } from "../utils/userUtils";
|
||||
|
||||
class MemberService {
|
||||
async getApprovalRequests(userToken, clubId) {
|
||||
checkAccess(userToken, clubId);
|
||||
return UserClub.findAll({
|
||||
where: {
|
||||
clubId: clubId,
|
||||
approved: false
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async getClubMembers(clubId) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export default new MemberService();
|
||||
Reference in New Issue
Block a user