diff --git a/src/app/controllers/auth.ts b/src/app/controllers/auth.ts deleted file mode 100644 index cbd6e3a29..000000000 --- a/src/app/controllers/auth.ts +++ /dev/null @@ -1,441 +0,0 @@ -const crypto = require('crypto') -const requestPromise = require('request-promise') -const secrets = require('../../config/secrets') -const user = require('../../modules/users') -const models = require('../../models') -const task = require('../../modules/tasks') -const Sendmail = require('../../modules/mail/mail') -const UserMail = require('../../modules/mail/user') -const passport = require('passport') - -export const register = async (req: any, res: any) => { - const { email, name, password } = req.body - if (name?.length > 72) return res.status(401).send({ message: 'user.name.too.long' }) - if (email?.length > 72) return res.status(401).send({ message: 'user.email.too.long' }) - if (password?.length > 72) return res.status(401).send({ message: 'user.password.too.long' }) - try { - const userData = await user.userExists({ email }) - if (userData.dataValues && userData.dataValues.email) { - res.status(403).send({ message: 'user.exist' }) - return - } - try { - const data = await user.userBuilds(req.body) - res.send(data) - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.send(false) - } - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.send(false) - } -} - -export const forgotPasswordNotification = async (req: any, res: any) => { - const { email } = req.body - try { - const foundUser = await user.userExists({ email }) - if (foundUser.dataValues && foundUser.dataValues.email) { - const token = models.User.generateToken() - await models.User.update({ recover_password_token: token }, { where: { email } }) - const url = `${process.env.FRONTEND_HOST}/#/reset-password/${token}` - const html = `
Hi ${foundUser.dataValues.name || 'Gitpay user'},
Click here to reset your password.
` - const subject = 'Reset Password' - const message = { - to: foundUser.dataValues, - subject, - html - } - Sendmail.success(message.to, message.subject, message.html) - res.send(true) - } else { - res.status(403).send({ message: 'user.not.exist' }) - } - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.send(false) - } -} - -export const resetPassword = async (req: any, res: any) => { - try { - const foundUser = await models.User.findOne({ - where: { recover_password_token: req.body.token } - }) - if (!foundUser) res.status(401) - const passwordHash = models.User.generateHash(req.body.password) - if (passwordHash) { - await models.User.update( - { password: passwordHash, recover_password_token: null }, - { where: { id: foundUser.dataValues.id } } - ) - res.send('successfully change password') - } else { - res.status(401).send({ message: 'user.no.password.reset' }) - } - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.send(false) - } -} - -export const changePassword = async (req: any, res: any) => { - try { - const data = await user.userChangePassword({ ...req.body, id: req.user.id }) - res.send(data) - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.status(400).send({ error: error.message }) - } -} - -export const createPrivateTask = async (req: any, res: any) => { - const { url, code, userId } = req.query - const githubClientId = secrets.github.id - const githubClientSecret = secrets.github.secret - try { - const response = await requestPromise({ - method: 'POST', - uri: 'https://github.com/login/oauth/access_token/', - headers: { - 'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0', - Authorization: - 'Basic ' + Buffer.from(`${githubClientId}:${githubClientSecret}`).toString('base64') - }, - body: { - code - }, - json: true - }) - if (response.access_token) { - try { - const taskResult = await task.taskBuilds({ - provider: 'github', - private: true, - userId, - url, - token: response.access_token - }) - res.redirect(`${process.env.FRONTEND_HOST}/#/task/${taskResult.id}`) - // return res.send(data) - } catch (error: any) { - // eslint-disable-next-line no-console - console.log('Error on import private task', error) - const errorStatus = - error?.error?.status ?? - error?.status ?? - error?.statusCode ?? - error?.response?.status ?? - error?.response?.statusCode - const errorMessage = error?.message || error?.error?.message - const isRateLimit = - String(errorStatus) === '403' || /rate limit exceeded/i.test(errorMessage || '') - const finalError = isRateLimit ? 'API limit reached, please try again later.' : errorMessage - const encodedError = encodeURIComponent(finalError || 'We could not import the issue.') - return res.redirect( - `${process.env.FRONTEND_HOST}/#/profile?createTaskError=true&message=${encodedError}` - ) - } - } - return res.status(response.access_token ? 200 : 401).send(response) - } catch (e: any) { - return res.status(401).send(e) - } -} - -export const activate_user = async (req: any, res: any) => { - const { token, userId } = req.query - try { - const foundUser = await models.User.findOne({ where: { activation_token: token, id: userId } }) - if (!foundUser) { - res.status(401).send({ message: 'user.not.exist' }) - } else { - const userUpdate = await models.User.update( - { activation_token: null, email_verified: true }, - { where: { id: foundUser.dataValues.id }, returning: true, plain: true } - ) - res.send(userUpdate[1]) - } - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.status(401).send(error) - } -} - -export const resend_activation_email = async (req: any, res: any) => { - const { id: userId } = req.user - try { - const foundUser = await models.User.findOne({ where: { id: userId } }) - if (!foundUser) res.status(401) - const { id, name } = foundUser.dataValues - const token = models.User.generateToken() - const userUpdate = - token && - (await models.User.update( - { activation_token: token, email_verified: false }, - { where: { id: foundUser.dataValues.id }, returning: true, plain: true } - )) - if (userUpdate[1].dataValues.id) { - UserMail.activation(userUpdate[1].dataValues, token) - } - res.send(userUpdate[1]) - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - } -} - -export const authorizeGithubPrivateIssue = (req: any, res: any) => { - const params = req.query - const uri = encodeURIComponent( - `${process.env.API_HOST}/callback/github/private?userId=${params.userId}&url=${params.url}` - ) - res.redirect( - `https://github.com/login/oauth/authorize?response_type=code&redirect_uri=${uri}&scope=repo&client_id=${secrets.github.id}` - ) -} - -export const disconnectGithub = async (req: any, res: any) => { - try { - const data = await user.userDisconnectGithub({ userId: req.user.id }) - if (data) { - res.redirect(`${process.env.FRONTEND_HOST}/#/profile/user-account/?disconnectAction=success`) - } else { - res.redirect(`${process.env.FRONTEND_HOST}/#/profile/user-account/?disconnectAction=error`) - } - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.redirect(`${process.env.FRONTEND_HOST}/#/profile/user-account/?disconnectAction=error`) - } -} - -export const preferences = async (req: any, res: any) => { - try { - const data = await user.userPreferences({ id: req.user.id }) - res.send(data) - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.send(false) - } -} - -export const organizations = async (req: any, res: any) => { - try { - const data = await user.userOrganizations({ id: req.user.id }) - res.send(data) - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.send(false) - } -} - -export const customer = async (req: any, res: any) => { - if (!req.user) { - return res.status(401).json({ error: 'Unauthorized' }) - } - try { - const data = await user.userCustomer({ id: req.user.id }) - res.send(data) - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.send(false) - } -} - -export const customerCreate = async (req: any, res: any) => { - try { - const data = await user.userCustomerCreate(req.user.id, req.body) - res.send(data) - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.send(false) - } -} - -export const customerUpdate = async (req: any, res: any) => { - try { - const data = await user.userCustomerUpdate(req.user.id, req.body) - res.send(data) - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.send(false) - } -} - -export const account = async (req: any, res: any) => { - try { - const data = await user.userAccount({ id: req.user.id }) - res.send(data) - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.send(false) - } -} - -export const accountCreate = async (req: any, res: any) => { - req.body.id = req.user.id - try { - const data = await user.userAccountCreate(req.body) - res.send(data) - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.send(false) - } -} - -export const accountCountries = async (req: any, res: any) => { - try { - const data = await user.userAccountCountries({ id: req.user.id }) - res.send(data) - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.send(false) - } -} - -export const accountBalance = async (req: any, res: any) => { - try { - const data = await user.userAccountBalance({ account_id: req.user.account_id }) - res.send(data) - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.send(false) - } -} - -export const accountUpdate = async (req: any, res: any) => { - try { - const data = await user.userAccountUpdate({ userParams: req.user, accountParams: req.body }) - res.send(data) - } catch (error: any) { - // eslint-disable-next-line no-console - console.log('error on account update', error) - res.status(401).send(error) - } -} - -export const accountDelete = async (req: any, res: any) => { - try { - const data = await user.userAccountDelete({ userId: req.user.id }) - res.send(data) - } catch (error: any) { - // eslint-disable-next-line no-console - console.log('error on account delete', error) - res.status(401).send(error) - } -} - -export const userFetch = async (req: any, res: any) => { - const userId = req.user.id - try { - const data = await user.userFetch(userId) - res.status(200).send(data) - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.status(400).send(error) - } -} - -export const createBankAccount = async (req: any, res: any) => { - try { - const data = await user.userBankAccountCreate({ - userParams: req.user, - bankAccountParams: req.body - }) - res.send(data) - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.send(error) - } -} - -export const updateBankAccount = async (req: any, res: any) => { - try { - const data = await user.userBankAccountUpdate({ userParams: req.user, bank_account: req.body }) - res.send(data) - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.send(error) - } -} - -export const userBankAccount = async (req: any, res: any) => { - try { - const data = await user.userBankAccount({ id: req.user.id }) - res.send(data) - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.send(error) - } -} - -export const deleteUserById = async (req: any, res: any) => { - const params = { id: req.user.id } - try { - const deleted = await user.userDeleteById(params) - res.status(200).send(`${deleted}`) - } catch (error: any) { - // eslint-disable-next-line no-console - console.log(error) - res.status(400).send(error) - } -} - -export const callbackGithub = (req: any, res: any) => { - const user = req.user - if (user && user.token) { - if (user.login_strategy === 'local' || user.login_strategy === null) { - res.redirect( - `${process.env.FRONTEND_HOST}/#/profile/user-account/?connectGithubAction=success` - ) - } else { - res.redirect(`${process.env.FRONTEND_HOST}/#/token/${user.token}`) - } - } -} - -export const connectGithub = (req: any, res: any, next: any) => { - const user = req.user - if (user) { - passport.authenticate('github', { - scope: ['user:email'], - state: req.user.email - })(req, res, next) - } else { - res.redirect(`${process.env.FRONTEND_HOST}/#/signin/invalid`) - } -} - -export const callbackBitbucket = (req: any, res: any) => { - if (req.user && req.user.token) { - res.redirect(`${process.env.FRONTEND_HOST}/#/token/` + req.user.token) - } -} - -export const authorizeLocal = (req: any, res: any, next: any) => { - if (req.user && req.user.token) { - res.set('Authorization', 'Bearer ' + req.user.token) - res.redirect(`${process.env.FRONTEND_HOST}/#/token/${req.user.token}`) - } -} diff --git a/src/app/controllers/auth/auth.ts b/src/app/controllers/auth/auth.ts index f41f6bc72..f40ff2496 100644 --- a/src/app/controllers/auth/auth.ts +++ b/src/app/controllers/auth/auth.ts @@ -1,5 +1,9 @@ import { userChangeEmail } from '../../../modules/users/userChangeEmail' import { userConfirmChangeEmail } from '../../../modules/users/userConfirmChangeEmail' +import userDisconnectGithub from '../../../modules/users/userDisconectGithub' +import secrets from '../../../config/secrets' +import * as user from '../../../modules/users' +import passport from 'passport' export const changeEmail = async (req: any, res: any) => { const userId = req.user.id @@ -36,3 +40,47 @@ export const confirmChangeEmail = async (req: any, res: any) => { res.redirect(`${process.env.FRONTEND_HOST}/#/signin/email-change-failed`) } } + +export const authorizeGithubPrivateIssue = (req: any, res: any) => { + const params = req.query + const uri = encodeURIComponent( + `${process.env.API_HOST}/callback/github/private?userId=${params.userId}&url=${params.url}` + ) + res.redirect( + `https://github.com/login/oauth/authorize?response_type=code&redirect_uri=${uri}&scope=repo&client_id=${secrets.github.id}` + ) +} + +export const disconnectGithub = async (req: any, res: any) => { + try { + const data = await userDisconnectGithub({ userId: req.user.id }) + if (data) { + res.redirect(`${process.env.FRONTEND_HOST}/#/profile/user-account/?disconnectAction=success`) + } else { + res.redirect(`${process.env.FRONTEND_HOST}/#/profile/user-account/?disconnectAction=error`) + } + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.redirect(`${process.env.FRONTEND_HOST}/#/profile/user-account/?disconnectAction=error`) + } +} + +export const connectGithub = (req: any, res: any, next: any) => { + const user = req.user + if (user) { + passport.authenticate('github', { + scope: ['user:email'], + state: req.user.email + })(req, res, next) + } else { + res.redirect(`${process.env.FRONTEND_HOST}/#/signin/invalid`) + } +} + +export const authorizeLocal = (req: any, res: any, next: any) => { + if (req.user && req.user.token) { + res.set('Authorization', 'Bearer ' + req.user.token) + res.redirect(`${process.env.FRONTEND_HOST}/#/token/${req.user.token}`) + } +} diff --git a/src/app/controllers/auth/callbacks.ts b/src/app/controllers/auth/callbacks.ts new file mode 100644 index 000000000..9880f4fe6 --- /dev/null +++ b/src/app/controllers/auth/callbacks.ts @@ -0,0 +1,18 @@ +export const callbackGithub = (req: any, res: any) => { + const user = req.user + if (user && user.token) { + if (user.login_strategy === 'local' || user.login_strategy === null) { + res.redirect( + `${process.env.FRONTEND_HOST}/#/profile/user-account/?connectGithubAction=success` + ) + } else { + res.redirect(`${process.env.FRONTEND_HOST}/#/token/${user.token}`) + } + } +} + +export const callbackBitbucket = (req: any, res: any) => { + if (req.user && req.user.token) { + res.redirect(`${process.env.FRONTEND_HOST}/#/token/` + req.user.token) + } +} diff --git a/src/app/controllers/user/account.ts b/src/app/controllers/user/account.ts new file mode 100644 index 000000000..3b100e067 --- /dev/null +++ b/src/app/controllers/user/account.ts @@ -0,0 +1,68 @@ +import * as user from '../../../modules/users' + +export const account = async (req: any, res: any) => { + try { + const data = await user.userAccount({ id: req.user.id }) + res.send(data) + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.send(false) + } +} + +export const accountCreate = async (req: any, res: any) => { + req.body.id = req.user.id + try { + const data = await user.userAccountCreate(req.body) + res.send(data) + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.send(false) + } +} + +export const accountCountries = async (req: any, res: any) => { + try { + const data = await user.userAccountCountries({ id: req.user.id }) + res.send(data) + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.send(false) + } +} + +export const accountBalance = async (req: any, res: any) => { + try { + const data = await user.userAccountBalance({ account_id: req.user.account_id }) + res.send(data) + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.send(false) + } +} + +export const accountUpdate = async (req: any, res: any) => { + try { + const data = await user.userAccountUpdate({ userParams: req.user, accountParams: req.body }) + res.send(data) + } catch (error: any) { + // eslint-disable-next-line no-console + console.log('error on account update', error) + res.status(401).send(error) + } +} + +export const accountDelete = async (req: any, res: any) => { + try { + const data = await user.userAccountDelete({ userId: req.user.id }) + res.send(data) + } catch (error: any) { + // eslint-disable-next-line no-console + console.log('error on account delete', error) + res.status(401).send(error) + } +} diff --git a/src/app/controllers/user/bank-account.ts b/src/app/controllers/user/bank-account.ts new file mode 100644 index 000000000..efc4b2395 --- /dev/null +++ b/src/app/controllers/user/bank-account.ts @@ -0,0 +1,37 @@ +import * as user from '../../../modules/users' + +export const createBankAccount = async (req: any, res: any) => { + try { + const data = await user.userBankAccountCreate({ + userParams: req.user, + bankAccountParams: req.body + }) + res.send(data) + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.send(error) + } +} + +export const updateBankAccount = async (req: any, res: any) => { + try { + const data = await user.userBankAccountUpdate({ userParams: req.user, bank_account: req.body }) + res.send(data) + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.send(error) + } +} + +export const userBankAccount = async (req: any, res: any) => { + try { + const data = await user.userBankAccount({ id: req.user.id }) + res.send(data) + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.send(error) + } +} diff --git a/src/app/controllers/user/customers.ts b/src/app/controllers/user/customers.ts new file mode 100644 index 000000000..154f922c1 --- /dev/null +++ b/src/app/controllers/user/customers.ts @@ -0,0 +1,37 @@ +import * as user from '../../../modules/users' + +export const customer = async (req: any, res: any) => { + if (!req.user) { + return res.status(401).json({ error: 'Unauthorized' }) + } + try { + const data = await user.userCustomer({ id: req.user.id }) + res.send(data) + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.send(false) + } +} + +export const customerCreate = async (req: any, res: any) => { + try { + const data = await user.userCustomerCreate(req.user.id, req.body) + res.send(data) + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.send(false) + } +} + +export const customerUpdate = async (req: any, res: any) => { + try { + const data = await user.userCustomerUpdate(req.user.id, req.body) + res.send(data) + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.send(false) + } +} diff --git a/src/app/controllers/user/user.ts b/src/app/controllers/user/user.ts index 55db1233e..1a4b3bb75 100644 --- a/src/app/controllers/user/user.ts +++ b/src/app/controllers/user/user.ts @@ -1,5 +1,15 @@ import userInfo from '../../../modules/users/userInfo' import userUpdate from '../../../modules/users/userUpdate' +import crypto from 'crypto' +import requestPromise from 'request-promise' +import secrets from '../../../config/secrets' +import * as user from '../../../modules/users' +import Models from '../../../models' +import * as task from '../../../modules/tasks' +import Sendmail from '../../../modules/mail/mail' +import UserMail from '../../../modules/mail/user' + +const models = Models as any export const getUserInfo = async (req: any, res: any) => { const userId = req.user.id @@ -25,3 +35,233 @@ export const updateUser = (req: any, res: any) => { res.status(status).json({ error: message }) }) } + +export const register = async (req: any, res: any) => { + const { email, name, password } = req.body + if (name?.length > 72) return res.status(401).send({ message: 'user.name.too.long' }) + if (email?.length > 72) return res.status(401).send({ message: 'user.email.too.long' }) + if (password?.length > 72) return res.status(401).send({ message: 'user.password.too.long' }) + try { + const userData = await user.userExists({ email }) + if (userData.dataValues && userData.dataValues.email) { + res.status(403).send({ message: 'user.exist' }) + return + } + try { + const data = await user.userBuilds(req.body) + res.send(data) + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.send(false) + } + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.send(false) + } +} + +export const forgotPasswordNotification = async (req: any, res: any) => { + const { email } = req.body + try { + const foundUser = await user.userExists({ email }) + if (foundUser.dataValues && foundUser.dataValues.email) { + const token = models.User.generateToken() + await models.User.update({ recover_password_token: token }, { where: { email } }) + const url = `${process.env.FRONTEND_HOST}/#/reset-password/${token}` + const html = `Hi ${foundUser.dataValues.name || 'Gitpay user'},
Click here to reset your password.
` + const subject = 'Reset Password' + const message = { + to: foundUser.dataValues, + subject, + html + } + Sendmail.success(message.to, message.subject, message.html) + res.send(true) + } else { + res.status(403).send({ message: 'user.not.exist' }) + } + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.send(false) + } +} + +export const resetPassword = async (req: any, res: any) => { + try { + const foundUser = await models.User.findOne({ + where: { recover_password_token: req.body.token } + }) + if (!foundUser) res.status(401) + const passwordHash = models.User.generateHash(req.body.password) + if (passwordHash) { + await models.User.update( + { password: passwordHash, recover_password_token: null }, + { where: { id: foundUser.dataValues.id } } + ) + res.send('successfully change password') + } else { + res.status(401).send({ message: 'user.no.password.reset' }) + } + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.send(false) + } +} + +export const changePassword = async (req: any, res: any) => { + try { + const data = await user.userChangePassword({ ...req.body, id: req.user.id }) + res.send(data) + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.status(400).send({ error: error.message }) + } +} + +export const createPrivateTask = async (req: any, res: any) => { + const { url, code, userId } = req.query + const githubClientId = secrets.github.id + const githubClientSecret = secrets.github.secret + try { + const response = await requestPromise({ + method: 'POST', + uri: 'https://github.com/login/oauth/access_token/', + headers: { + 'User-Agent': 'octonode/0.3 (https://github.com/pksunkara/octonode) terminal/0.0', + Authorization: + 'Basic ' + Buffer.from(`${githubClientId}:${githubClientSecret}`).toString('base64') + }, + body: { + code + }, + json: true + }) + if (response.access_token) { + try { + const taskResult = await task.taskBuilds({ + provider: 'github', + private: true, + userId, + url, + token: response.access_token + }) + res.redirect(`${process.env.FRONTEND_HOST}/#/task/${taskResult.id}`) + // return res.send(data) + } catch (error: any) { + // eslint-disable-next-line no-console + console.log('Error on import private task', error) + const errorStatus = + error?.error?.status ?? + error?.status ?? + error?.statusCode ?? + error?.response?.status ?? + error?.response?.statusCode + const errorMessage = error?.message || error?.error?.message + const isRateLimit = + String(errorStatus) === '403' || /rate limit exceeded/i.test(errorMessage || '') + const finalError = isRateLimit ? 'API limit reached, please try again later.' : errorMessage + const encodedError = encodeURIComponent(finalError || 'We could not import the issue.') + return res.redirect( + `${process.env.FRONTEND_HOST}/#/profile?createTaskError=true&message=${encodedError}` + ) + } + } + return res.status(response.access_token ? 200 : 401).send(response) + } catch (e: any) { + return res.status(401).send(e) + } +} + +export const activateUser = async (req: any, res: any) => { + const { token, userId } = req.query + try { + const foundUser = await models.User.findOne({ where: { activation_token: token, id: userId } }) + if (!foundUser) { + res.status(401).send({ message: 'user.not.exist' }) + } else { + const userUpdate = await models.User.update( + { activation_token: null, email_verified: true }, + { where: { id: foundUser.dataValues.id }, returning: true, plain: true } + ) + res.send(userUpdate[1]) + } + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.status(401).send(error) + } +} + +export const resendActivationEmail = async (req: any, res: any) => { + const { id: userId } = req.user + try { + const foundUser = await models.User.findOne({ where: { id: userId } }) + if (!foundUser) res.status(401) + const { id, name } = foundUser.dataValues + const token = models.User.generateToken() + const userUpdate = + token && + (await models.User.update( + { activation_token: token, email_verified: false }, + { where: { id: foundUser.dataValues.id }, returning: true, plain: true } + )) + if (userUpdate[1].dataValues.id) { + UserMail.activation(userUpdate[1].dataValues, token) + } + res.send(userUpdate[1]) + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + } +} + +export const preferences = async (req: any, res: any) => { + try { + const data = await user.userPreferences({ id: req.user.id }) + res.send(data) + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.send(false) + } +} + +export const organizations = async (req: any, res: any) => { + try { + const data = await user.userOrganizations({ id: req.user.id }) + res.send(data) + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.send(false) + } +} + +export const userFetch = async (req: any, res: any) => { + const userId = req.user.id + try { + const data = await user.userFetch(userId) + res.status(200).send(data) + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.status(400).send(error) + } +} + +export const deleteUserById = async (req: any, res: any) => { + const params = { id: req.user.id } + try { + const deleted = await user.userDeleteById(params) + res.status(200).send(`${deleted}`) + } catch (error: any) { + // eslint-disable-next-line no-console + console.log(error) + res.status(400).send(error) + } +} diff --git a/src/app/routes/auth/auth.ts b/src/app/routes/auth/auth.ts index 67129a56c..9a92663c7 100644 --- a/src/app/routes/auth/auth.ts +++ b/src/app/routes/auth/auth.ts @@ -1,6 +1,6 @@ import express from 'express' import * as authenticationHelpers from '../../../modules/authenticationHelpers' -import * as controllers from '../../controllers/auth' +import { register, forgotPasswordNotification, resetPassword, changePassword, activateUser, resendActivationEmail } from '../../controllers/user/user' import { changeEmail } from '../../controllers/auth/auth' import { confirmChangeEmail } from '../../controllers/auth/auth' import secure from '../secure' @@ -8,16 +8,16 @@ import secure from '../secure' const router = express.Router() router.get('/authenticated', authenticationHelpers.isAuth) -router.put('/auth/reset-password', controllers.resetPassword) -router.post('/auth/forgot-password', controllers.forgotPasswordNotification) +router.put('/auth/reset-password', resetPassword) +router.post('/auth/forgot-password', forgotPasswordNotification) -router.post('/auth/register', controllers.register) -router.get('/auth/activate', controllers.activate_user) +router.post('/auth/register', register) +router.get('/auth/activate', activateUser) router.get('/auth/change-email/confirm', confirmChangeEmail) router.use('/auth/', secure) -router.put('/auth/change-password', controllers.changePassword) -router.get('/auth/resend-activation-email', controllers.resend_activation_email) +router.put('/auth/change-password', changePassword) +router.get('/auth/resend-activation-email', resendActivationEmail) router.post('/auth/change-email', changeEmail) export default router diff --git a/src/app/routes/auth/providers.ts b/src/app/routes/auth/providers.ts index 45a1c61bf..c171218d1 100644 --- a/src/app/routes/auth/providers.ts +++ b/src/app/routes/auth/providers.ts @@ -1,7 +1,9 @@ import express from 'express' import passport from 'passport' import * as authenticationHelpers from '../../../modules/authenticationHelpers' -import * as controllers from '../../controllers/auth' +import { authorizeGithubPrivateIssue, disconnectGithub, connectGithub, authorizeLocal } from '../../controllers/auth/auth' +import { callbackGithub, callbackBitbucket } from '../../controllers/auth/callbacks' +import { createPrivateTask } from '../../controllers/user/user' import secure from '../secure' const router = express.Router() @@ -31,21 +33,21 @@ router.get('/authorize/github', passport.authenticate('github', { scope: ['user: router.get( '/callback/github', passport.authenticate('github', { failureRedirect: `/` }), - controllers.callbackGithub + callbackGithub ) -router.get('/callback/github/private', controllers.createPrivateTask) +router.get('/callback/github/private', createPrivateTask) -router.get('/connect/github', secure, controllers.connectGithub) +router.get('/connect/github', secure, connectGithub) -router.get('/authorize/github/private', controllers.authorizeGithubPrivateIssue) -router.get('/authorize/github/disconnect', secure, controllers.disconnectGithub) +router.get('/authorize/github/private', authorizeGithubPrivateIssue) +router.get('/authorize/github/disconnect', secure, disconnectGithub) router.get('/authorize/bitbucket', passport.authenticate('bitbucket', { scope: ['email'] })) router.get( '/callback/bitbucket', passport.authenticate('bitbucket', { failureRedirect: '/' }), - controllers.callbackBitbucket + callbackBitbucket ) router.post( @@ -56,7 +58,7 @@ router.post( failureRedirect: `${process.env.FRONTEND_HOST}/#/signin/invalid` // successRedirect: `${process.env.FRONTEND_HOST}/#/token/${req?.user?.token}` }), - controllers.authorizeLocal + authorizeLocal ) export default router diff --git a/src/app/routes/user.ts b/src/app/routes/user.ts index d7df8a5fc..12cf33e72 100644 --- a/src/app/routes/user.ts +++ b/src/app/routes/user.ts @@ -1,5 +1,8 @@ import express from 'express' -import * as controllers from '../controllers/auth' +import { userFetch, preferences, organizations, deleteUserById } from '../controllers/user/user' +import { customer, customerCreate, customerUpdate } from '../controllers/user/customers' +import { account, accountCreate, accountCountries, accountBalance, accountUpdate, accountDelete } from '../controllers/user/account' +import { createBankAccount, updateBankAccount, userBankAccount } from '../controllers/user/bank-account' import secure from './secure' import { updateUser } from '../controllers/user/user' @@ -7,28 +10,28 @@ const router = express.Router() router.use('/', secure) -router.get('/', controllers.userFetch) +router.get('/', userFetch) router.put('/', updateUser) -router.post('/customer', controllers.customerCreate) -router.get('/customer', controllers.customer) -router.put('/customer', controllers.customerUpdate) +router.post('/customer', customerCreate) +router.get('/customer', customer) +router.put('/customer', customerUpdate) -router.get('/preferences', controllers.preferences) -router.get('/organizations', controllers.organizations) +router.get('/preferences', preferences) +router.get('/organizations', organizations) -router.post('/account', controllers.accountCreate) -router.get('/account', controllers.account) -router.put('/account', controllers.accountUpdate) -router.delete('/account', controllers.accountDelete) +router.post('/account', accountCreate) +router.get('/account', account) +router.put('/account', accountUpdate) +router.delete('/account', accountDelete) -router.get('/account/balance', controllers.accountBalance) -router.get('/account/countries', controllers.accountCountries) +router.get('/account/balance', accountBalance) +router.get('/account/countries', accountCountries) -router.post('/bank_accounts', controllers.createBankAccount) -router.get('/bank_accounts', controllers.userBankAccount) -router.put('/bank_accounts', controllers.updateBankAccount) +router.post('/bank_accounts', createBankAccount) +router.get('/bank_accounts', userBankAccount) +router.put('/bank_accounts', updateBankAccount) -router.delete('/delete', controllers.deleteUserById) +router.delete('/delete', deleteUserById) export default router