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

Add Discord verification to user profiles #12

Merged
merged 3 commits into from May 19, 2022
Merged

Conversation

deammer
Copy link
Contributor

@deammer deammer commented May 19, 2022

Enable users to tie their Discord profile with their OSH account. Users who are logged in will now see a "Verify with Discord" button on their own profile page. This takes them through the OAuth flow, allowing us to store their Discord id in our database. We don't store anything else! Once we have a Discord id, we know users have verified and we can display a badge on their profile (that only they can see, for privacy.)

Here's what that looks like:

Screen.Recording.2022-05-19.at.1.12.19.PM.mov

@vercel
Copy link

vercel bot commented May 19, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
opensourcehub ✅ Ready (Inspect) Visit Preview May 19, 2022 at 11:19PM (UTC)

@ghost
Copy link

ghost commented May 19, 2022

CodeSee Review Map:

Review these changes using an interactive CodeSee Map

Review in an interactive map

View more CodeSee Maps

Legend

CodeSee Map Legend

Copy link

@cubes cubes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty good! I made a bunch of suggestions, questions, and comments inline, mostly to delete or improve some comments in the code. Most are non-blocking, please take a moment to review, and possibly address before merging this?

General thought/suggestion: Given that we're using a NoSQL store, do we have any documentation of the structure/schema of the objects we're storing in it? I think such a document could be useful given that the store itself is effectively schemaless.

:🚢

import { updateUser } from "~/database.server";

export const loader: LoaderFunction = async ({ request }) => {
const session = await getSession(request.headers.get("Cookie"));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought/suggestion (non-blocking): The code to get the session and currentUser, and redirect to /login if the user is not logged in feels like it will repeat a lot. Does @remix-run have any concept of middleware? That feels like the place for this sort of thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw new Error("No code");
}

// Grab an access token from the user's code
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion (non-blocking): This comment feels a little redundant given that you chose good names for the variable and function below. Consider deleting? If the function being called doesn't have a docstring, consider adding one where it is defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I'll remove it.

// Grab an access token from the user's code
const accessToken = await getDiscordAccessToken(code);

// Grab the user's Discord id
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion (non-blocking): This comment feels a little redundant given that you chose good names for the variable and function below. Consider deleting? If the function being called doesn't have a docstring, consider adding one where it is defined?

// Grab the user's Discord id
const discordUserId = await getDiscordUserId(accessToken);

// Save the user's Discord id in our database
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion (non-blocking): This comment feels a little redundant given that you chose good names for the code below. Consider deleting?

};

export const action: ActionFunction = async () => {
// Make sure the user is logged in
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion (blocking): I think these comments could be deleted or improved. Are they TODO statements? Are they saying what the code does?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They were TODOs from when I was figuring out how to do the thing. I'll remove them!

@@ -124,6 +123,26 @@ export async function updateProfileForUser(
.set(updatedProfile, { merge: true });
}

/**
* Updates a User in the database. The following fields are stripped from the
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Praise: Thanks for writing good docstrings!

import DiscordOauth2 from "discord-oauth2";

const DISCORD_REDIRECT_URI =
process.env.NODE_ENV === "production"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought (non-blocking): If we do checks like this in many places, we might want to adopt something like the codesee_metaconfig.json file for storing non-sensitive constants that are env specific.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. This is the only place where we're doing this so far but I'll keep an eye on it.

? "https://opensourcehub.io/verify-discord"
: "http://localhost:3000/verify-discord";

const DISCORD_SCOPES = ["identify"];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion (non-blocking): Is there any documentation of Discord's available scopes? Maybe add a link to such documentation in a comment?


const token = await discordAuth.tokenRequest({
clientId: process.env.DISCORD_CLIENT_ID,
clientSecret: process.env.DISCORD_CLIENT_SECRET,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question (maybe blocking): How are we keeping these values secret?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're pulled from a .env file that isn't checked into source control. These variables will only be available on production and, because all this code happens on the server, they won't be exposed to any client. I do need to fail gracefully for local development, though.


const url = oauth.generateAuthUrl({
scope: ["identify"],
state: crypto.randomBytes(16).toString("hex"),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Praise: Thanks for using a real source of entropy!

@deammer
Copy link
Contributor Author

deammer commented May 19, 2022

@cubes to answer your question:

General thought/suggestion: Given that we're using a NoSQL store, do we have any documentation of the structure/schema of the objects we're storing in it? I think such a document could be useful given that the store itself is effectively schemaless.

The schema is documented in Coda and the types are an exact match for the shape of the database.

@cubes
Copy link

cubes commented May 19, 2022

The schema is documented in Coda and the types are an exact match for the shape of the database.

Thanks @deammer, you've thought of everything. 😄

@deammer deammer marked this pull request as ready for review May 19, 2022 23:15
@deammer deammer merged commit b353098 into main May 19, 2022
@deammer deammer deleted the discord-verification branch May 19, 2022 23:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants