Skip to content

Commit

Permalink
Merge pull request #67 from hubcio2115/fix/google-refresh-token
Browse files Browse the repository at this point in the history
  • Loading branch information
hubcio2115 authored Sep 29, 2024
2 parents c719b68 + 6c5e0d4 commit dfc2ca9
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions apps/web/src/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createOrganization,
getOrganizationByName,
} from "./api/utils/organizations";
import { eq } from "drizzle-orm";

/**
* Module augmentation for `next-auth` types. Allows us to add custom properties to the `session`
Expand Down Expand Up @@ -45,9 +46,40 @@ export const { auth, handlers, signIn, signOut } = NextAuth({
id: user.id,
},
}),

authorized: async ({ auth }) => {
return !!auth?.user;
},

signIn: async ({ user, account }) => {
if (!account) {
return true;
}

const newTokens = {
access_token: account.access_token ?? "",
expires_at: account.expires_at ?? 0,
refresh_token: account.refresh_token ?? "",
id_token: account.id_token ?? "",
};

if (Object.values(newTokens).some((value) => !value)) {
console.error("one of newTokens value is undefined", newTokens);
return true;
}

await db
.update(accounts)
.set({
access_token: newTokens.access_token,
id_token: newTokens.id_token,
expires_at: newTokens.expires_at,
refresh_token: newTokens.refresh_token,
})
.where(eq(accounts.userId, user.id!));

return true;
},
},

adapter: DrizzleAdapter(db, {
Expand All @@ -69,6 +101,7 @@ export const { auth, handlers, signIn, signOut } = NextAuth({
"https://www.googleapis.com/auth/youtube.force-ssl",
].join(" "),
access_type: "offline",
prompt: "consent",
},
},
}),
Expand Down

0 comments on commit dfc2ca9

Please sign in to comment.