UNPKG

@andreabiagini5/applicazioni-e-servizi-web-project

Version:
162 lines 5.87 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.deleteAccount = exports.changeEmail = exports.getMe = exports.logout = exports.login = exports.register = void 0; const service = __importStar(require("../services/account")); const Account_1 = require("../models/Account"); const jwt_1 = require("../config/jwt"); const jwt_2 = require("../config/jwt"); const jsonwebtoken_1 = __importDefault(require("jsonwebtoken")); /** * POST /register * Register a new user * * @returns 201: registration completed, 409: account already exists, 400: missing fields, 500: internal server error */ const register = async (req, res) => { try { const { username, email, password } = req.body; if (!username || !password || !email) { res.status(400).json({ message: 'Username, email and password are required' }); return; } const user = await Account_1.AccountFactory.createWithHashing(username, email, password); const result = await service.registerAccount(user); if (!result) res.status(409).json({ message: 'Account already exists' }); else res.status(201).json({ message: 'Account registered successfully', username }); } catch (error) { res.status(500).json({ message: 'Internal server error', error }); } }; exports.register = register; /** * POST /login * Authenticate an existing account * * @returns 200: with the created jwt in the cookie, 400: missing fields, 409: invalid credentials, 500: internal server error */ const login = async (req, res) => { try { const { username, password } = req.body; if (!username || !password) { res.status(400).json({ message: 'Username and password are required' }); return; } const account = await service.authenticateAccount(username, password); if (!account) { res.status(409).json({ message: 'Invalid username or password' }); return; } const token = jsonwebtoken_1.default.sign({ username: account.username, email: account.email, }, jwt_1.secret, { expiresIn: jwt_2.expiration }); res .cookie('token', token, { httpOnly: true, secure: process.env.NODE_ENV === 'production', sameSite: 'lax', maxAge: jwt_2.expiration * 1000, }) .status(200) .json({ message: 'Login successful' }); return; } catch (error) { res.status(500).json({ message: 'Internal server error', error }); } }; exports.login = login; /** * POST /logout * Logout the user * * @returns 200: logout successful, 500: internal server error */ const logout = async (_, res) => { res .clearCookie('token', { httpOnly: true, secure: process.env.NODE_ENV === 'production', sameSite: 'lax', }) .status(200) .json({ message: 'Logout successful' }); }; exports.logout = logout; /** * GET /me * Get the current user's account information * * @returns 200: with the user data */ const getMe = async (req, res) => { const { username, _ } = req.account; const account = await service.getAccount(username); if (!account) { res.status(404).json({ message: 'Account not found' }); return; } const { email } = account; res.status(200).json({ username, email }); }; exports.getMe = getMe; /*********************************** SOCKET ********************************/ /** * Change the email of the user * @param newEmail the new email of the user */ const changeEmail = async (oldAccountUsername, newEmail) => { const oldAccount = await service.getAccount(oldAccountUsername); if (!oldAccount) throw new Error('Account not found'); if (!await service.updateEmail(oldAccount, newEmail)) throw new Error('Email already exists'); }; exports.changeEmail = changeEmail; /** * Deletes the account associated with the given username. * @param username the username of the account to delete * @returns {Promise<boolean>} resolves when the account is deleted */ const deleteAccount = async (username) => await service.deleteAccount(username); exports.deleteAccount = deleteAccount; //# sourceMappingURL=account.js.map