diff --git a/client/src/components/Login.js b/client/src/components/Login.js deleted file mode 100644 index db1e928..0000000 --- a/client/src/components/Login.js +++ /dev/null @@ -1,133 +0,0 @@ -import React, { useState } from 'react'; -import { useHistory } from 'react-router'; -import { useMutation, gql } from '@apollo/client'; -import { AUTH_TOKEN } from '../constants'; - -const SIGNUP_MUTATION = gql` - mutation SignupMutation( - $email: String! - $password: String! - $name: String! - ) { - signup( - email: $email - password: $password - username: $name - ) { - token - } - } -`; - -const LOGIN_MUTATION = gql` - mutation LoginMutation( - $email: String! - $password: String! - ) { - login(email: $email, password: $password) { - token - } - } -`; - -const Login = () => { - const history = useHistory(); - - const [formState, setFormState] = useState({ - login: true, - email: '', - password: '', - name: '' - }); - - const [login] = useMutation(LOGIN_MUTATION, { - variables: { - email: formState.email, - password: formState.password - }, - onCompleted: ({ login }) => { - localStorage.setItem(AUTH_TOKEN, login.token); - history.push('/'); - } - }); - - const [signup] = useMutation(SIGNUP_MUTATION, { - variables: { - name: formState.name, - email: formState.email, - password: formState.password - }, - onCompleted: ({ signup }) => { - localStorage.setItem(AUTH_TOKEN, signup.token); - history.push('/'); - } - }); - - return ( -
-

- {formState.login ? 'Login' : 'Sign Up'} -

-
- {!formState.login && ( - - setFormState({ - ...formState, - name: e.target.value - }) - } - type="text" - placeholder="Your name" - /> - )} - - setFormState({ - ...formState, - email: e.target.value - }) - } - type="text" - placeholder="Your email address" - /> - - setFormState({ - ...formState, - password: e.target.value - }) - } - type="password" - placeholder="Choose a safe password" - /> -
-
- - -
-
- ); -}; - -export default Login; \ No newline at end of file diff --git a/client/src/components/Product.js b/client/src/components/Product.js deleted file mode 100644 index 53d6e1b..0000000 --- a/client/src/components/Product.js +++ /dev/null @@ -1,14 +0,0 @@ -import React from 'react'; - -const Product = (props) => { - const { product } = props; - return ( -
-
- {product.title}: only {product.qty}! -
-
- ); -}; - -export default Product; \ No newline at end of file diff --git a/client/src/components/ProductList.js b/client/src/components/ProductList.js deleted file mode 100644 index 972735f..0000000 --- a/client/src/components/ProductList.js +++ /dev/null @@ -1,40 +0,0 @@ -import React from 'react'; -import Product from './Product'; -import { useQuery, gql } from '@apollo/client'; - -const FEED_QUERY = gql` - { - allProducts{ - title - qty - } - } -`; - -const ProductList = () => { - - const { data } = useQuery(FEED_QUERY); - - console.log("Data:", data); - - if (data !== undefined) { - return ( -
- { - data.allProducts.map((product) => ( - - )) - } -
- ); - } else { - return ( -
- Rendering... -
- ) - } - -}; - -export default ProductList; \ No newline at end of file diff --git a/client/src/components/Search.js b/client/src/components/Search.js index afd5600..b534f5e 100644 --- a/client/src/components/Search.js +++ b/client/src/components/Search.js @@ -11,10 +11,6 @@ const FEED_SEARCH_QUERY = gql` title description type - createdBy { - id - username - } } } } diff --git a/server/src/index.js b/server/src/index.js index efdc0cb..fccb3c2 100644 --- a/server/src/index.js +++ b/server/src/index.js @@ -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, diff --git a/server/src/models/product.js b/server/src/models/product.js deleted file mode 100644 index 917b4b9..0000000 --- a/server/src/models/product.js +++ /dev/null @@ -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); \ No newline at end of file diff --git a/server/src/models/user.js b/server/src/models/user.js deleted file mode 100644 index d9e1472..0000000 --- a/server/src/models/user.js +++ /dev/null @@ -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); \ No newline at end of file diff --git a/server/src/resolvers.js b/server/src/resolvers.js index 462c452..1f727cb 100644 --- a/server/src/resolvers.js +++ b/server/src/resolvers.js @@ -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 - }); - }, } }; \ No newline at end of file diff --git a/server/src/resolvers/Appointment.js b/server/src/resolvers/Appointment.js index 547c2d1..3aaede9 100644 --- a/server/src/resolvers/Appointment.js +++ b/server/src/resolvers/Appointment.js @@ -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 }; diff --git a/server/src/resolvers/Follow.js b/server/src/resolvers/Follow.js deleted file mode 100644 index b58293f..0000000 --- a/server/src/resolvers/Follow.js +++ /dev/null @@ -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 -}; diff --git a/server/src/resolvers/Mutation.js b/server/src/resolvers/Mutation.js index c98e157..8d2ff8a 100644 --- a/server/src/resolvers/Mutation.js +++ b/server/src/resolvers/Mutation.js @@ -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 }; diff --git a/server/src/resolvers/Subscription.js b/server/src/resolvers/Subscription.js index bb4d0b5..8653d6d 100644 --- a/server/src/resolvers/Subscription.js +++ b/server/src/resolvers/Subscription.js @@ -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 } \ No newline at end of file diff --git a/server/src/schema.graphql b/server/src/schema.graphql index 1686dcc..fd01e46 100644 --- a/server/src/schema.graphql +++ b/server/src/schema.graphql @@ -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 diff --git a/server/src/utils.js b/server/src/utils.js index f8cbbc0..2a57b71 100644 --- a/server/src/utils.js +++ b/server/src/utils.js @@ -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 };