-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Exercise 4 #7
Closed
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
593ff01
Add JWT, Graphql-middleware, Graphql-shield, signup, bcrypt, dotenv
yiimnta 5f666e9
test login, update createPost, votePost
yiimnta 0336687
temp
ilonae 6759e02
adding login, edit createPost, votePost
yiimnta 0f8020a
add jest test
yiimnta 8dacaa7
adding intermediate fixes
ilonae File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
JWT_SECRET=thecountryroads | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,14 @@ | |
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"apollo-datasource-rest": "^0.9.5", | ||
"apollo-datasource": "^0.7.2", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yiimnta Danke, dass du das geändert hast! |
||
"apollo-server": "^2.19.0", | ||
"bcrypt": "^5.0.0", | ||
"dotenv": "^8.2.0", | ||
"graphql": "^15.4.0", | ||
"graphql-middleware": "^4.0.2", | ||
"graphql-shield": "^7.4.2", | ||
"jsonwebtoken": "^8.5.1", | ||
"nodemon": "^2.0.6" | ||
}, | ||
"devDependencies": { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const {rule} = require('graphql-shield'); | ||
|
||
const isAuthenticated = rule({ cache: 'contextual' })( | ||
async (parent, args, context, info) => { | ||
return !!(await context.dataSources.usersDataSrc.getUser(context.decodedJwt.id)); | ||
}, | ||
) | ||
|
||
module.exports = {isAuthenticated} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require('dotenv').config(); | ||
const jwt = require('jsonwebtoken'); | ||
|
||
const context = ({req}) => { | ||
|
||
let token = req.headers.authorization || ''; | ||
token = token.replace('Bearer ', ''); | ||
|
||
try { | ||
const decodedJwt = jwt.verify( | ||
token, | ||
process.env.JWT_SECRET | ||
); | ||
|
||
console.log(decodedJwt) | ||
|
||
return {decodedJwt}; | ||
|
||
} catch(e) { | ||
return {} | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
module.exports = {context}; | ||
yiimnta marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,47 @@ | ||
const {gql } = require('apollo-server'); | ||
const {gql} = require('apollo-server'); | ||
|
||
const typeDefs = gql` | ||
type Post { | ||
id: ID! | ||
title: String! | ||
votes: Int! | ||
author: User! | ||
voters:[User] | ||
} | ||
|
||
type User { | ||
name: ID! | ||
posts: [Post] | ||
} | ||
|
||
type Query { | ||
posts: [Post] | ||
users: [User] | ||
} | ||
|
||
type Mutation { | ||
write(post: PostInput!): Post | ||
delete(id: ID!): Post | ||
upvote(id: ID!, voter: UserInput!): Post | ||
downvote(id: ID!, voter: UserInput!): Post | ||
} | ||
|
||
input PostInput { | ||
title: String! | ||
author: UserInput! | ||
} | ||
|
||
input UserInput { | ||
name: String! | ||
} | ||
type Post { | ||
id: ID! | ||
title: String! | ||
votes: Int! | ||
author: User! | ||
} | ||
|
||
type User { | ||
# ⚠️ attributes 'id' and 'name' have changed! | ||
# 'id' now represents a randomly generated string, similar to 'Post.id' | ||
id: ID! | ||
name: String! | ||
email: String! | ||
posts: [Post] | ||
} | ||
|
||
type Query { | ||
posts: [Post] | ||
users: [User] | ||
} | ||
|
||
type Mutation { | ||
write(post: PostInput!): Post | ||
upvote(id: ID!): Post | ||
downvote(id: ID!): Post | ||
delete(id: ID!): Post | ||
|
||
""" | ||
returns a signed JWT or null | ||
""" | ||
login(email: String!, password: String!): String | ||
|
||
""" | ||
returns a signed JWT or null | ||
""" | ||
signup(name: String!, email: String!, password: String!): String | ||
} | ||
|
||
input PostInput { | ||
title: String! | ||
} | ||
`; | ||
|
||
module.exports = typeDefs |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❌ If this stays, you will loose a ⭐ here.
You have multiple options:
a) Mock
jsonwebtoken
node module in your tests, see our forum our group lichtow: Systems-Development-and-Frameworks/lichtow@6d164b9#diff-45f14d66b6a3b7ed6473601b04ce8c30013d014dfd73e439376d93e9196f93dcR98b) Use dotenv-flow and commit a non-secret in
.env.test
c) Explain in README.md one has to create a
.env
file before running the tests.