Skip to content

Commit

Permalink
resolvers for oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
RunTerror committed Feb 7, 2024
1 parent 0a3017f commit 24d89cb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
33 changes: 31 additions & 2 deletions graphql/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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");

Expand All @@ -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": {
Expand Down
7 changes: 7 additions & 0 deletions graphql/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!
Expand Down
2 changes: 1 addition & 1 deletion index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions permissions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const permissions = shield({
},
Mutation: {
"*": isAuthenticated,
oAuth: not(isAuthenticated),
register: not(isAuthenticated),
login: not(isAuthenticated),
},
Expand Down

0 comments on commit 24d89cb

Please sign in to comment.