remove mongoose lines
This commit is contained in:
parent
4cbed331b1
commit
6eb94b8520
|
@ -1,14 +1,10 @@
|
||||||
import express from "express"
|
import express from "express"
|
||||||
import mongoose from "mongoose"
|
|
||||||
import passport from "passport"
|
import passport from "passport"
|
||||||
import bodyParser from "body-parser"
|
import bodyParser from "body-parser"
|
||||||
import { db } from "./db.mjs"
|
import { db } from "./db.mjs"
|
||||||
import { default as routes } from "./routes/routes.mjs"
|
import { default as routes } from "./routes/routes.mjs"
|
||||||
import { default as secureRoute } from "./routes/secure-routes.mjs"
|
import { default as secureRoute } from "./routes/secure-routes.mjs"
|
||||||
import "./auth/auth.mjs"
|
import "./auth/auth.mjs"
|
||||||
mongoose.connect("mongodb://127.0.0.1:27017/passport-jwt", {});
|
|
||||||
mongoose.connection.on('error', error => console.log(error));
|
|
||||||
mongoose.Promise = global.Promise;
|
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
app.use(passport.initialize())
|
app.use(passport.initialize())
|
||||||
|
|
|
@ -1,47 +1,4 @@
|
||||||
import mongoose from "mongoose";
|
|
||||||
import bcrypt from "bcrypt"
|
import bcrypt from "bcrypt"
|
||||||
|
|
||||||
const Schema = mongoose.Schema
|
|
||||||
|
|
||||||
const UserSchema = new Schema({
|
|
||||||
email: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
unique: true
|
|
||||||
},
|
|
||||||
password: {
|
|
||||||
type: String,
|
|
||||||
required: true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
UserSchema.pre(
|
|
||||||
"save",
|
|
||||||
async function(next) {
|
|
||||||
const hash = await bcrypt.hash(this.password, 10)
|
|
||||||
this.password = hash;
|
|
||||||
next();
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
UserSchema.methods.isValidPassword = async function(password: string) {
|
|
||||||
const compare = await bcrypt.compare(password, this.password)
|
|
||||||
return compare
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface User {
|
|
||||||
email: string;
|
|
||||||
password: string;
|
|
||||||
isValidPassword: (password: string) => Promise<boolean>
|
|
||||||
}
|
|
||||||
|
|
||||||
export const UserModel = mongoose.model("user", UserSchema)
|
|
||||||
|
|
||||||
export interface User {
|
|
||||||
username: string;
|
|
||||||
password: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function encryptPwd(pwd: string) {
|
export async function encryptPwd(pwd: string) {
|
||||||
return Promise.resolve(bcrypt.hash(pwd, 10))
|
return Promise.resolve(bcrypt.hash(pwd, 10))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue