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

facebook and google oauth added #373

Closed
wants to merge 3 commits into from
Closed
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
30 changes: 28 additions & 2 deletions graphql/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,37 @@ 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 +120,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
6 changes: 6 additions & 0 deletions graphql/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ 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 +120,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 @@ -81,7 +81,7 @@ mongoose
{ port: 4000 },
console.log(
`Server ready at http://localhost:4000${server.graphqlPath}\n` +
`Subscriptions endpoint at ws://localhost:4000${server.subscriptionsPath}`
`Subscriptions endpoint at ws://localhost:4000${server.subscriptionsPath}`
)
)
)
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
Loading