Removed unused authentication and product code

This commit is contained in:
Riccardo
2022-07-15 23:12:37 +02:00
parent 36a945e175
commit 0ee1ce6fb8
14 changed files with 9 additions and 555 deletions

View File

@@ -9,34 +9,6 @@ import './utils/db.js';
import fs from 'fs';
import path from 'path';
import cors from 'cors';
import jwt from 'jsonwebtoken';
const APP_SECRET = 'GraphQL-is-aw3some';
function getTokenPayload(token) {
return jwt.verify(token, APP_SECRET);
}
function getUserId(req, authToken) {
if (req) {
const authHeader = req.headers.authorization;
if (authHeader) {
const token = authHeader.replace('Bearer ', '');
if (!token) {
throw new Error('No token found');
}
const { userId } = getTokenPayload(token);
return userId;
}
} else if (authToken) {
const { userId } = getTokenPayload(authToken);
return userId;
}
throw new Error('Not authenticated');
}
const moduleURL = new URL(import.meta.url);
const __dirname = path.dirname(moduleURL.pathname);
@@ -64,11 +36,7 @@ const server = new ApolloServer({
return {
...req,
mongoose,
pubsub,
userId:
req && req.headers.authorization
? getUserId(req)
: null
pubsub
}
},
introspection: true,

View File

@@ -1,12 +0,0 @@
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
const ProductSchema = new Schema({
title: {
type: String,
required: true
},
qty: {
type: Number
}
});
export default mongoose.model('product', ProductSchema);

View File

@@ -1,33 +0,0 @@
import mongoose from 'mongoose';
import bcrypt from 'bcrypt';
const Schema = mongoose.Schema;
const UserSchema = new Schema({
username: {
type: String,
required: true
},
email: {
type: String,
required: true
},
password: {
type: String,
required: true
},
deleted: {
type: Boolean,
required: false
}
});
// hash the password
UserSchema.methods.generateHash = function (password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
// checking if password is valid
UserSchema.methods.validPassword = function (password) {
return bcrypt.compareSync(password, this.password);
};
export default mongoose.model('user', UserSchema);

View File

@@ -1,7 +1,4 @@
import Product from './models/product.js';
import Appointment from './models/appointment.js';
import User from './models/user.js'
import jwt from 'jsonwebtoken';
export const resolvers = {
Query: {
@@ -13,57 +10,11 @@ export const resolvers = {
_id: args._id
});
},
async allProducts() {
return await Product.find();
},
async allUsers() {
return await User.find();
},
},
Mutation: {
async signup(root, args, context, info) {
var user = await User.create(args);
user.password = user.generateHash(args.password);
user.save();
const token = jwt.sign({ userId: user.id }, process.env.APP_SECRET);
return {
token,
user
};
},
async login(parent, args, context, info) {
console.log(context);
const { userId } = context;
console.log(userId);
const user = await User.findOne({
email: args.email
});
if (!user) {
throw new Error('No such user found');
}
if (!user.validPassword(args.password)) {
throw new Error('Invalid password');
}
const token = jwt.sign({ userId: user.id }, process.env.APP_SECRET);
return {
token,
user
};
},
async createAppointment(parent, args, context, info) {
console.log(context);
const { userId } = context;
console.log("userID", userId);
args.deleted = false;
args.createdBy = userId;
return await Appointment.create(args);
},
async updateAppointment(parent, args, context, info) {
@@ -77,27 +28,5 @@ export const resolvers = {
async deleteAppointment(parent, args, context, info) {
return await Appointment.findOneAndUpdate({ _id: args._id }, { deleted: true })
},
async createProduct(root, {
input
}) {
return await Product.create(input);
},
async updateProduct(root, {
_id,
input
}) {
return await Product.findOneAndUpdate({
_id
}, input, {
new: true
})
},
async deleteProduct(root, {
_id
}) {
return await Product.findOneAndRemove({
_id
});
},
}
};

View File

@@ -4,13 +4,6 @@ function createdBy(parent, args, context) {
.createdBy();
}
function follows(parent, args, context) {
return context.mongo.appointment
.findUnique({ where: { id: parent.id } })
.follows();
}
module.exports = {
createdBy,
follows
createdBy
};

View File

@@ -1,16 +0,0 @@
function appointment(parent, args, context) {
return context.mongo.follow
.findUnique({ where: { id: parent.id } })
.appointment();
}
function user(parent, args, context) {
return context.mongo.follow
.findUnique({ where: { id: parent.id } })
.user();
}
module.exports = {
appointment,
user
};

View File

@@ -1,90 +1,14 @@
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const { APP_SECRET } = require('../utils');
function createAppointment(parent, args, context, info) {
const { userId } = context;
const newAppointment = context.mongo.appointment.create({
data: {
title: args.title,
description: args.description,
createdBy: { connect: { id: userId } }
description: args.description
}
});
return newAppointment;
}
async function signup(parent, args, context, info) {
const password = await bcrypt.hash(args.password, 10);
const user = await context.mongo.user.create({
data: { ...args, password }
});
const token = jwt.sign({ userId: user.id }, APP_SECRET);
return {
token,
user
};
}
async function login(parent, args, context, info) {
const user = await context.mongo.user.findUnique({
where: { email: args.email }
});
if (!user) {
throw new Error('No such user found');
}
const valid = await bcrypt.compare(
args.password,
user.password
);
if (!valid) {
throw new Error('Invalid password');
}
const token = jwt.sign({ userId: user.id }, APP_SECRET);
return {
token,
user
};
}
async function follow(parent, args, context, info) {
const { userId } = context;
const follow = await context.mongo.follow.findUnique({
where: {
linkId_userId: {
linkId: Number(args.linkId),
userId: userId
}
}
});
if (Boolean(follow)) {
throw new Error(
`Already followed the appointment: ${args.linkId}`
);
}
const newFollow = context.mongo.follow.create({
data: {
user: { connect: { id: userId } },
link: { connect: { id: Number(args.linkId) } }
}
});
context.pubsub.publish('NEW_FOLLOW', newFollow);
return newFollow;
}
module.exports = {
createAppointment,
signup,
login,
follow
createAppointment
};

View File

@@ -9,18 +9,6 @@ const newAppointment = {
},
}
function newFollowSubscribe(parent, args, context, info) {
return context.pubsub.asyncIterator("NEW_FOLLOW")
}
const newFollow = {
subscribe: newFollowSubscribe,
resolve: payload => {
return payload
},
}
module.exports = {
newAppointment,
newFollow
}

View File

@@ -6,13 +6,8 @@ type Query {
take: Int
orderBy: AppointmentOrderByInput
): Feed!
allProducts: [Product]
allAppointments: [Appointment]
oneAppointment(
_id: ID!
): Appointment
allUsers: [User]
users: [User!]!
oneAppointment(_id: ID!): Appointment
}
type Feed {
@@ -31,7 +26,7 @@ type Mutation {
deleted: Boolean
): Appointment!
updateAppointment(
_id: ID!,
_id: ID!
title: String!
description: String
type: String!
@@ -39,55 +34,11 @@ type Mutation {
end: DateTime!
deleted: Boolean
): Appointment
deleteAppointment(
_id: ID!
) : Appointment
createProduct(
input: ProductInput
) : Product
updateProduct(
_id: ID!,
input: ProductInput
): Product
deleteProduct(
_id: ID!
) : Product
signup(
email: String!
password: String!
username: String!
): AuthPayload
login(
email: String!,
password: String!
): AuthPayload
follow(
appointmentId: ID!
): Follow
deleteAppointment(_id: ID!): Appointment
}
type Subscription {
newAppointment: Appointment
newFollow: Follow
}
#User Schemas
type User {
_id: ID!
username: String!
email: String!
password: String!
# appointments: [Appointment!]!
}
input UserInput{
username: String!
email: String!
password: String!
# appointments: [Appointment!]!
}
type AuthPayload {
token: String
user: User
}
# Appointment schemas
@@ -99,9 +50,6 @@ type Appointment {
start: DateTime!
end: DateTime!
deleted: Boolean
createdBy: User
# follows: [Follow!]!
# createdAt: DateTime!
}
input AppointmentInput {
title: String!
@@ -110,29 +58,10 @@ input AppointmentInput {
start: DateTime!
end: DateTime!
deleted: Boolean
}
}
input AppointmentOrderByInput {
title: Sort
desc: Sort
# createdAt: Sort
}
# Product schemas
type Product {
_id: ID!
title: String!
qty: Int
}
input ProductInput {
title: String!
qty: Int
}
# Follow schemas
type Follow {
_id: ID!
appointment: Appointment!
user: User!
}
# General-purpose schemas

View File

@@ -1,30 +1,5 @@
import jwt from 'jsonwebtoken';
const APP_SECRET = 'GraphQL-is-aw3some';
function getTokenPayload(token) {
return jwt.verify(token, APP_SECRET);
}
function getUserId(req, authToken) {
if (req) {
const authHeader = req.headers.authorization;
if (authHeader) {
const token = authHeader.replace('Bearer ', '');
if (!token) {
throw new Error('No token found');
}
const { userId } = getTokenPayload(token);
return userId;
}
} else if (authToken) {
const { userId } = getTokenPayload(authToken);
return userId;
}
throw new Error('Not authenticated');
}
module.exports = {
APP_SECRET,
getUserId
APP_SECRET
};