Compare commits
No commits in common. "28806f49ca47d460c7e1b087e4181876fb56f5d4" and "4cbed331b1c3ab01971ddec6180d0fafe71ea698" have entirely different histories.
28806f49ca
...
4cbed331b1
|
@ -1,4 +1,3 @@
|
||||||
node_modules/
|
node_modules/
|
||||||
package-lock.json
|
package-lock.json
|
||||||
*.mjs
|
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
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,4 +1,47 @@
|
||||||
|
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