Skip to content

Commit

Permalink
feat: fetch user details when getting the session
Browse files Browse the repository at this point in the history
  • Loading branch information
GetPsyched committed Apr 20, 2024
1 parent 832c2c2 commit 836ceab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 7 additions & 3 deletions app/[locale]/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ export default async function Header({ locale }: { locale: string }) {
<ProfileImage
alt={text.profile.alt}
href={`/${locale}/profile`}
src={session.user.image}
// FIXME: Remove session.user.image once
// everyone's image is fed to the database
src={session.user.image ?? session.person.image}
/>
) : (
<Button asChild className="h-full w-16 xl:w-20">
Expand Down Expand Up @@ -186,7 +188,9 @@ export default async function Header({ locale }: { locale: string }) {
<Link href={`/${locale}/profile`}>
<ProfileImage
alt={text.profile.alt}
src={session.user.image}
// FIXME: Remove session.user.image once
// everyone's image is fed to the database
src={session.user.image ?? session.person.image}
/>
{text.profile.view}
</Link>
Expand Down Expand Up @@ -214,7 +218,7 @@ const ProfileImage = ({
alt: string;
className?: string;
href?: string;
src?: string | null;
src?: string;
}) => {
return (
<Button
Expand Down
10 changes: 9 additions & 1 deletion server/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { InferSelectModel } from 'drizzle-orm';
import NextAuth, { type DefaultSession, type User } from 'next-auth';
import Google from 'next-auth/providers/google';

import { db } from '~/server/db';
import { db, persons } from '~/server/db';

if (!process.env.AUTH_GOOGLE_ID || !process.env.AUTH_GOOGLE_SECRET) {
throw new Error(
Expand All @@ -11,6 +12,7 @@ if (!process.env.AUTH_GOOGLE_ID || !process.env.AUTH_GOOGLE_SECRET) {

declare module 'next-auth' {
interface Session extends DefaultSession {
person: InferSelectModel<typeof persons>;
user: User;
}
}
Expand All @@ -22,6 +24,12 @@ export const {
signOut,
} = NextAuth({
callbacks: {
async session({ session }) {
session.person = (await db.query.persons.findFirst({
where: ({ email }, { eq }) => eq(email, session.user.email),
}))!;
return session;
},
async signIn({ user: { email } }) {
if (!email) return false;

Expand Down

0 comments on commit 836ceab

Please sign in to comment.