Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

lazily backup user avatar to R2

Steven Tey7900118584d1
diff --git a/apps/web/lib/auth/options.ts b/apps/web/lib/auth/options.ts
index 76c0d32da0..e74f0b7bd3 100644
--- a/apps/web/lib/auth/options.ts
+++ b/apps/web/lib/auth/options.ts
@@ -11,6 +11,7 @@ import EmailProvider from "next-auth/providers/email";
import GithubProvider from "next-auth/providers/github";
import GoogleProvider from "next-auth/providers/google";
import { subscribe } from "../flodesk";
+import { isStored, storage } from "../storage";
const VERCEL_DEPLOYMENT = !!process.env.VERCEL_URL;
@@ -186,40 +187,38 @@ export const authOptions: NextAuthOptions = {
if (!user.email || (await isBlacklistedEmail(user.email))) {
return false;
}
- if (account?.provider === "google") {
+ if (account?.provider === "google" || account?.provider === "github") {
const userExists = await prisma.user.findUnique({
where: { email: user.email },
select: { name: true, image: true },
});
- // if the user already exists via email,
- // update the user with their name and image from Google
- if (userExists && profile) {
- await prisma.user.update({
- where: { email: user.email },
- data: {
- ...(userExists.name ? {} : { name: profile.name }),
- // @ts-expect-error - this is a bug in the types, `picture` is a valid on the `Profile` type
- ...(userExists.image ? {} : { image: profile.picture }),
- },
- });
+ if (!userExists || !profile) {
+ return true;
}
- } else if (account?.provider === "github") {
- const userExists = await prisma.user.findUnique({
- where: { email: user.email },
- select: { name: true, image: true },
- });
// if the user already exists via email,
- // update the user with their name and image from Github
+ // update the user with their name and image
if (userExists && profile) {
+ const profilePic =
+ profile[account.provider === "google" ? "picture" : "avatar_url"];
+ let newAvatar: string | null = null;
+ // if the existing user doesn't have an image or the image is not stored in R2
+ if (
+ (!userExists.image || !isStored(userExists.image)) &&
+ profilePic
+ ) {
+ const { url } = await storage.upload(
+ `avatars/${user.id}`,
+ profilePic,
+ );
+ console.log({ url });
+ newAvatar = url;
+ }
await prisma.user.update({
where: { email: user.email },
data: {
- ...(userExists.name
- ? {}
- : // @ts-expect-error - this is a bug in the types, `login` is a valid on the `Profile` type
- { name: profile.name || profile.login }),
- // @ts-expect-error - this is a bug in the types, `avatar_url` is a valid on the `Profile` type
- ...(userExists.image ? {} : { image: profile.avatar_url }),
+ // @ts-expect-error - this is a bug in the types, `login` is a valid on the `Profile` type
+ ...(!userExists.name && { name: profile.name || profile.login }),
+ ...(newAvatar && { image: newAvatar }),
},
});
}
@@ -277,7 +276,7 @@ export const authOptions: NextAuthOptions = {
}
return true;
},
- jwt: async ({ token, account, user, trigger }) => {
+ jwt: async ({ token, user, trigger }) => {
// force log out banned users
if (!token.email || (await isBlacklistedEmail(token.email))) {
return {};
@@ -342,6 +341,22 @@ export const authOptions: NextAuthOptions = {
]);
}
}
+ // lazily backup user avatar to R2
+ const currentImage = message.user.image;
+ if (currentImage && !isStored(currentImage)) {
+ const { url } = await storage.upload(
+ `avatars/${message.user.id}`,
+ currentImage,
+ );
+ await prisma.user.update({
+ where: {
+ id: message.user.id,
+ },
+ data: {
+ image: url,
+ },
+ });
+ }
},
},
};
diff --git a/apps/web/lib/storage.ts b/apps/web/lib/storage.ts
index ca6f95d13f..90c155c35e 100644
--- a/apps/web/lib/storage.ts
+++ b/apps/web/lib/storage.ts
@@ -101,7 +101,6 @@ class StorageClient {
if (opts.width) proxyUrl.searchParams.set("w", opts.width.toString());
if (opts.height) proxyUrl.searchParams.set("h", opts.height.toString());
proxyUrl.searchParams.set("fit", "cover");
- console.log(proxyUrl.toString());
response = await fetchWithTimeout(proxyUrl.toString());
} catch (error) {
response = await fetch(url);