123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- import _ from "lodash";
- import slugify from "slugify";
- import AccountService from "../accounts/account.service.js";
- import AccountValidator from "../accounts/account.validator.js";
- import UserValidator from "../users/user.validator.js";
- import AuthService from "./auth.service.js";
- class Controller {
- async signup(req, res, next) {
- const accountData = _.pick(req.body, [
- "subdomain",
- "privacyAccepted",
- "marketingAccepted",
- ]);
- accountData.subdomain = slugify(accountData.subdomain, {
- replacement: "-",
- lower: true,
- strict: true,
- trim: true,
- });
- const accountErrors = await AccountValidator.onSignup(accountData);
- if (accountErrors) {
- return res.status(422).json({
- success: false,
- errors: accountErrors.details,
- });
- }
- const userData = _.pick(req.body, ["email", "password"]);
- userData.language = req.body.language || process.env.DEFAULT_LOCALE;
- const userErrors = await UserValidator.onSignup(userData);
- if (userErrors) {
- return res.status(422).json({
- success: false,
- errors: userErrors.details,
- });
- }
- console.log(
- "SIGNUP_WITH_ACTIVATE ->",
- process.env.SIGNUP_WITH_ACTIVATE,
- process.env.SIGNUP_WITH_ACTIVATE === "true",
- process.env.SIGNUP_WITH_ACTIVATE === "false"
- );
- if (process.env.SIGNUP_WITH_ACTIVATE === "true") {
- const signupResponse = await AuthService.signupWithActivate(
- accountData,
- userData
- );
- return res.json(signupResponse);
- } else {
- const signupResponse = await AuthService.signup(accountData, userData);
- return res.json(signupResponse);
- }
- }
- async manualSignup(req, res, next) {
- const accountData = _.pick(req.body, ["subdomain"]);
- accountData.subdomain = slugify(accountData.subdomain);
- const userData = _.pick(req.body, ["email", "password"]);
- userData.active = true;
- const { account, user } = await AuthService.signup(accountData, userData);
- const accountCompanyData = _.pick(req.body, [
- "companyName",
- "companyVat",
- "companyBillingAddress",
- "companySdi",
- "companyPhone",
- "companyEmail",
- "companyPec",
- ]);
- await AccountService.update(account.id, accountCompanyData);
- await AccountService.activate(user);
- return res.json({
- success: true,
- message: "created",
- });
- }
- async activateAccount(req, res) {
- const userErrors = await UserValidator.onActivate(req.body);
- if (userErrors) {
- return res.status(422).json({
- success: false,
- errors: userErrors.details,
- });
- }
- const user = await AuthService.activate(req.body.token, req.body.email);
- if (!user) {
- return res.status(404).json({
- success: false,
- message: "Failed to activate account - No account found",
- });
- }
- return res.json({
- success: true,
- });
- }
- async resendActivation(req, res) {
- const errors = await UserValidator.onResendActivation(req.body);
- if (errors) {
- return res.status(422).json({
- success: false,
- errors: errors.details,
- });
- }
- const user = await AuthService.resendActivation(req.body.email);
- if (!user) {
- return res.status(404).json({
- success: false,
- message: "Account not found or already activated",
- });
- }
- return res.json({
- success: true,
- });
- }
- async login(req, res) {
- const error = await UserValidator.onLogin(req.body);
- if (error) {
- return res.status(422).json(error.details);
- }
- const token = await AuthService.login(req.body.email, req.body.password);
- if (token) {
- return res.json({
- success: true,
- message: "Enjoy your tokens!",
- token: token,
- });
- } else {
- return res.status(401).json({ message: "Email or password invalid" });
- }
- }
- async forgotPassword(req, res, next) {
- const errors = await UserValidator.forgotPassword(req.body);
- if (errors) {
- return res.status(422).json({
- success: false,
- message: "Please use a valid e-mail address!",
- errrors: errors.details,
- });
- }
- const user = await AuthService.forgotPassword(req.body.email);
- if (!user) {
- return res.status(404).json({ message: "Email not found" });
- }
- return res.json({
- success: true,
- message: "We sent You an email with link to change Your password.",
- });
- }
- async resetPassword(req, res, next) {
- const errors = await UserValidator.onResetPassword(req.body);
- if (errors) {
- return res.status(422).json({
- success: false,
- message: "Failed to update password",
- errors: errors.details,
- });
- }
- const done = await AuthService.resetPassword(
- req.body.passwordResetToken,
- req.body.password,
- req.body.email
- );
- if (done) {
- return res.json({
- success: true,
- message: "Successfully changed password!",
- });
- } else {
- return res.status(422).json({
- success: false,
- message: "Failed to find user with the provided reset password token.",
- });
- }
- }
- async refreshToken(req, res) {
- const token = await AuthService.login(req.user.email, null, true);
- if (token) {
- return res.json({
- success: true,
- message: "Enjoy your tokens!",
- token: token,
- });
- }
- }
- async deleteRefreshToken(req, res) {
- if (
- await AuthService.checkRefreshToken(req.body.email, req.body.refreshToken)
- ) {
- await AuthService.deleteToken(req.body.email);
- return res.json({
- success: true,
- message: "Refresh token deleted.",
- });
- }
- return res.status(401).json({ message: "Email or refresh token invalid" });
- }
- async ssoLogin(req, res) {
- const error = await UserValidator.onSso(req.body);
- if (error) {
- return res.status(422).json(error.details);
- }
- const tokens = await AuthService.ssoLogin(req.body.sso);
- if (tokens) {
- return res.json({
- success: true,
- message: "Enjoy your tokens!",
- token: tokens.token,
- refreshToken: tokens.refreshToken,
- });
- } else {
- return res.status(401).json({ message: "Email or sso invalid" });
- }
- }
- }
- export default new Controller();
|