express-user-activator

simple user activation and password reset for express.js

Express.JS User Activator

Usage

var _ = require('lodash-node');
var express = require('express'),
    bodyParser = require('body-parser'),
    app = express();

// lets assume that your model is here
// Make sure to specify following fields if you use mongoose:
//
// password_reset_code: String
// password_reset_time: Number
// activation_code: String
// password: String
// validated: Boolean
//
// Obviously password hashing is not handled here, either pre-process it yourself,
// or use mongoose middleware
//
var User = require('./models/user');

var activator = require('express-user-activator');
var config = {
  user: {},
  protocol: 'http://',
  domain: 'localhost',
  // this will be used to craft email activation links
  pathActivate: '/api/1/users/activate',
  // this will be used to craft password reset confirmation links
  pathResetPassword: '/api/1/users/forgot'
};

// method to find user
config.user.find = function (searchQuery, callback) {
  // it should return user in the callback, signature is <err, user>
  User.findOne(searchQuery, callback);
};

// method to update user
// this is used to save data into user,
// it's your responsibility to setup protection here
config.user.save = function (id, data, callback) {
  // data contains $set and $del properties, we need to perform actions based
  // on these properties
  // Lets assume we use mongodb, then the example would be as simple as this:
  // You might want to take care of error handling though, since here we just
  // spit them out
  User.update({ _id: id }, data, callback);
};

// make sure to initialize it
activator.init(config);


// handling activation
var activateRoutes = express.router(config.pathActivate)
  .get(activator.createActivate)
  .post(activator.completeActivate);

// handling password reset
var passwordResetRoutes = express.router(config.pathResetPassword)
  .get(activator.createPasswordReset)
  .post(activator.completePasswordReset);


app.use(bodyParser);
app.use(activateRoutes);
app.use(passwordResetRoutes);

Configuration

Everything in the code is pretty much commented, but here is a little helper to get you started quicker

  • user{Object}: must override
    1. find{Function} <userId, callback>signature
    2. save{Function} <userId, dataToSave, callback>signature
  • user{Object}: can override
    1. throttle{Function} <userModel, type, callback>signature

Note
User must have these fields available: password_reset_code, password_reset_time, activation_code, password

  • templates{String} - directory for templates used by smtp
  • resetExpire{Number} - duration of expiration link validity, default: 60
  • smtp: {Function|Object} - function which accepts type, lang, data, to, subject, callbackand callbackparams and sends emails based on them, defaults: built-in mail composer
  • from: {String|Object|Null} - to be used in nodemailer's fromfield must override
  • emailProperty: {String} - defaults to email
  • idProperty: {String} - defaults to _id
  • createNextHandler: {Function}
  • createResponse: {Function}
  • transport: {Function} - set this if you want to use preconfigured transport setup these for generating links in the email templates
  • protocol: {String} - defaults to http://
  • domain: {String} - defaults to localhost, must override
  • pathActivate: {String} - defaults to /api/1/users/activate
  • pathResetPassword: {String} - defaults to /api/1/users/forgot
  • password_reset_subject: {String} - subject for password reset emails
  • activation_subject: {String} - subject for account activation emails
  • validatedProperty: {String} - use this to mark account as validated

Testing

To run the tests, from the root directory, run npm test.

License

Released under the MIT License.

Originally developed by Avi Deitcher

Rewritten by Vitaly Aminev

Repository

http://github.com/arkcore/activator.git


上一篇:smtp-tester
下一篇:browser-shim-node-dgram

相关推荐

  • 🙋Hanjst汉吉斯特改进+enSafeExpression安全表达式等

    Hanjst汉吉斯特模版语言及模版引擎,近期持续改进升级。 这次改进主要是增加了对安全输出表达式兼容,由于涉及到对软件开发过程中的效率和软件运行效率的平衡和取舍,所以多写了几句,以描述这个权衡利弊对...

    8 天前
  • 项目总结 - 构建vue cli3.0+express项目

    简介:本篇是记录搭建流程,不过多叙述,搭建的细节,主要以前端为主,项目是主要是为了重构前端代码,后端代码完全复用,还会有篇主要讲node项目的搭建 项目背景: 开发环境前后端同时实现热加载...

    2 年前
  • 通过变量来使用next()在expressjs未来中间件

    cchamberlainuser2791897(https://stackoverflow.com/users/769871/cchamberlain)提出了一个问题:passing variable...

    2 年前
  • 笔记:解读express 4.x源码

    此为裁剪过的笔记版本。 原文在此:https://segmentfault.com/a/11...(https://segmentfault.com/a/1190000000577149) 感谢@...

    2 年前
  • 用动画和实战打开 React Hooks(三):useReducer 和 useContext

    (/public/upload/45308ae73587fefac7115697b30fc22a) 本文由图雀社区成员 mRc 写作而成,欢迎加入图雀社区,一起创作精彩的免费技术教程,予力编...

    23 天前
  • 深入剖析Express cookie-parser中间件实现

    文章导读 cookieparser 是Express的中间件,用来实现cookie的解析,是官方脚手架内置的中间件之一。 它的使用非常简单,但在使用过程中偶尔也会遇到问题。

    2 年前
  • 文件的上传与下载实现(react、express,create-react-app脚手架)

    项目结构为前后端分离,中间布了一层node。 文件上传 要求:将文件信息等发送到后台。 html代码 客户端上传文件时的信息处理 可以使用FormData来异步上传一个二进制文件。

    2 年前
  • 探讨Express Router & Route

    基于 Node.js 平台,快速、开放、极简的 web 开发框架 安装 成功生成后,会产生以下的目录和文件: 接下来我们通过: 启动程序后,访问127.0.0.1:3000,就...

    2 年前
  • 手把教你搭建SSR(vue/vue-cli + express)

    最近简单的研究了一下,对已经有了一个简单的认知,主要应用于单页面应用,是很不错的框架。也有过调研,简单的用了一下,感觉还是很不错。但是还是想知道若不依赖于框架又应该如果处理,研究一下做个笔记。

    1 年前
  • 手把手用 express 搭建后台

    前言 想必很多小伙伴开始学 node 的时候想搞个项目出来却不知道怎么下手吧,这个教程的话就是教大家用 express 框架简单粗暴搭建一个可以用的后台出来,然后关于 node 和 express 的...

    1 年前

官方社区

扫码加入 JavaScript 社区