From 24d89cb310bfe2f82331484fd864f5ec14ebafba Mon Sep 17 00:00:00 2001 From: Abhishek Date: Wed, 7 Feb 2024 22:19:27 +0530 Subject: [PATCH 1/2] resolvers for oauth --- graphql/resolvers.js | 33 +++++++++++++++++++++++++++++++-- graphql/schema.js | 7 +++++++ index.mjs | 2 +- permissions/index.js | 1 + 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/graphql/resolvers.js b/graphql/resolvers.js index 3714d37..2293c4c 100644 --- a/graphql/resolvers.js +++ b/graphql/resolvers.js @@ -9,6 +9,7 @@ const Group = require("../models/group.js"); const Landmark = require("../models/landmark.js"); const { User } = require("../models/user.js"); const { MongoServerError } = require("mongodb"); +const user = require("../models/user.js"); const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // even if we generate 10 IDs per hour, @@ -73,10 +74,39 @@ const resolvers = { password: await bcrypt.hash(credentials.password, 10), }), }); + console.log(newUser); const userObj = await newUser.save(); return userObj; }, + oAuth: async (_parent, { userInput }) => { + + const { name, email } = userInput; + let user = await User.findOne({ email }); + + if (!user) { + const newUser = new User({ name, email }); + user = await newUser.save(); + } + + const anon = false; + const tokenPayload = { + "https://beacon.ccextractor.org": { + anon, + ...(email && { email }), + }, + }; + + const token = jwt.sign(tokenPayload, process.env.JWT_SECRET, { + algorithm: "HS256", + subject: user._id.toString(), + expiresIn: "7d", + }); + + return token; + }, + + login: async (_parent, { id, credentials }) => { if (!id && !credentials) return new UserInputError("One of ID and credentials required"); @@ -93,11 +123,10 @@ const resolvers = { let anon = true; if (credentials) { - const valid = email === user.email && (await bcrypt.compare(password, user.password)); + const valid = (email === user.email && bcrypt.compare(password, user.password)); if (!valid) return new AuthenticationError("credentials don't match"); anon = false; } - return jwt.sign( { "https://beacon.ccextractor.org": { diff --git a/graphql/schema.js b/graphql/schema.js index c0d0662..ad7dafd 100644 --- a/graphql/schema.js +++ b/graphql/schema.js @@ -104,6 +104,12 @@ const typeDefs = gql` hello: String } + + input oAuthInput { + email: String + name: String + } + type Mutation { """ if start time not supplied, default is Date.now @@ -115,6 +121,7 @@ const typeDefs = gql` one of ID or credentials required (ID for anon) """ login(id: ID, credentials: AuthPayload): String + oAuth(userInput: oAuthInput): String joinBeacon(shortcode: String!): Beacon! updateBeaconLocation(id: ID!, location: LocationInput!): Beacon! updateUserLocation(id: ID!, location: LocationInput!): User! diff --git a/index.mjs b/index.mjs index 0f68fc2..a8edef8 100644 --- a/index.mjs +++ b/index.mjs @@ -14,7 +14,7 @@ import { permissions } from "./permissions/index.js"; import pubsub from "./pubsub.js"; const server = new ApolloServer({ - schema: applyMiddleware(makeExecutableSchema({ typeDefs, resolvers }), permissions), + schema: applyMiddleware(makeExecutableSchema({ typeDefs, resolvers }),permissions), // schema: makeExecutableSchema({ typeDefs, resolvers }), // to temp disable shield on dev context: async ({ req, connection }) => { // initialize context even if it comes from subscription connection diff --git a/permissions/index.js b/permissions/index.js index e394e83..f5a163f 100644 --- a/permissions/index.js +++ b/permissions/index.js @@ -8,6 +8,7 @@ const permissions = shield({ }, Mutation: { "*": isAuthenticated, + oAuth: not(isAuthenticated), register: not(isAuthenticated), login: not(isAuthenticated), }, From 736e6999867d3564631de01b8d85e95929e8b5a9 Mon Sep 17 00:00:00 2001 From: Abhishek Date: Thu, 8 Feb 2024 12:46:37 +0530 Subject: [PATCH 2/2] beacons leader fixed --- graphql/resolvers.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/graphql/resolvers.js b/graphql/resolvers.js index 2293c4c..6a55a5a 100644 --- a/graphql/resolvers.js +++ b/graphql/resolvers.js @@ -9,7 +9,6 @@ const Group = require("../models/group.js"); const Landmark = require("../models/landmark.js"); const { User } = require("../models/user.js"); const { MongoServerError } = require("mongodb"); -const user = require("../models/user.js"); const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // even if we generate 10 IDs per hour, @@ -37,12 +36,20 @@ const resolvers = { return beacon; }, group: async (_parent, { id }, { user }) => { - const group = await Group.findById(id).populate("leader members beacons"); - if (!group) return new UserInputError("No group exists with that id."); - // return error iff user not in group - if (group.leader.id !== user.id && !group.members.includes(user)) - return new Error("User should be a part of the group"); - return group; + const group = await Group.findById(id).populate('leader members').populate({ + path: 'beacons', + populate: { + path: 'leader', + }, + }); + + if (!group) return new UserInputError("No group exists with that id."); + // Check if the user is part of the group + if (group.leader.id !== user.id && !group.members.includes(user)) + throw new Error("User should be a part of the group"); + + console.log(`group: ${group}`); + return group; }, nearbyBeacons: async (_, { location }) => { // get active beacons