Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation
This project
Loading...
Sign in
hfpp2012
/
merng
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 9b4cad9d
authored
Sep 17, 2019
by
qiuzhi99
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
check-auth createpost deletepost
1 parent
ddb977b9
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
76 additions
and
3 deletions
graphql/resolvers/index.js
graphql/resolvers/posts.js
graphql/typeDefs.js
index.js
util/check-auth.js
graphql/resolvers/index.js
View file @
9b4cad9
...
@@ -6,6 +6,7 @@ module.exports = {
...
@@ -6,6 +6,7 @@ module.exports = {
...
postsResolvers
.
Query
...
postsResolvers
.
Query
},
},
Mutation
:
{
Mutation
:
{
...
usersResolvers
.
Mutation
...
usersResolvers
.
Mutation
,
...
postsResolvers
.
Mutation
}
}
};
};
graphql/resolvers/posts.js
View file @
9b4cad9
const
Post
=
require
(
"../../models/Post"
);
const
Post
=
require
(
"../../models/Post"
);
const
checkAuth
=
require
(
"../../util/check-auth"
);
const
{
AuthenticationError
}
=
require
(
"apollo-server"
);
module
.
exports
=
{
module
.
exports
=
{
Query
:
{
Query
:
{
async
getPosts
()
{
async
getPosts
()
{
try
{
try
{
const
posts
=
await
Post
.
find
();
const
posts
=
await
Post
.
find
()
.
sort
({
createdAt
:
-
1
})
;
return
posts
;
return
posts
;
}
catch
(
err
)
{
}
catch
(
err
)
{
throw
new
Error
(
err
);
throw
new
Error
(
err
);
}
}
},
async
getPost
(
_
,
{
postId
})
{
try
{
const
post
=
await
Post
.
findById
(
postId
);
if
(
post
)
{
return
post
;
}
else
{
throw
new
Error
(
"Post not found"
);
}
}
catch
(
err
)
{
throw
new
Error
(
err
);
}
}
},
Mutation
:
{
async
createPost
(
_
,
{
body
},
context
)
{
const
user
=
checkAuth
(
context
);
const
newPost
=
new
Post
({
body
,
username
:
user
.
username
,
createdAt
:
new
Date
().
toISOString
(),
user
:
user
.
id
});
const
post
=
await
newPost
.
save
();
return
post
;
},
async
deletePost
(
_
,
{
postId
},
context
)
{
const
user
=
checkAuth
(
context
);
try
{
const
post
=
await
Post
.
findById
(
postId
);
if
(
user
.
username
===
post
.
username
)
{
await
post
.
delete
();
return
"Post deleted successfully"
;
}
else
{
throw
new
AuthenticationError
(
"Action not allowed"
);
}
}
catch
(
err
)
{
throw
new
Error
(
err
);
}
}
}
}
}
};
};
graphql/typeDefs.js
View file @
9b4cad9
...
@@ -10,6 +10,7 @@ module.exports = gql`
...
@@ -10,6 +10,7 @@ module.exports = gql`
type Query {
type Query {
getPosts: [Post]
getPosts: [Post]
getPost(postId: ID!): Post
}
}
type User {
type User {
...
@@ -30,5 +31,7 @@ module.exports = gql`
...
@@ -30,5 +31,7 @@ module.exports = gql`
type Mutation {
type Mutation {
register(registerInput: RegisterInput): User!
register(registerInput: RegisterInput): User!
login(username: String!, password: String!): User!
login(username: String!, password: String!): User!
createPost(body: String!): Post!
deletePost(postId: ID!): String!
}
}
`
;
`
;
index.js
View file @
9b4cad9
...
@@ -9,7 +9,8 @@ const resolvers = require("./graphql/resolvers");
...
@@ -9,7 +9,8 @@ const resolvers = require("./graphql/resolvers");
const
server
=
new
ApolloServer
({
const
server
=
new
ApolloServer
({
typeDefs
,
typeDefs
,
resolvers
resolvers
,
context
:
({
req
})
=>
({
req
})
});
});
mongoose
mongoose
...
...
util/check-auth.js
0 → 100644
View file @
9b4cad9
const
jwt
=
require
(
"jsonwebtoken"
);
const
{
SECRET_KEY
}
=
require
(
"../config"
);
const
{
AuthenticationError
}
=
require
(
"apollo-server"
);
module
.
exports
=
context
=>
{
const
authHeader
=
context
.
req
.
headers
.
authorization
;
if
(
authHeader
)
{
const
token
=
authHeader
.
split
(
"Bearer "
)[
1
];
if
(
token
)
{
try
{
const
user
=
jwt
.
verify
(
token
,
SECRET_KEY
);
return
user
;
}
catch
(
error
)
{
throw
new
AuthenticationError
(
"Invalid/Expired token"
);
}
}
throw
new
Error
(
"Authentication token must be 'Bearer [token]"
);
}
throw
new
Error
(
"Authorization header must be provided"
);
};
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment