summaryrefslogtreecommitdiffstats
path: root/backend/src/resolvers
diff options
context:
space:
mode:
authorWes Bos <wesbos@gmail.com>2018-05-15 22:15:36 -0400
committerWes Bos <wesbos@gmail.com>2018-05-15 22:15:36 -0400
commit3f9b14e3c1d7f47d9b3e48b94b3b5bf2c722205e (patch)
tree63373ce8c75e51305b7899af6c53903ef6c4f179 /backend/src/resolvers
parentae1a94b08f5aba0c16c6536e388b4dd4a53120fd (diff)
migrate to cookies for jwt
Diffstat (limited to 'backend/src/resolvers')
-rw-r--r--backend/src/resolvers/Mutation.js24
-rw-r--r--backend/src/resolvers/Query.js3
2 files changed, 20 insertions, 7 deletions
diff --git a/backend/src/resolvers/Mutation.js b/backend/src/resolvers/Mutation.js
index 268b348..153833d 100644
--- a/backend/src/resolvers/Mutation.js
+++ b/backend/src/resolvers/Mutation.js
@@ -22,10 +22,18 @@ const mutations = {
info
);
- return {
- token: jwt.sign({ userId: user.id }, process.env.APP_SECRET),
- user,
- };
+ const token = jwt.sign({ userId: user.id }, process.env.APP_SECRET);
+ ctx.response.cookie('token', token, {
+ maxAge: 1000 * 60 * 60 * 24 * 365,
+ httpOnly: true,
+ });
+ return { user };
+ },
+
+ async signout(parent, args, ctx, info) {
+ ctx.response.clearCookie('token');
+ // TODO: What do we return here?
+ return { id: 'abc123' };
},
async signin(parent, { email, password }, ctx, info) {
@@ -38,8 +46,14 @@ const mutations = {
if (!valid) {
throw new Error('Invalid password');
}
+ // set the cookie
+ const token = jwt.sign({ userId: user.id }, process.env.APP_SECRET);
+ ctx.response.cookie('token', token, {
+ maxAge: 1000 * 60 * 60 * 24 * 365,
+ httpOnly: true,
+ });
return {
- token: jwt.sign({ userId: user.id }, process.env.APP_SECRET),
+ token,
user,
};
},
diff --git a/backend/src/resolvers/Query.js b/backend/src/resolvers/Query.js
index 276f7fa..b1e0c5b 100644
--- a/backend/src/resolvers/Query.js
+++ b/backend/src/resolvers/Query.js
@@ -36,8 +36,7 @@ const Query = {
},
me(parent, args, ctx, info) {
- const Authorization = ctx.request.get('Authorization');
- if (!Authorization || Authorization === 'null') {
+ if (!ctx.request.userId) {
return null; // don't error out, just return nothing
}