-
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
Changes from 4 commits
593ff01
5f666e9
0336687
6759e02
0f8020a
8dacaa7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
JWT_SECRET=thecountryroads | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"env": { | ||
"browser": true, | ||
"node": true, | ||
"commonjs": true, | ||
"es2021": true | ||
}, | ||
|
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 was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const {DataSource} = require('apollo-datasource'); | ||
const {UserInputError} = require('apollo-server'); | ||
const crypto = require('crypto'); | ||
|
||
class Post { | ||
constructor (data) { | ||
this.id = crypto.randomBytes(16).toString('hex'); | ||
this.voters = []; | ||
this.votes = 0; | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Object.assign(this, data); | ||
} | ||
} | ||
|
||
class PostsDataSource extends DataSource { | ||
|
||
constructor() { | ||
super(); | ||
this.posts = []; | ||
} | ||
|
||
async allPosts () { | ||
return this.posts; | ||
} | ||
|
||
async createPost (data, currUser) { | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if(currUser){ | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const newPost = new Post({...data, author:currUser}); | ||
this.posts.push(newPost); | ||
|
||
return newPost; | ||
} | ||
|
||
return null; | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
async votePost(id, value, currUser) { | ||
const post = this.posts.find(e => e.id == id); | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (post) { | ||
|
||
if(currUser) { | ||
|
||
if(!post.voters.find(voter => voter.id == currUser.id)){ | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
post.votes+= value; | ||
post.voters.push(currUser); | ||
|
||
return post; | ||
} | ||
else{ | ||
throw new UserInputError("This user voted on this post already"); | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
else{ | ||
throw new UserInputError("No post with this ID", {invalidArgs: [id]}); | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
async deletePost (id, currUser) { | ||
|
||
const post = this.posts.find(e => e.id == id); | ||
|
||
if (post) { | ||
|
||
if(currUser) { | ||
|
||
if( currUser.id == post.author.id) { | ||
|
||
const deleteIndex = this.posts.indexOf(post); | ||
this.posts.splice(deleteIndex,1); | ||
|
||
return post; | ||
|
||
}else { | ||
throw new Error("Only authors of a post are allowed to delete their own posts."); | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
else{ | ||
throw new UserInputError("No post with this ID", {invalidArgs: [id]}); | ||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
} | ||
} | ||
module.exports = {Post, PostsDataSource} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,65 @@ | ||||||||
const {DataSource} = require('apollo-datasource'); | ||||||||
const {createAccessToken} = require('../token') | ||||||||
const crypto = require('crypto'); | ||||||||
const bcrypt = require('bcrypt'); | ||||||||
|
||||||||
class User{ | ||||||||
constructor(data){ | ||||||||
this.id = crypto.randomBytes(16).toString('hex') | ||||||||
data.password = bcrypt.hashSync(data.password, 10); | ||||||||
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.
Suggested change
|
||||||||
Object.assign(this, data); | ||||||||
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. Actually, it would be better to assign only white listed arguments here:
Suggested change
that way you don't save the clear text password in the user. |
||||||||
} | ||||||||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
} | ||||||||
|
||||||||
class UsersDataSource extends DataSource { | ||||||||
|
||||||||
constructor() { | ||||||||
super(); | ||||||||
this.users = [] | ||||||||
} | ||||||||
|
||||||||
async getUser(id) { | ||||||||
return this.users.find(user => user.id === id); | ||||||||
} | ||||||||
|
||||||||
getUserByEmail(email) { | ||||||||
return this.users.find(user => user.email === email); | ||||||||
} | ||||||||
|
||||||||
async signup(name, email, password) { | ||||||||
//trim | ||||||||
email = email.trim(); | ||||||||
password = password.trim(); | ||||||||
|
||||||||
/* | ||||||||
Validate: | ||||||||
- Make sure the email address is not taken by another user. | ||||||||
- Accept only passwords with a length of at least 8 characters. | ||||||||
*/ | ||||||||
if(this.getUserByEmail(email) || password.length < 8){ | ||||||||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
return null; | ||||||||
} | ||||||||
|
||||||||
const newUser = new User({name, email, password}); | ||||||||
this.users.push(newUser); | ||||||||
console.log(this.users); | ||||||||
|
||||||||
return createAccessToken(newUser.id); | ||||||||
} | ||||||||
|
||||||||
async login(email, password) { | ||||||||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
let user = this.getUserByEmail(email); | ||||||||
console.log(user); | ||||||||
|
||||||||
if(user && bcrypt.compareSync(password, user.password)){ | ||||||||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
return createAccessToken(user.id); | ||||||||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
} | ||||||||
|
||||||||
return null; | ||||||||
ilonae marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
} | ||||||||
|
||||||||
async allUsers() { | ||||||||
return this.users; | ||||||||
} | ||||||||
} | ||||||||
module.exports = {User, UsersDataSource} |
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("context here", 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
|
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.