This commit is contained in:
Riccardo
2021-08-16 22:13:26 +02:00
parent c503984d92
commit 7939d15537
19 changed files with 1646 additions and 1755 deletions

View File

@@ -10,6 +10,7 @@
"@prisma/client": "^2.12.1",
"apollo-server": "^2.19.0",
"bcryptjs": "2.4.3",
"graphql-depth-limit": "^1.1.0",
"jsonwebtoken": "8.5.1"
},
"devDependencies": {

Binary file not shown.

View File

@@ -1,5 +1,6 @@
const { ApolloServer, PubSub } = require('apollo-server');
const { PrismaClient } = require('@prisma/client');
const depthLimit = require('graphql-depth-limit');
const Query = require('./resolvers/Query');
const Mutation = require('./resolvers/Mutation');
const Subscription = require('./resolvers/Subscription');
@@ -31,6 +32,7 @@ const server = new ApolloServer({
'utf8'
),
resolvers,
validationRules: [depthLimit(4)],
context: ({ req }) => {
return {
...req,

View File

@@ -1,11 +1,12 @@
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const utils = require('../utils');
const { APP_SECRET } = require('../utils');
function post(parent, args, context, info) {
async function post(parent, args, context, info) {
const { userId } = context;
const newLink = context.prisma.link.create({
const newLink = await context.prisma.link.create({
data: {
url: args.url,
description: args.description,
@@ -13,30 +14,19 @@ function post(parent, args, context, info) {
}
});
context.pubsub.publish('NEW_LINK', newLink);
return newLink;
}
// async function post(parent, args, context, info) {
// const { userId } = context;
// const newLink = await context.prisma.link.create({
// data: {
// url: args.url,
// description: args.description,
// postedBy: { connect: { id: userId } }
// }
// });
// context.pubsub.publish('NEW_LINK', newLink);
// return newLink;
// }
async function signup(parent, args, context, info) {
const password = await bcrypt.hash(args.password, 10);
const user = await context.prisma.user.create({
data: { ...args, password }
});
const token = jwt.sign({ userId: user.id }, APP_SECRET);
const token = utils.setToken(user.id);
return {
token,
@@ -56,11 +46,12 @@ async function login(parent, args, context, info) {
args.password,
user.password
);
if (!valid) {
throw new Error('Invalid password');
}
const token = jwt.sign({ userId: user.id }, APP_SECRET);
const token = utils.setToken(user.id);
return {
token,

View File

@@ -18,7 +18,6 @@ async function feed(parent, args, context, info) {
const count = await context.prisma.link.count({ where });
return {
id: 'main-feed',
links,
count
};

View File

@@ -1,26 +1,16 @@
type Query {
info: String!
feed(
filter: String
skip: Int
take: Int
orderBy: LinkOrderByInput
): Feed!
feed(filter: String, skip: Int, take: Int, orderBy: LinkOrderByInput): Feed!
}
type Feed {
id: ID!
links: [Link!]!
count: Int!
}
type Mutation {
post(url: String!, description: String!): Link!
signup(
email: String!
password: String!
name: String!
): AuthPayload
signup(email: String!, password: String!, name: String!): AuthPayload
login(email: String!, password: String!): AuthPayload
vote(linkId: ID!): Vote
}

View File

@@ -5,7 +5,11 @@ function getTokenPayload(token) {
return jwt.verify(token, APP_SECRET);
}
function getUserId(req, authToken) {
function setToken(user_id) {
return jwt.sign({ userId: user_id }, APP_SECRET);
}
function getUserId(req) {
if (req) {
const authHeader = req.headers.authorization;
if (authHeader) {
@@ -16,9 +20,6 @@ function getUserId(req, authToken) {
const { userId } = getTokenPayload(token);
return userId;
}
} else if (authToken) {
const { userId } = getTokenPayload(authToken);
return userId;
}
throw new Error('Not authenticated');
@@ -26,5 +27,6 @@ function getUserId(req, authToken) {
module.exports = {
APP_SECRET,
setToken,
getUserId
};

File diff suppressed because it is too large Load Diff