Skip to content

Commit

Permalink
Merge pull request #875 from 2anki/chore/remove-verification
Browse files Browse the repository at this point in the history
chore(users): remove the verification token
  • Loading branch information
aalemayhu authored Oct 30, 2022
2 parents cc19f77 + b9ecea1 commit f462187
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 98 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ RUN node --version

RUN rm -rvf /app/node_modules
RUN git clone https://github.com/2anki/web /app/web
RUN git -C /app/web checkout 2c195d254bef9398db2a32888f570b8bf1e765d8
RUN git clone https://github.com/2anki/create_deck /app/create_deck
RUN npm --prefix /app/web install

Expand Down
13 changes: 13 additions & 0 deletions migrations/20221029071325_remove-verify-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.up = function (knex) {
return knex.schema.table('users', (table) => {
table.dropColumn('verification_token');
table.dropColumn('verified');
});
};

module.exports.down = (knex) => {
return knex.schema.table('users', (table) => {
table.boolean('verified').defaultTo(true);
table.string('verification_token');
});
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"notion2anki"
],
"author": "Alexander Alemayhu",
"version": "1.0.0-alpha.32",
"version": "1.0.0-alpha.35",
"engines": {
"node": ">=12.0.0"
},
Expand Down
19 changes: 0 additions & 19 deletions src/lib/email/EmailHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import fs from 'fs';

const EMAIL_TEMPLATES_DIRECTORY = path.join(__dirname, 'templates');

const VERIFICATION_TEMPLATE = fs.readFileSync(
path.join(EMAIL_TEMPLATES_DIRECTORY, 'verification.html'),
'utf8'
);
const PASSWORD_RESET_TEMPLATE = fs.readFileSync(
path.join(EMAIL_TEMPLATES_DIRECTORY, 'reset.html'),
'utf8'
Expand Down Expand Up @@ -40,21 +36,6 @@ class EmailHandler {
return sgMail.send(msg);
}

static async SendVerificationEmail(email: string, token: string) {
const link = `${process.env.DOMAIN}/api/users/v/${token}`;
const markup = VERIFICATION_TEMPLATE.replace('{{link}}', link);
const msg = {
to: email,
from: DEFAULT_SENDER,
subject: 'Verify your 2anki.net account',
text: `Please verify your account by visiting the following link ${link}`,
html: markup,
replyTo: 'alexander@alemayhu.com',
};

return sgMail.send(msg);
}

static async SendConversionEmail(
email: string,
filename: string,
Expand Down
35 changes: 0 additions & 35 deletions src/lib/email/templates/verification.html

This file was deleted.

22 changes: 0 additions & 22 deletions src/lib/misc/TokenHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ class TokenHandler {
return crypto.randomBytes(64).toString('hex');
}

static NewVerificationToken(): string {
return crypto.randomBytes(64).toString('hex');
}

static async IsValidResetToken(token: string): Promise<boolean> {
if (!token || token.length < 128) {
return false;
Expand All @@ -97,24 +93,6 @@ class TokenHandler {
return user && user.reset_token;
}

static async IsValidVerificationToken(token: string): Promise<boolean> {
if (!token || token.length < 128) {
return false;
}
const user = await DB('users')
.where({
verification_token: token,
})
.first();
if (user) {
console.debug('found user with verification token');
} else {
console.debug('no user with verification token');
}
/* @ts-ignore */
return user && user.verification_token;
}

static async IsValidJWTToken(token: string): Promise<boolean> {
if (!token) {
return false;
Expand Down
18 changes: 0 additions & 18 deletions src/routes/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,6 @@ router.post('/forgot-password', async (req, res, next) => {
}
});

router.get('/v/:id', async (req, res, next) => {
console.debug(`verify user ${req.params.id}`);
const valid = await TokenHandler.IsValidVerificationToken(req.params.id);
if (!valid) {
console.debug('invalid verification token');
return res.redirect('/login#login');
}
const token = req.params.id;
DB('users')
.where({ verification_token: token })
.update({ verified: true })
.then(() => res.redirect('/search'))
.catch((err) => next(err));
});

router.get('/logout', RequireAuthentication, async (req, res, next) => {
const { token } = req.cookies;
res.clearCookie('token');
Expand Down Expand Up @@ -171,17 +156,14 @@ router.post('/register', async (req, res, next) => {
const password = hashPassword(req.body.password);
const { name } = req.body;
const email = req.body.email.toLowerCase();
const token = TokenHandler.NewVerificationToken();
try {
await DB('users')
.insert({
name,
password,
email,
verification_token: token,
})
.returning(['id']);
await EmailHandler.SendVerificationEmail(email, token);
res.status(200).json({ message: 'ok' });
} catch (error) {
captureException(error);
Expand Down

0 comments on commit f462187

Please sign in to comment.