dhn-accounts
v1.1.2
Published
An account management library for Node.js and MongoDB
Downloads
12
Maintainers
Readme
dhn-accounts
A promise based account management library for Node.js and MongoDB
Usage
Api
Usage
Basic Configurations
const { MongoClient } = require('mongodb')
const dhnAccounts = require('dhn-accounts')
const accounts = new dhnAccounts
MongoClient.connect('mongodb://127.0.0.1:27017', { useNewUrlParser: true })
.then(db => {
accounts.init({
db,
database: 'database',
collection: 'users'
})
}).catch(err => console.error(err))
accounts.secret = 'secretKey' // Secret key for JWT
//smtp setup | optional
const nodemailer = require('nodemailer') // not included in dhn-accounts
accounts.smtpSetup({
nodemailer, // you need to pass nodemailer by yourself
from: '"Duhan BALCI" <[email protected]>',
host: '',
port: 465,
secure: true,
user: '',
pass: '',
})
Functions
accounts.createUser({
email: '[email protected]',
password: 'p@ssw0rd'
}).then(() => console.log('user created'))
.catch(err => console.error(err.message))
accounts.login('[email protected]', '123123')
.then(res => console.log(res)) // returns jwt
.catch(err => console.error(err.message))
accounts.verifyToken(`${jwtToken}`)
.then(res => console.log(res)) // returns payload
.catch(err => console.error(err.message))
accounts.changePassword(`${userId}`, `${newPassword}`)
.then(() => console.log('changed'))
.catch(err => console.log(err.message))
accounts.changePasswordByUser(`${userId}`, `${oldPass}`, `${newPass}`)
.then(() => console.log('changed'))
.catch(err => console.log(err.message))
accounts.sendVerificationEmail(`${emailAdress}`)
.then(() => console.log('sent'))
.catch(err => console.error(err.message))
accounts.verifyEmail(`${verificationCode}`)
.then(() => console.log('verified'))
.catch(err => console.error(err.message))
accounts.sendPasswordRecoveryEmail(`${emailAdress}`)
.then(() => console.log('sent'))
.catch(err => console.error(err.message))
accounts.passwordRecovery(`${recoveryCode}`, `${newPassword}`)
.then(() => console.log('changed'))
.catch(err => console.error(err.message))
accounts.setUsername(`${userId}`, `${newUsername}`)
.then(() => console.log('change'))
.catch(err => console.error(err.message))
accounts.addEmail(`${userId}`, `${newEmail}`)
.then(() => console.log('added'))
.catch(err => console.error(err.message))
accounts.removeEmail(`${emailAdress}`)
.then(() => console.log('deleted'))
.catch(err => console.error(err.message))
Email Templates
//defaults
accounts.emailVerificationEmail.template = '<a href="http://example.com/verifyEmail/{verificationCode}" >Click here</a> for validte your email adress.'
accounts.emailVerificationEmail.subject = 'Email Verification Mail'
accounts.passwordRecoveryEmail.template = '<a href="{passwordRecoveryCode}">Click here</a> for recover your password.'
accounts.passwordRecoveryEmail.subject = 'Password Recovery Mail'
Internationalization
//Defaults
accounts.lang = 'en'
accounts.i18n.en = {
passwordCantBeEmpty: 'password can not be empty',
usernameAndEmailCantBeEmpty: 'username and email adress cannot be empty at the same time',
usernameAlreadyUsing: 'username already using',
emailAdressAlreadyUsing: 'email adress already using',
wrongUsername: 'wrong username',
wrongEmailAdress: 'wrong email adress',
wrongPassword: 'wrong password',
userNotFound: 'user not found',
emailAdressNotFound: 'email adress not found',
loginError: 'login error',
wrongToken: 'wrong token',
verificationEmailFail: 'failed to send verification email',
recoveryEmailFail: 'failed to send password recovery email',
}
Api
accounts.createUser(options)
options Object
- username string: optional if email entered
- email string: optional if username entered
- password string: password
- profile? object: optional. You can access it from jwt payload
accounts.login(login, password)
- login string: username or email adress
- password string: password
accounts.verifyToken(jwtToken)
- jwtToken string: json web token
accounts.changePassword(userId, newPassword)
- userId string: user id
- newPassword string: new password
accounts.changePasswordByUser(userId, oldPass, newPass)
- userId string: user id
- oldPass string: old password
- newPass string: new password
accounts.sendVerificationEmail(emailAdress)
- emailAdress string: email adress
accounts.verifyEmail(verificationCode)
- verificationCode string: verificationCode from email
accounts.sendPasswordRecoveryEmail(emailAdress)
- emailAdress string: email adress
accounts.passwordRecovery(recoveryCode, newPassword)
- recoveryCode string: recovery code from email
- newPassword string: email adress
accounts.setUsername(userId, newUsername)
- userId string: user id
- newUsername string: new username
accounts.addEmail(userId, newEmail)
- userId string: user id
- newEmail string: email adress
accounts.removeEmail(emailAdress)
- emailAdress string: email adress