Compare commits
	
		
			2 Commits
		
	
	
		
			4cbed331b1
			...
			28806f49ca
		
	
	| Author | SHA1 | Date | 
|---|---|---|
| 
							
							
								
								 | 
						28806f49ca | |
| 
							
							
								
								 | 
						6eb94b8520 | 
| 
						 | 
				
			
			@ -1,3 +1,4 @@
 | 
			
		|||
node_modules/
 | 
			
		||||
package-lock.json
 | 
			
		||||
*.mjs
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,14 +1,10 @@
 | 
			
		|||
import express from "express"
 | 
			
		||||
import mongoose from "mongoose"
 | 
			
		||||
import passport from "passport"
 | 
			
		||||
import bodyParser from "body-parser"
 | 
			
		||||
import { db } from "./db.mjs"
 | 
			
		||||
import { default as routes } from "./routes/routes.mjs"
 | 
			
		||||
import { default as secureRoute } from "./routes/secure-routes.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()
 | 
			
		||||
app.use(passport.initialize())
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,47 +1,4 @@
 | 
			
		|||
import mongoose from "mongoose";
 | 
			
		||||
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) {
 | 
			
		||||
	return Promise.resolve(bcrypt.hash(pwd, 10))
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue