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

feat: 👤 Implement gravatar #1268

Merged
merged 3 commits into from
Sep 27, 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
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import Image from "next/image";
import Link from "next/link";
import { MdPerson } from "react-icons/md";

import { useSupabase } from "@/lib/context/SupabaseProvider";

import { useGravatar } from "../hooks/useGravatar";
import { sidebarLinkStyle } from "../styles/SidebarLinkStyle";

export const UserButton = (): JSX.Element => {
const { session } = useSupabase();
const { gravatarUrl } = useGravatar();

return (
<Link aria-label="account" className={sidebarLinkStyle} href={"/user"}>
<MdPerson className="text-4xl" />
<div className="relative w-8 h-8">
<Image
Copy link
Contributor

Choose a reason for hiding this comment

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

So what happens when it doesnt exist on gravatar ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It displays a default image representing an anonymous user :
image

alt="gravatar"
layout="fill"
src={gravatarUrl}
className="rounded-xl"
/>
</div>
<span className="text-ellipsis overflow-hidden">
{session?.user.email ?? ""}
</span>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { createHash } from "crypto";
import { useEffect, useState } from "react";

import { useSupabase } from "@/lib/context/SupabaseProvider";

// Gravatar images may be requested just like a normal image, using an IMG tag. To get an image specific to a user, you must first calculate their email hash.
// The most basic image request URL looks like this:
// https://www.gravatar.com/avatar/HASH

const computeGravatarUrl = (email?: string) => {
const hash = createHash("md5")
.update(typeof email === "string" ? email.trim().toLowerCase() : "")
.digest("hex");

return `https://www.gravatar.com/avatar/${hash}?d=mp`;
};

export const useGravatar = (): { gravatarUrl: string } => {
const { session } = useSupabase();
const [gravatarUrl, setGravatarUrl] = useState<string>(computeGravatarUrl());

const email = session?.user.email;

useEffect(() => {
const computedUrl = computeGravatarUrl(email);
setGravatarUrl(computedUrl);
}, [email]);

return { gravatarUrl };
};
6 changes: 5 additions & 1 deletion frontend/next.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const nextConfig = {
images: {
domains: ["www.quivr.app", "quivr-cms.s3.eu-west-3.amazonaws.com"]
domains: [
"www.quivr.app",
"quivr-cms.s3.eu-west-3.amazonaws.com",
"www.gravatar.com",
Copy link
Contributor

Choose a reason for hiding this comment

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

You would also need to add the gravatar url to the ContentSecurityPolicy in frontend/next.config.js so it won't be blocked in preview and prod

],
},
// eslint-disable-next-line prefer-arrow/prefer-arrow-functions
async headers() {
Expand Down
Loading