Skip to content

Instantly share code, notes, and snippets.

@aanchirinah
Last active May 19, 2020 06:26
Show Gist options
  • Save aanchirinah/9b6f11c9ca7c88bdbb5629ba41118adf to your computer and use it in GitHub Desktop.
Save aanchirinah/9b6f11c9ca7c88bdbb5629ba41118adf to your computer and use it in GitHub Desktop.
Migration file for Verification Token
module.exports = {
up: function(queryInterface, Sequelize) {
return queryInterface.createTable('VerificationTokens', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
userId: {
type: Sequelize.INTEGER,
allowNull: false,
onUpdate: "cascade",
onDelete: "cascade",
references: { model: "Users", key: "id" }
},
token: {
type: Sequelize.STRING,
allowNull: false
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}).then(() => {
console.log('created VerificationToken table');
return queryInterface.sequelize.query(`
CREATE EVENT expireToken
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
DO
DELETE FROM verification_tokens WHERE createdAt < DATE_SUB(NOW(), INTERVAL 1 DAY);
`)
}).then(() => { console.log('expireToken event created') });
},
down: function(queryInterface) {
return queryInterface.dropTable('VerificationTokens')
.then(() => {
console.log(‘VericationTokens table dropped’)
return queryInterface.sequelize.query(`DROP EVENT IF EXISTS expireToken`);
}).then(() => { console.log(‘expireToken event dropped’) })
}
};
@ShejaEddy
Copy link

Hey man. I followed everything. but I'm getting this error syntax error at or near "expireToken" I'm using postgrsql as database. cron events aren't being set. can you help?

@aanchirinah
Copy link
Author

Hey man. I followed everything. but I'm getting this error syntax error at or near "expireToken" I'm using postgrsql as database. cron events aren't being set. can you help?

@ShejaEddy, thanks for pointing this out, I have actually revised this implementation now using JWT tokens , so there is no need to store expireTokens I will update the article soon.

@ShejaEddy
Copy link

ShejaEddy commented Feb 17, 2020 via email

@aanchirinah
Copy link
Author

Yes @ShejaEddy and you resend the email verification in the callback that validates the JWT token

@ckOfor
Copy link

ckOfor commented May 19, 2020

Hey @aanchirinah this tutorials looks good, waiting for the updated version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment