-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Step 7.2: Add jwt middleware to express
- Loading branch information
1 parent
0136bb9
commit 10842b3
Showing
1 changed file
with
27 additions
and
0 deletions.
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 |
---|---|---|
@@ -1,14 +1,41 @@ | ||
import { ApolloServer } from 'apollo-server'; | ||
import jwt from 'express-jwt'; | ||
|
||
import { typeDefs } from './data/schema'; | ||
import { mocks } from './data/mocks'; | ||
import { resolvers } from './data/resolvers'; | ||
import { JWT_SECRET } from './config'; | ||
import { User } from './data/connectors'; | ||
|
||
const PORT = 8080; | ||
|
||
const server = new ApolloServer({ | ||
resolvers, | ||
typeDefs, | ||
// mocks, | ||
context: ({ req, res, connection }) => { | ||
// web socket subscriptions will return a connection | ||
if (connection) { | ||
// check connection for metadata | ||
return {}; | ||
} | ||
|
||
const user = new Promise((resolve, reject) => { | ||
jwt({ | ||
secret: JWT_SECRET, | ||
credentialsRequired: false, | ||
})(req, res, (e) => { | ||
if (req.user) { | ||
resolve(User.findOne({ where: { id: req.user.id } })); | ||
} else { | ||
resolve(null); | ||
} | ||
}); | ||
}); | ||
return { | ||
user, | ||
}; | ||
}, | ||
}); | ||
|
||
server.listen({ port: PORT }).then(({ url }) => console.log(`🚀 Server ready at ${url}`)); |