- Clone all files into a directory
- Run
yarn
- Run
export JWT_SECRET="your_jwt_secret_key"
- Run
node server.js
- Run
node client-auth.js
- See a console log lists of posts fetched from GraphQL
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
const { ApolloServer, gql } = require('apollo-server'); | |
const { validateToken } = require("./authService"); | |
const resolvers = require("./resolvers"); | |
const typeDefs = gql` | |
type User { | |
id: ID! | |
email: String! | |
password: String! |
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
const axios = require("axios"); | |
axios.defaults.baseURL = "http://localhost:4000/"; | |
const login = async (email, password) => { | |
const axiosResponse = await axios.post('/', { | |
query: `mutation Login { | |
login(email: "${email}", password: "${password}") { | |
token | |
} |
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
const posts = require("./posts"); | |
module.exports = { | |
Query: { | |
posts: () => { | |
return posts; | |
} | |
} | |
}; |
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
const { ApolloServer, gql } = require('apollo-server'); | |
const resolvers = require("./resolvers"); | |
const typeDefs = gql` | |
type Post { | |
title: String! | |
authorId: String! | |
} |
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
const jwt = require('jsonwebtoken'); | |
module.exports = { | |
authenticate: (email, password) => { | |
let user; | |
// Implement the credential validation however you'd like. | |
if (email.length && password.length) { | |
user = { | |
id: 1, |
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
const posts = require("./posts"); | |
const { authenticate } = require("./authService"); | |
module.exports = { | |
Query: { | |
posts: (_parent, _args, context, _info) => { | |
return posts.filter(post => { | |
const isAuthor = post.authorId === context.user.id; | |
const isAdmin = context.user.roles.includes("admin"); | |
return isAuthor || isAdmin; |
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
const posts = require("./posts"); | |
module.exports = { | |
Query: { | |
posts: (_parent, _args, context, _info) => { | |
return posts.filter(post => { | |
const isAuthor = post.authorId === context.user.id; | |
const isAdmin = context.user.roles.includes("admin"); | |
return isAuthor || isAdmin; | |
}); |
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
const posts = require("./posts"); | |
module.exports = { | |
Query: { | |
posts: (_parent, _args, context, _info) => { | |
return posts.filter(post => post.authorId === context.user.id); | |
} | |
} | |
}; |
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
module.exports = [ | |
{ | |
authorId: 1, | |
title: "Post 1" | |
}, | |
{ | |
authorId: 1, | |
title: "Post 2" | |
}, | |
{ |
NewerOlder