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

Update profile picture for AAD and GH #489

Merged
merged 6 commits into from
Feb 19, 2025
Merged
Changes from 4 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
40 changes: 38 additions & 2 deletions src/features/auth-page/auth-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import CredentialsProvider from "next-auth/providers/credentials";
import GitHubProvider from "next-auth/providers/github";
import { Provider } from "next-auth/providers/index";
import { hashValue } from "./helpers";
import { image } from "@markdoc/markdoc/dist/src/schema";
import { access } from "fs";

const configureIdentityProvider = () => {
const providers: Array<Provider> = [];
Expand All @@ -18,10 +20,13 @@ const configureIdentityProvider = () => {
clientId: process.env.AUTH_GITHUB_ID!,
clientSecret: process.env.AUTH_GITHUB_SECRET!,
async profile(profile) {
const image = await fetchProfilePicture(profile.avatar_url, null);
const newProfile = {
...profile,
isAdmin: adminEmails?.includes(profile.email.toLowerCase()),
image: image,
};
console.log("GitHub profile:", newProfile);
return newProfile;
},
})
Expand All @@ -38,17 +43,24 @@ const configureIdentityProvider = () => {
clientId: process.env.AZURE_AD_CLIENT_ID!,
clientSecret: process.env.AZURE_AD_CLIENT_SECRET!,
tenantId: process.env.AZURE_AD_TENANT_ID!,
async profile(profile) {
authorization: {
params: {
scope: "openid profile User.Read",
},
},
async profile(profile, tokens) {
const email = profile.email || profile.preferred_username || "";
const image = await fetchProfilePicture(`https://graph.microsoft.com/v1.0/me/photos/48x48/$value`, tokens.access_token);
const newProfile = {
...profile,
email,
// throws error without this - unsure of the root cause (https://stackoverflow.com/questions/76244244/profile-id-is-missing-in-google-oauth-profile-response-nextauth)
id: profile.sub,
isAdmin:
adminEmails?.includes(profile.email?.toLowerCase()) ||
adminEmails?.includes(profile.preferred_username?.toLowerCase()),
image: image,
};
console.log("Azure AD profile:", newProfile);
return newProfile;
},
})
Expand Down Expand Up @@ -94,6 +106,30 @@ const configureIdentityProvider = () => {
return providers;
};

export const fetchProfilePicture = async (profilePictureUrl: string, accessToken: any): Promise<any> => {
console.log("Fetching profile picture...");
var image = null
const profilePicture = await fetch(
profilePictureUrl,
accessToken && {
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);
if (profilePicture.ok) {
console.log("Profile picture fetched successfully.");
const pictureBuffer = await profilePicture.arrayBuffer();
const pictureBase64 = Buffer.from(pictureBuffer).toString("base64");
image = `data:image/jpeg;base64,${pictureBase64}`;
}
else {
console.error("Failed to fetch profile picture:", profilePictureUrl, profilePicture.statusText);
}
return image;
};


export const options: NextAuthOptions = {
secret: process.env.NEXTAUTH_SECRET,
providers: [...configureIdentityProvider()],
Expand Down
Loading