{"size": 1443, "ext": "js", "max_stars_repo_path": "Student-Hub/models/User.js", "max_stars_repo_name": "chandankeshri1812/IEEE-Megaproject", "max_stars_repo_stars_event_min_datetime": "2022-01-07T14:07:07.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-05T04:03:07.000Z", "max_issues_repo_path": "Student-Hub/models/User.js", "max_issues_repo_name": "chandankeshri1812/IEEE-Megaproject", "max_issues_repo_issues_event_min_datetime": "2022-01-08T08:42:08.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-28T01:49:15.000Z", "max_forks_repo_path": "Student-Hub/models/User.js", "max_forks_repo_name": "chandankeshri1812/IEEE-Megaproject", "max_forks_repo_forks_event_min_datetime": "2022-01-07T13:15:15.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-25T04:35:15.000Z"}
{"max_stars_count": 8, "max_issues_count": 41, "max_forks_count": 50, "avg_line_length": 21.2205882353, "max_line_length": 103, "alphanum_fraction": 0.5862785863}
//User schema used for saving user data
const mongoose = require("mongoose");
//Using bcrypt to encrypt user password making the website more safe for user
const bcrypt = require("bcrypt");

mongoose.connect("mongodb+srv://allstudentsHub:allstudentsHub@cluster0.14bcs.mongodb.net/studentHub", {
    useNewUrlParser: true,
    useUnifiedTopology: true
});

const userSchema = mongoose.Schema({
    username: {
        type: String,
        unique: true,
        required: true
    },
    email: {
        type: String,
        unique: true,
        required: true
    },
    verified: {
        type: Boolean,
        default: false
    },
    password: {
        type: String,
        required: true
    },
    designation: {
        type: String
    },
	institute: {
		type: String
	},
    portfolio: {
        type: String
    },
    resources: [{
        id: {
            type: String,
        },
        title: {
            type: String
        }
    }],
    img:
    {
        type: String,
        default: 'blank.png'
    }
});

userSchema.pre("save", function(next) {
    if(!this.isModified("password")) {
        return next()
    }
    this.password = bcrypt.hashSync(this.password, 10);
    next();
});

userSchema.methods.comparePassword = function(plainText, callback) {
    return callback(null, bcrypt.compareSync(plainText, this.password))
}

const userModel = mongoose.model("user", userSchema);

module.exports = userModel