组态
第一步
我们需要创建一个名为 jwToken 的服务。转到 api/services
目录并创建 jwToken.js
。
'use strict';
const jwt = require('jsonwebtoken'),
tokenSecret = "secretissecret";
module.exports = {
// Generates a token from supplied payload
issue(payload) {
return jwt.sign(
payload,
tokenSecret, // Token Secret that we sign it with
{
expiresIn: "30 days" // Token Expire time
});
},
// Verifies token on a request
verify(token, callback) {
return jwt.verify(
token, // The token to be verified
tokenSecret, // Same token we used to sign
{}, // No Option, for more see https://github.com/auth0/node-jsonwebtoken#jwtverifytoken-secretorpublickey-options-callback
callback //Pass errors or decoded token to callback
);
}
};
第二步
使用 bcrypt
加密我们的密码。去 api/models/User.js
。
'use strict';
const bcrypt = require('bcrypt');
module.exports = {
attributes: {
// your code...
},
// Here we encrypt password before creating a User
beforeCreate(values, next) {
bcrypt.genSalt(10, (err, salt) => {
if (err) {
sails.log.error(err);
return next();
}
bcrypt.hash(values.password, salt, (err, hash) => {
if (err) {
sails.log.error(err);
return next();
}
values.encryptedPassword = hash; // Here is our encrypted password
return next();
});
});
},
comparePassword(password, encryptedPassword) {
return new Promise(function(resolve, reject) {
bcrypt.compare(password, encryptedPassword, (err, match) => {
if (err) {
sails.log.error(err);
return reject("Something went wrong!");
}
if (match) return resolve();
else return reject("Mismatch passwords");
});
});
}
};
第三步
创建 isAuthorized 策略以检查用户是否在请求标头中具有有效令牌。去 api/policies
并创建 isAuthorized.js
。
'use strict';
module.exports = (req, res, next) => {
let token;
if (req.headers && req.headers.token) {
token = req.headers.token;
if (token.length <= 0) return res.json(401, {err: 'Format is Authorization: Bearer [token]'});
} else if (req.param('token')) {
token = req.param('token');
// We delete the token from param to not mess with blueprints
delete req.query.token;
} else {
return res.json(401, {err: 'No Authorization header was found'});
}
jwToken.verify(token, function (err, token) {
if (err) return res.json(401, {err: 'Invalid Token!'});
req.token = token; // This is the decrypted token or the payload you provided
next();
});
};
第四步
我们使用 config / policies.js 来保护我们的控制器
module.exports.policies = {
'*': ['isAuthorized'], // Everything resctricted here
'UserController': { // Name of your controller
'create': true // We dont need authorization here, allowing public access
}
};
第五步
我们来测试一下我们的实现。去 api/controllers
并创建 UserController.js
'use strict';
module.exports = {
create(req, res) {
const data = req.body;
if (data.password !== data.confirmPassword) return res.badRequest("Password not the same");
User.create({
email: data.email,
password: data.password,
name: data.name
//etc...
})
.then((user) => {
res.send({ token: jwToken.issue({ id: user.id }) }); // payload is { id: user.id}
})
.catch((err) => {
sails.log.error(err);
return res.serverError("Something went wrong");
});
},
login(req, res) {
const data = req.body;
if (!data.email || !data.password) return res.badRequest('Email and password required');
User.findOne({ email: email })
.then((user) => {
if (!user) return res.notFound();
User.comparePassword(password, user.password)
.then(() => {
return res.send({ token: jwToken.issue({ id: user.id }) })
})
.catch((err) => {
return res.forbidden();
});
})
.catch((err) => {
sails.log.error(err);
return res.serverError();
});
}
};