mirror of
				https://github.com/strapi/strapi.git
				synced 2025-10-31 18:08:11 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			43 lines
		
	
	
		
			917 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			43 lines
		
	
	
		
			917 B
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| const _ = require('lodash');
 | |
| const bcrypt = require('bcryptjs');
 | |
| 
 | |
| const sanitizeUser = user => {
 | |
|   return _.omit(user.toJSON ? user.toJSON() : user, [
 | |
|     'password',
 | |
|     'resetPasswordToken',
 | |
|   ]);
 | |
| };
 | |
| 
 | |
| /**
 | |
|  * Creates a JWT token for an administration user
 | |
|  * @param {object} admon - admin user
 | |
|  */
 | |
| const createJwtToken = admin => {
 | |
|   return strapi.plugins['users-permissions'].services.jwt.issue({
 | |
|     id: admin.id,
 | |
|     isAdmin: true,
 | |
|   });
 | |
| };
 | |
| 
 | |
| /**
 | |
|  * hashes a password
 | |
|  * @param {string} password - password to hash
 | |
|  * @returns {string} hashed password
 | |
|  */
 | |
| const hashPassword = password => bcrypt.hash(password, 10);
 | |
| 
 | |
| /**
 | |
|  * Validate a password
 | |
|  * @param {string} password
 | |
|  * @param {string} hash
 | |
|  * @returns {boolean} is the password valid
 | |
|  */
 | |
| const validatePassword = (password, hash) => bcrypt.compare(password, hash);
 | |
| 
 | |
| module.exports = {
 | |
|   createJwtToken,
 | |
|   sanitizeUser,
 | |
|   validatePassword,
 | |
|   hashPassword,
 | |
| };
 | 
