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

Feature/127/save GitHub user #128

Merged
merged 6 commits into from
Feb 20, 2023
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
15 changes: 14 additions & 1 deletion app/auth.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ import { Auth0Strategy } from "remix-auth-auth0";
import { sessionStorage } from "~/session.server";
import type { User } from "~/models/user.server";
import { findOrCreate } from "~/models/user.server";
import { createProfile, getProfileByEmail } from "./models/profile.server";
import {
createProfile,
getProfileByEmail,
updateProfile,
} from "./models/profile.server";
import { findProfileData } from "./lake.server";
import { getUserInfo } from "./routes/api/github/get-getUserInfo";

invariant(process.env.AUTH0_CLIENT_ID, "AUTH0_CLIENT_ID must be set");
invariant(process.env.AUTH0_CLIENT_SECRET, "AUTH0_CLIENT_SECRET must be set");
Expand All @@ -30,6 +35,14 @@ let auth0Strategy = new Auth0Strategy(
// search profile in our DB or get from data lake
const email = profile.emails[0].value;
const userProfile = await getProfileByEmail(email);
if (userProfile?.githubUser === '' || userProfile?.githubUser === null) {
const { data } = await getUserInfo(email);
if (data.total_count > 0) {
const gitHubUser = data.items[0].login;
userProfile.githubUser = gitHubUser;
updateProfile(userProfile, userProfile.id);
}
}
if (!userProfile) {
const lakeProfile = await findProfileData(email);
createProfile({
Expand Down
7 changes: 7 additions & 0 deletions app/models/profile.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ export async function createProfile(data: Prisma.ProfilesCreateInput) {
return prisma.profiles.create({ data });
}

export async function updateProfile(
data: Prisma.ProfilesUpdateInput,
id: string
) {
return prisma.profiles.update({ where: { id }, data: data });
}

export async function consolidateProfilesByEmail(
data: Prisma.ProfilesCreateInput[],
db: PrismaClient
Expand Down
16 changes: 16 additions & 0 deletions app/routes/api/github/get-getUserInfo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable no-console */
import { Octokit } from "@octokit/core";
LuisTejedaS marked this conversation as resolved.
Show resolved Hide resolved
import { env } from "process";
const octokit = new Octokit({ USERNAME: env.AUTH0_CLIENT_SECRET });

export const getUserInfo = async (email: string) => {
try{
return await octokit.request(`GET /search/users?q=${email}`);
}
catch (e){
console.log('unable to get the guthub user');
return {
data: null
};
}
};
2 changes: 1 addition & 1 deletion app/routes/projects/components/ProjectForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function ProjectForm({
);
const [helpWanted, setHelpWanted] = useControlField<boolean>("helpWanted");
const isSubmitting = useIsSubmitting();
const { isValid, fieldErrors, getValues } = useFormContext();
const { isValid } = useFormContext();
const disabled = isSubmitting;
if (!isValid) {
// console.log(fieldErrors);
Expand Down
Loading