Skip to content

Commit

Permalink
Add json web token for authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
StyvenSoft committed Jan 6, 2021
1 parent 85e51c6 commit 92c7ec0
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ build/Release
# Dependency directories
node_modules/
jspm_packages/
config/

# TypeScript v1 declaration files
typings/
Expand Down
23 changes: 0 additions & 23 deletions config/config.json

This file was deleted.

12 changes: 11 additions & 1 deletion graphql/resolvers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { User } = require('../models');
const bcrypt = require('bcryptjs');
const { UserInputError, AuthenticationError } = require('apollo-server');
const { JWT_SECRET } = require('../config/env.json');
const jwt = require('jsonwebtoken');

module.exports = {
Query: {
Expand Down Expand Up @@ -38,7 +40,15 @@ module.exports = {
throw new AuthenticationError('Password is incorrect', { errors })
}

return user;
const token = jwt.sign({
data: 'foobar'
}, JWT_SECRET, { expiresIn: 60 * 60 });

return {
...user.toJSON(),
createdAt: user.createdAt.toISOString(),
token,
};
} catch (error) {
console.log(error);
throw error;
Expand Down
2 changes: 2 additions & 0 deletions graphql/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module.exports = gql`
type User {
username: String!
email: String!
createdAt: String!
token: String
}
type Query {
getUsers: [User]!
Expand Down
94 changes: 92 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"apollo-server": "^2.19.1",
"bcryptjs": "^2.4.3",
"graphql": "^15.4.0",
"jsonwebtoken": "^8.5.1",
"mysql2": "^2.2.5",
"sequelize": "^6.3.5"
},
Expand Down

0 comments on commit 92c7ec0

Please sign in to comment.