Skip to content

Commit

Permalink
ts configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
afekTheMiniLearner committed Sep 26, 2022
1 parent 6e3d4af commit a456e72
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 68 deletions.
30 changes: 0 additions & 30 deletions back-end-practice/cook-site/config/passport-config.js

This file was deleted.

32 changes: 32 additions & 0 deletions back-end-practice/cook-site/config/passport-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import passport from 'passport';
import { Strategy as LocalStrategy } from 'passport-local';
import bcrypt from 'bcrypt';
import { UserModel } from '../models/user.model';
import { findUserByEmail } from '../services/users.services';
import { passportConfigUser } from '../interfaces/user.interface';

passport.use(
new LocalStrategy(async (userEmail, password, done) => {
const user = await findUserByEmail(userEmail);

if (!user) return done('user not found', null);

const matchedPassword = await bcrypt.compare(password, user.password);

if (!matchedPassword) {
return done('user not match password', null);
}

done(null, user);
})
);

passport.serializeUser((user: passportConfigUser | null, done: Function) => {
done(null, user?._id);
});

passport.deserializeUser(async (id: string, done: Function) => {
const user = await UserModel.findById(id);
if (!user) done('user not found', null);
else done(null, user);
});
7 changes: 7 additions & 0 deletions back-end-practice/cook-site/interfaces/recipe.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface IRecipe {
name: string;
creator: string;
ingredients: string;
cookingTime: string;
difficulityLevel: string;
}
21 changes: 3 additions & 18 deletions back-end-practice/cook-site/interfaces/user.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,6 @@ export interface IUser {
fullName: string;
}

/*
export interface IDogQuery {
status?: number;
gender?: string;
race?: string[];
minAge?: number;
maxAge?: number;
name?: string;
page: number; // offset
itemsPerPage: number; // limit
sortByStatus?: number;
sortByGender?: number;
sortByRace?: number;
sortByAge?: number;
sortByName?: number;
sortByLastUpdated?: number;
}
*/
export type passportConfigUser = {
_id?: number;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const mongoose = require('mongoose');
import mongoose from 'mongoose';
const Schema = mongoose.Schema;

const recipeSchema = new Schema(
Expand All @@ -11,11 +11,11 @@ const recipeSchema = new Schema(
creator: {
type: String,
default: 'anonymous',
},
}, //todo : change to id
ingredients: {
type: String,
required: [true, 'missing ingredients to the recipe'],
},
}, //todo : change to array of strings (string[])
cookingTime: {
type: String,
required: [true, 'you forgot to mention the cooking time!'],
Expand All @@ -25,4 +25,4 @@ const recipeSchema = new Schema(
{ timestamps: true }
);

module.exports.RecipeModel = mongoose.model('recipe', recipeSchema);
export const RecipeModel = mongoose.model('recipe', recipeSchema);
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const mongoose = require('mongoose');
import mongoose from 'mongoose';
import bcrypt from 'bcrypt';
const Schema = mongoose.Schema;
const bcrypt = require('bcrypt');

const userSchema = new Schema(
{
Expand Down Expand Up @@ -33,4 +33,4 @@ userSchema.pre('save', async function (done) {
done();
});

module.exports.UserModel = mongoose.model('user', userSchema);
export const UserModel = mongoose.model('user', userSchema);
31 changes: 20 additions & 11 deletions back-end-practice/cook-site/services/users.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,30 @@ export async function registerUser(user: IUser): Promise<IUser | undefined> {
return result.toJSON();
}

export async function updateUserData(data: IUser): Promise<IUser | undefined> {
export async function updateUserData(
userData: IUser
): Promise<IUser | undefined> {
if (userData.password !== undefined) {
const salt = bcrypt.genSaltSync(10);
userData.password = bcrypt.hashSync(userData.password, salt);
}

const result: any = await UserModel.findOneAndUpdate(
{ _id: data._id },
{ _id: userData._id },
{
password: data.password
? bcrypt.hashSync(data.password, bcrypt.genSaltSync(10))
: password,
email: userData.email,
password: userData.password,
phoneNumber: userData.phoneNumber,
fullName: userData.fullName,
},
...(data.fullName!==undefined && fullName:data.fullName),
{ phoneNumber: data.phoneNumber ? data.phoneNumber : phoneNumber },
{
returnOriginal: false,
}
//fix me
{ new: true, omitUndefined: true }
);

return result;
}

export async function findUserByEmail(userEmail: string) {
const userDoc = await UserModel.findOne({ email: userEmail });

return userDoc;
}
2 changes: 1 addition & 1 deletion nodemon.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"watch": "for-fun-!/police-officer.ts",
"watch": "back-end-practice/cook-site/**/*.ts",
"execMap": {
"ts": "ts-node"
}
Expand Down
62 changes: 62 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"author": "Hadriel",
"license": "ISC",
"devDependencies": {
"@types/bcrypt": "^5.0.0",
"@types/passport-local": "^1.0.34",
"jest": "^27.5.1",
"jest-html-reporters": "^3.0.5",
"prettier": "^2.7.1",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ts-node": { "files": true },
"files": ["for-fun-!/police-officer.ts"],
"files": ["cook-site/app.ts"],
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */

Expand Down

0 comments on commit a456e72

Please sign in to comment.