Skip to content

Commit

Permalink
#23 | fix conflict dev
Browse files Browse the repository at this point in the history
  • Loading branch information
leophan07 committed Dec 1, 2020
2 parents 7f35b7e + 5ed6ec4 commit 16f8b52
Show file tree
Hide file tree
Showing 25 changed files with 195 additions and 119 deletions.
10 changes: 6 additions & 4 deletions api/.env-example
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
PORT=5000

FRONTEND_URL=http://localhost:3000


DATABASE_NAME=saasgear
DATABASE_HOST=localhost
DATABASE_PASSWORD=admin
DATABASE_USER=root
DATABASE_PORT=3306

DB_HOST_OUTSIZE_DOCKER=localhost
DB_PORT_OUTSIZE_DOCKER=33061

JWT_SECRET=JWT_SECRET
JWT_ISSUER=JSLANCER
JWT_SUBJECT=JSLANCER
Expand All @@ -28,4 +27,7 @@ GITHUB_SECRET_KEY=
GITHUB_CLIENT_KEY=

FACEBOOK_CLIENT_KEY=
FACEBOOK_CLIENT_SECRET_KEY=
FACEBOOK_CLIENT_SECRET_KEY=

GOOGLE_CLIENT_KEY=
GOOGLE_SECRET_KEY=
69 changes: 33 additions & 36 deletions api/README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
### Struct ture Backend APP

| caches (all cache providers here, Such as redis, memcache, etc)
     | redis.cache.js (redis cache provider config or functionality)
     | memcache.cache.js (redis cache provider config or functionality)
| configs
     | knexfile.js (config for knex)
| constants (all constants for application)
     | status.constant.js (file constant of http status code)
| database
     | db.config.js (Config database)
| docs (All documents here, such as Postman collection, schema docs, mutation, query, etc)
| graphql
     | resolvers (All graphql resolver)
          | user.resolver.js -> resolver of user feature
     | scalars (custom scalars for graphql type and input)
          | date.scalar.js -> custom date scalar
     | schemas (graphql schemas)
          | user.schema.js -> schema for feature user
     | root.resolver.js -> this file combine all resolver
     | root.schema.js -> this file combine all schema
| helpers (Helpers function)
| libs (implement libs here)
| logs (logs to tracing error)
| middlewares (define all middlewares of rest or graphql here)
| migrations (migrations database, can contain seed db)
| public (all static file of backend app, images, video ....)
| queue (queue handler)
| repository (interact with databases or db schema)
| services (all logic of a request here)
| utils (utils for support validation, logger, etc.)
.env
.env-example
package.json
README.md
| caches (all cache providers here, Such as redis, memcache, etc)
     | redis.cache.js (redis cache provider config or functionality)
     | memcache.cache.js (redis cache provider config or functionality)
| configs
     | knexfile.js (config for knex)
| constants (all constants for application)
     | status.constant.js (file constant of http status code)
| database
     | db.config.js (Config database)
| docs (All documents here, such as Postman collection, schema docs, mutation, query, etc)
| graphql
     | resolvers (All graphql resolver)
          | user.resolver.js -> resolver of user feature
     | scalars (custom scalars for graphql type and input)
          | date.scalar.js -> custom date scalar
     | schemas (graphql schemas)
          | user.schema.js -> schema for feature user
     | root.resolver.js -> this file combine all resolver
     | root.schema.js -> this file combine all schema
| helpers (Helpers function)
| libs (implement libs here)
| logs (logs to tracing error)
| middlewares (define all middlewares of rest or graphql here)
| migrations (migrations database, can contain seed db)
| public (all static file of backend app, images, video ....)
| queue (queue handler)
| repository (interact with databases or db schema)
| services (all logic of a request here)
| utils (utils for support validation, logger, etc.)
.env
.env-example
package.json
README.md
server.js

