Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mongoose = require('mongoose');
- const bcrypt = require('bcryptjs');
- const config = require('../config/database');
- //User Schema
- const UserSchema = mongoose.Schema({
- name: {
- type: String
- },
- email: {
- type : String,
- required: true
- },
- username: {
- type : String,
- required: true
- },
- password: {
- type : String,
- required: true
- }
- })
- const User = module.exports = mongoose.model('User', UserSchema);
- module.exports.getUserById = function(id, callback){
- User.findById(id,callback);
- }
- module.exports.getUserByUsername = function(username, callback){
- const query = {username : username};
- User.findOne(query,callback);
- }
- module.exports.addUser = function(newUser, callback){
- bcrypt.genSalt(10, (err, salt) => {
- bcrypt.hash(newUser.password, salt, (err, hash) => {
- if(err) throw err;
- newUser.password = hash;
- newUser.save(callback);
- });
- });
- }
- module.exports.comparePassword= function(password, password_hashed, callback){
- bcrypt.compare(password, password_hashed, (err, isMatch) =>{
- if(err) throw err;
- callback(null, isMatch);
- });
- }
- /*{
- "name":"John",
- "email":"valafaf.com",
- "username":"John",
- "password":"1asd23",
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement