mirror of
https://github.com/wassname/talk.git
synced 2026-07-28 11:27:05 +08:00
create and decode jwt
This commit is contained in:
+23
-1
@@ -1,6 +1,7 @@
|
||||
const mongoose = require('../mongoose');
|
||||
const uuid = require('uuid');
|
||||
const bcrypt = require('bcrypt');
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
const SALT_ROUNDS = 10;
|
||||
|
||||
@@ -83,10 +84,15 @@ UserSchema.options.toObject.transform = (doc, ret, options) => {
|
||||
* @param {Function} done [description]
|
||||
*/
|
||||
UserSchema.statics.findLocalUser = function(email, password) {
|
||||
|
||||
if (!email || typeof email !== 'string') {
|
||||
return Promise.reject('email is required for findLocalUser');
|
||||
}
|
||||
|
||||
return User.findOne({
|
||||
profiles: {
|
||||
$elemMatch: {
|
||||
id: email,
|
||||
id: email.toLowerCase(),
|
||||
provider: 'local'
|
||||
}
|
||||
}
|
||||
@@ -221,6 +227,8 @@ UserSchema.statics.createLocalUser = function(email, password, displayName) {
|
||||
return Promise.reject('email is required');
|
||||
}
|
||||
|
||||
email = email.toLowerCase();
|
||||
|
||||
if (!password) {
|
||||
return Promise.reject('password is required');
|
||||
}
|
||||
@@ -338,6 +346,20 @@ UserSchema.statics.findByIdArray = function(ids) {
|
||||
});
|
||||
};
|
||||
|
||||
UserSchema.statics.createJWT = function (email) {
|
||||
if (!email || typeof email !== 'string') {
|
||||
return Promise.reject('email is required when creating a JWT for resetting passord');
|
||||
}
|
||||
|
||||
email = email.toLowerCase();
|
||||
|
||||
const payload = {email, jti: uuid.v4()};
|
||||
|
||||
const token = jwt.sign(payload, process.env.TALK_SESSION_SECRET, {expiresIn: '1d'});
|
||||
|
||||
return Promise.resolve(token);
|
||||
};
|
||||
|
||||
const User = mongoose.model('User', UserSchema);
|
||||
|
||||
module.exports = User;
|
||||
|
||||
+2
-1
@@ -51,13 +51,13 @@
|
||||
"debug": "^2.2.0",
|
||||
"ejs": "^2.5.2",
|
||||
"express": "^4.14.0",
|
||||
"jsonwebtoken": "^7.1.9",
|
||||
"lodash": "^4.16.6",
|
||||
"mongoose": "^4.6.5",
|
||||
"morgan": "^1.7.0",
|
||||
"nodemailer": "^2.6.4",
|
||||
"nodemailer-sendgrid-transport": "^0.2.0",
|
||||
"prompt": "^1.0.0",
|
||||
"react-mdl-selectfield": "^0.2.0",
|
||||
"uuid": "^2.0.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -107,6 +107,7 @@
|
||||
"react": "15.3.2",
|
||||
"react-dom": "15.3.2",
|
||||
"react-mdl": "^1.7.2",
|
||||
"react-mdl-selectfield": "^0.2.0",
|
||||
"react-onclickoutside": "^5.7.1",
|
||||
"react-redux": "^4.4.5",
|
||||
"react-router": "^3.0.0",
|
||||
|
||||
+13
-1
@@ -1,11 +1,23 @@
|
||||
const express = require('express');
|
||||
|
||||
const jwt = require('jsonwebtoken');
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/embed/stream/preview', (req, res) => {
|
||||
res.render('embed-stream', {basePath: '/client/embed/stream'});
|
||||
});
|
||||
|
||||
router.get('/password-reset/:token', (req, res, next) => {
|
||||
jwt.verify(req.params.token, process.env.TALK_SESSION_SECRET, (error, decoded) => {
|
||||
if (error) {
|
||||
return res.status(400).json({error});
|
||||
}
|
||||
|
||||
console.log(decoded);
|
||||
|
||||
res.json(decoded);
|
||||
});
|
||||
});
|
||||
|
||||
router.get('*', (req, res) => {
|
||||
res.render('admin', {basePath: '/client/coral-admin'});
|
||||
});
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const User = require('../../../models/user');
|
||||
const mailer = require('../../../services/mailer');
|
||||
|
||||
router.get('/', (req, res, next) => {
|
||||
const {
|
||||
@@ -70,4 +71,37 @@ router.post('/:user_id/role', (req, res, next) => {
|
||||
.catch(next);
|
||||
});
|
||||
|
||||
/**
|
||||
* this endpoint takes an email (username) and checks if it belongs to a User account
|
||||
* if it does, create a JWT and send an email
|
||||
*/
|
||||
router.post('/request-password-reset', (req, res, next) => {
|
||||
const {email} = req.body;
|
||||
|
||||
console.log('/request-password-reset', req.body);
|
||||
|
||||
if (!email) {
|
||||
return next();
|
||||
}
|
||||
|
||||
User
|
||||
.createJWT(email)
|
||||
.then(token => {
|
||||
const options = {
|
||||
subject: 'password reset requested',
|
||||
from: 'coralcore@mozillafoundation.org',
|
||||
to: 'riley.davis@gmail.com',
|
||||
html: `<a href="http://localhost:3000/admin/password-reset/${token}">reset password</a>`
|
||||
};
|
||||
return mailer.sendSimple(options);
|
||||
})
|
||||
.then(success => {
|
||||
console.log(success);
|
||||
res.json({success: true});
|
||||
})
|
||||
.catch(error => {
|
||||
res.status(500).json({error});
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -8,23 +8,6 @@ const options = {
|
||||
|
||||
const transporter = nodemailer.createTransport(sgTransport(options));
|
||||
|
||||
transporter.sendMail({
|
||||
from: 'support@mrdavis.com',
|
||||
to: 'riley.davis@gmail.com',
|
||||
subject: 'this is only a test',
|
||||
text: 'this is the body of the email maybe?',
|
||||
html: `<table>
|
||||
<thead>
|
||||
<tr><th>foo</th><th>bar</th><tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>riley</td><td>davis</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>`
|
||||
});
|
||||
|
||||
const mailer = {
|
||||
/**
|
||||
* sendSimple
|
||||
|
||||
Reference in New Issue
Block a user