![Flow](https://res.cloudinary.com/tuananh-asia/image/upload/v1604460716/ggg_yl8vbk.png)
Expand All @@ -47,9 +47,6 @@ DATABASE_PASSWORD=root
DATABASE_USER=root
DATABASE_PORT=3306
DB_HOST_OUTSIZE_DOCKER=localhost
DB_PORT_OUTSIZE_DOCKER=33061
JWT_SECRET=JWT_SECRET
JWT_ISSUER=JSLANCER
JWT_SUBJECT=JSLANCER
Expand Down
4 changes: 2 additions & 2 deletions api/config/knexfile.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ module.exports = (database, host, user, password, port) => ({
client: 'mysql2',
connection: {
database: database || process.env.DATABASE_NAME,
host: host || process.env.DB_HOST_OUTSIZE_DOCKER,
host: host || process.env.DATABASE_HOST,
user: user || process.env.DATABASE_USER,
password: password || process.env.DATABASE_PASSWORD,
port: port || process.env.DB_PORT_OUTSIZE_DOCKER,
port: port || process.env.DATABASE_PORT,
},
migrations: {
tableName: 'migrations',
Expand Down
4 changes: 4 additions & 0 deletions api/constants/send-mail-type.constant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const SEND_MAIL_TYPE = {
VERIFY_EMAIL: 'verify_email',
FORGOT_PASSWORD: 'forgot_password',
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
</mj-text>
<mj-divider border-color="#06adef" width="100px"></mj-divider>
<mj-text font-size="18px" align="left" color="#555" line-height="30px">
Hi {{name}}! Your password on Saasgear has been changed at {{date}}.
Otherwise you are the one making this change, please check your account on SaaS again.
Hi {{name}}! Your password on SaaSgear has been changed on {{date}}.
If you didn't initialize this change, please check your account on SaaSgear again.
</mj-text>
</mj-column>
</mj-section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</mj-text>
<mj-divider border-color="#06adef" width="100px"></mj-divider>
<mj-text font-size="18px" align="center" color="#555" line-height="30px"
>Hi {{name}}!!! We cannot simply sent you your old password. A unique
>Hi {{name}}! A unique
link to reset your password has been generated for you. To reset your
password, click the following link and follow the
instructions</mj-text
Expand All @@ -27,7 +27,7 @@
font-size="15px"
href="{{url}}"
>
RESET PASSWORD
Reset password
</mj-button>
</mj-column>
</mj-section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
Verify your email address
</mj-text>
<mj-text font-size="18px" align="center" color="#555" line-height="30px"
>Hi {{name}}!!! Please confirm that you want to use this as your
SASSGEAR account email address. Once it's done you will be able to
start selling!</mj-text
>Hi {{name}}! Please confirm that you want to use this as your
SaaSgear account email address.</mj-text
>
<mj-button
background-color="#06adef"
Expand All @@ -25,7 +24,7 @@
font-size="15px"
href="{{url}}"
>
VERIFY MY EMAIL
Verify my email
</mj-button>
<mj-divider border-color="#06adef" width="100px"></mj-divider>
<mj-social icon-size="30px" mode="horizontal">
Expand Down
11 changes: 6 additions & 5 deletions api/graphql/resolvers/user.resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { resetPasswordUser } from '~/services/authentication/reset-password.serv
import { changePasswordUser } from '~/services/authentication/change-password.service';
import { loginSocial } from '~/services/authentication/social-login.service';
import { registerAccountBySocial } from '~/services/authentication/register-social.service';
import { lowerCaseAndTrim } from '~/helpers/string.helper';

const resolvers = {
Query: {
Expand All @@ -17,20 +18,20 @@ const resolvers = {
},
Mutation: {
register(_, { email, password, name, paymentMethodToken, planName, billingType }) {
return registerUser(email, password, name, paymentMethodToken, planName, billingType);
return registerUser(lowerCaseAndTrim(email), password, name, paymentMethodToken, planName, billingType);
},
login(_, { email, password }) {
return loginUser(email, password);
return loginUser(lowerCaseAndTrim(email), password);
},
forgotPassword: async (_, { email }) => forgotPasswordUser(email),
forgotPassword: async (_, { email }) => forgotPasswordUser(lowerCaseAndTrim(email)),
changePassword: combineResolvers(
isAuthenticated,
(_, { currentPassword, newPassword }, { user }) => changePasswordUser(user.id, currentPassword, newPassword),
),
resetPassword: async (_, { token, password, confirmPassword }) => resetPasswordUser(token, password, confirmPassword),
verify: (_, { token }) => verifyEmail(token),
resendEmail: (_, { type }, { user }) => resendEmailAction(user, type.toLowerCase()),
registerSocialAccount: (_, { provider, email, name, avatarUrl, providerId }) => registerAccountBySocial(provider, email, name, avatarUrl, providerId),
resendEmail: (_, { type }, { user }) => resendEmailAction(user, lowerCaseAndTrim(type)),
registerSocialAccount: (_, { provider, email, name, avatarUrl, providerId }) => registerAccountBySocial(provider, lowerCaseAndTrim(email), name, avatarUrl, providerId),
},
};

Expand Down
10 changes: 10 additions & 0 deletions api/helpers/compile-email-template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import handlebars from 'handlebars';
import mjml2html from 'mjml';
import fs from 'fs';
import path from 'path';

export default async function compileEmailTemplate({ fileName, data }) {
const mjMail = await fs.promises.readFile(path.join('email-templates', fileName), 'utf8');
const template = mjml2html(mjMail).html;
return handlebars.compile(template)(data).toString();
}
9 changes: 0 additions & 9 deletions api/helpers/generate-template-email.js

This file was deleted.

3 changes: 3 additions & 0 deletions api/helpers/string.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function lowerCaseAndTrim(text) {
return text.toLowerCase().trim();
}
1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"db:create-products": "node --experimental-modules --es-module-specifier-resolution=node scripts/create-products.js",
"db:migrate": "knex --esm migrate:latest --knexfile config/knexfile.cjs",
"db:rollback": "knex --esm migrate:rollback --knexfile config/knexfile.cjs",
"db:create": "knex --esm migrate:make --knexfile config/knexfile.cjs",
"db:down": "knex --esm migrate:down --knexfile config/knexfile.cjs",
"db:seed": "knex --esm seed:run --knexfile config/knexfile.cjs",
"postinstall": "link-module-alias",
Expand Down
15 changes: 6 additions & 9 deletions api/services/authentication/change-password.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import dayjs from 'dayjs';
import { findUser, updateUser } from '~/repository/user.repository';
import logger from '~/utils/logger';
import sendMail from '~/libs/mail';
import generateTemplateEmail from '~/helpers/generate-template-email';
import compileEmailTemplate from '~/helpers/compile-email-template';
import { comparePassword, generatePassword } from '~/helpers/hashing.helper';
import { changePasswordValidation } from '~/utils/validations/authenticate.validation';

Expand All @@ -18,12 +18,9 @@ export async function changePasswordUser(userId, currentPassword, newPassword) {

const validateResult = changePasswordValidation({ password: newPassword });
if (validateResult.length) {
throw new UserInputError(
validateResult.map((it) => it.message).join(','),
{
invalidArgs: validateResult.map((it) => it.field).join(','),
},
);
throw new UserInputError(validateResult.map((it) => it.message).join(','), {
invalidArgs: validateResult.map((it) => it.field).join(','),
});
}

const matchPassword = await comparePassword(currentPassword, user.password);
Expand All @@ -34,15 +31,15 @@ export async function changePasswordUser(userId, currentPassword, newPassword) {
const passwordHashed = await generatePassword(newPassword);
await updateUser(user.id, { password: passwordHashed });

const template = generateTemplateEmail({
const template = await compileEmailTemplate({
fileName: 'changePassword.mjml',
data: {
name: user.name,
date: dayjs().format('dddd, MMMM D, YYYY h:mm A'),
},
});

await sendMail(user.email, 'Change Password from SAASGEAR', template);
await sendMail(user.email, 'Change Password from SaaSgear', template);
return true;
} catch (error) {
logger.error(error);
Expand Down
19 changes: 7 additions & 12 deletions api/services/authentication/forgot-password.service.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import pkg from 'apollo-server-express';
import {
findUser,
getUserByIdAndJoinUserToken,
} from '~/repository/user.repository';
import { findUser, getUserByIdAndJoinUserToken } from '~/repository/user.repository';
import generateRandomKey from '~/helpers/genarateRandomkey';
import {
createToken,
updateUserTokenById,
} from '~/repository/user_tokens.repository';
import logger from '~/utils/logger';
import sendMail from '~/libs/mail';
import generateTemplateEmail from '~/helpers/generate-template-email';
import compileEmailTemplate from '~/helpers/compile-email-template';
import { SEND_MAIL_TYPE } from '~/constants/send-mail-type.constant';

const { ApolloError } = pkg;

Expand All @@ -20,27 +18,24 @@ export async function forgotPasswordUser(email) {
if (!user || !user.id) {
throw new ApolloError('Can not find any user');
}
const session = await getUserByIdAndJoinUserToken(
user.id,
'forgot_password',
);
const session = await getUserByIdAndJoinUserToken(user.id, SEND_MAIL_TYPE.FORGOT_PASSWORD);
const tokenGenerated = await generateRandomKey();
const token = `${tokenGenerated}-${user.id}`;
if (!session) {
await createToken(user.id, token, 'forgot_password');
await createToken(user.id, token, SEND_MAIL_TYPE.FORGOT_PASSWORD);
} else {
await updateUserTokenById(session.id, token);
}

const template = generateTemplateEmail({
const template = await compileEmailTemplate({
fileName: 'forgotPassword.mjml',
data: {
name: session.name,
url: `${process.env.FRONTEND_URL}/auth/reset-password?&token=${token}`,
},
});

await sendMail(session.email, ' Reset Password from SAASGEAR', template);
await sendMail(session.email, ' Reset Password from SaaSgear', template);
return true;
} catch (error) {
logger.error(error);
Expand Down
Loading

0 comments on commit 16f8b52

Please sign in to comment.