Skip to content
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

Lookup user in graphql context #297

Merged
merged 2 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion runs/graphql/src/resolvers/me.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getUser } from "../services/firebase.js";

export const me = async (parent, args, context) => {
const doc = await getUser(context.user.user_id);
const doc = await getUser(context.token.user_id);
if (!doc.exists) {
throw Error("No such user!");
}
Expand Down
2 changes: 1 addition & 1 deletion runs/graphql/src/resolvers/me.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test("me to return", async () => {
data: () => fakeUser
})
);
const user = await me({}, {}, { user: { user_id: "fake" } });
const user = await me({}, {}, { token: { user_id: "fake" } });
expect(user).toEqual(fakeUser);
expect(getUser).lastCalledWith("fake");
});
4 changes: 2 additions & 2 deletions runs/graphql/src/resolvers/updateprofile.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { updateUser } from "../services/firebase.js";

export const updateProfile = async (parent, arg, context) => {
if (!context.user) {
if (!context.token) {
throw new Error("Can't update with no user");
}
const data = await updateUser(context.user.user_id, arg.input);
const data = await updateUser(context.token.user_id, arg.input);
return arg.input;
};
2 changes: 1 addition & 1 deletion runs/graphql/src/resolvers/updateprofile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test("updateProfile with user", async () => {
const profile = await updateProfile(
null,
{ input: userInput },
{ user: { user_id: "donald" } }
{ token: { user_id: "donald" } }
);
expect(profile).toEqual(userInput);
expect(updateUser).lastCalledWith("donald", {
Expand Down
11 changes: 9 additions & 2 deletions runs/graphql/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ import { makeExecutableSchema } from "graphql-tools";
const { typeDefs, resolvers } = require("./schema.js");
const http = require("http");
const admin = require("firebase-admin");
const { decodeJWT } = require("./services/firebase.js");
const { decodeJWT, getUser } = require("./services/firebase.js");

export const context = async ({ req }) => {
const token = (req.headers.authorization || "").replace("bearer ", "");

try {
const decodedToken = await decodeJWT(token);
return { user: decodedToken };
if (decodedToken) {
const user = await getUser(decodedToken.user_id);
return { token: decodedToken, user: user.data() };
}
return {
token: undefined,
user: undefined
};
} catch (e) {
if (e.code === "auth/argument-error") {
return {};
Expand Down
26 changes: 24 additions & 2 deletions runs/graphql/src/server.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
jest.mock(
"../data/videos.js",
() => {
return {
videos: []
};
},
{ virtual: true }
);

import jwt from "jsonwebtoken";
import { context } from "./server.js";
jest.mock("./services/firebase.js");
const { decodeJWT } = require("./services/firebase.js");
const { decodeJWT, getUser } = require("./services/firebase.js");

test("context defined", () => {
expect(context).toBeDefined();
Expand All @@ -25,17 +35,29 @@ test("context - good token", async () => {
sign_in_provider: "password"
}
};

const testToken = jwt.sign(officialPayload, "test");
decodeJWT.mockReturnValueOnce(Promise.resolve(jwt.verify(testToken, "test")));

const user = {
name: "Donald Duck"
};
getUser.mockReturnValueOnce({
data: () => user
});

const value = await context({
req: {
headers: {
authorization: "bearer " + testToken
}
}
});

expect(getUser).toHaveBeenCalledWith(officialPayload.user_id);
expect(value).toEqual({
user: officialPayload
token: officialPayload,
user
});
});

Expand Down