29 lines
759 B
TypeScript

import mongoose, { Schema, SchemaTypes } from 'mongoose';
import * as bcrypt from "https://deno.land/x/bcrypt@v0.4.1/mod.ts";
import { genSalt } from 'https://deno.land/x/bcrypt@v0.4.1/src/main.ts';
import { configDatabase } from 'lib/data.ts';
configDatabase(mongoose, 'CGGUsers');
const userSchema = new Schema({
username: {
type: SchemaTypes.String,
required: true,
unique: true
},
password: {
type: SchemaTypes.String,
required: true
}
})
userSchema.methods.generateHash = async (pw: string) => {
return await bcrypt.hash(pw, await genSalt(8));
}
userSchema.methods.validatePassword = async function(pw:string) {
return await bcrypt.compare(pw, this.password)
}
export const User = mongoose.model('User', userSchema);