diff --git a/app/actions/comments.ts b/app/actions/comments.ts index f0b8792..a380292 100644 --- a/app/actions/comments.ts +++ b/app/actions/comments.ts @@ -4,7 +4,6 @@ import {auth, signIn} from '@/auth' import {createClient} from '@/lib/supabase/server' import {z} from 'zod' import {sendNotification} from '@/lib/ntfy' -import {extractUserId} from '@/lib/github' export async function loginWithGitHub() { await signIn('github') @@ -56,16 +55,21 @@ export async function addComment(prevState: { message: string }, formData: FormD try { const supabase = await createClient() const session = await auth() - const isMe = process.env.GITHUB_USER_ID === extractUserId(session?.user?.image ?? '') + const isMe = process.env.GITHUB_USER_ID === String(session?.user?.id) if (!session?.user) { return {message: authorisation_error} } - const {name, email, image} = session.user + const {name, email, image, login} = session.user as { [k: string]: string } const [{data: user}, {data: article}] = await Promise.all([ - supabase.from('users').upsert({name, email, image}, {onConflict: 'email'}).select('*').single(), + supabase.from('users').upsert({ + name, + email, + image, + username: login ?? null + }, {onConflict: 'email'}).select('*').single(), supabase.from('articles').select('*').eq('slug', slug).single() ]) diff --git a/auth.ts b/auth.ts index a2ce624..bed08b6 100644 --- a/auth.ts +++ b/auth.ts @@ -11,6 +11,21 @@ export const {handlers, signIn, signOut, auth} = NextAuth({ callbacks: { async redirect({url, baseUrl}) { return url + }, + async jwt({token, user, account, profile}) { + if (profile) { + token.profile = profile + } + return token + }, + async session({session, user, token}) { + if (token.profile) { + session.user = { + ...session.user, + ...token.profile + } + } + return session } } }) \ No newline at end of file diff --git a/components/ui/Comments.tsx b/components/ui/Comments.tsx index 7651bde..8c9765d 100644 --- a/components/ui/Comments.tsx +++ b/components/ui/Comments.tsx @@ -3,6 +3,7 @@ import React, {useActionState, useEffect, useState} from 'react' import {useSession} from 'next-auth/react' import Image from 'next/image' +import Link from 'next/link' import clsx from 'clsx' import {addComment, loginWithGitHub} from '@/app/actions/comments' import {Button} from '@/components/ui/Button' @@ -57,7 +58,9 @@ Comments.Comment = function Comment({comment, isReply = false, className}: { className={clsx('rounded-full', isReply ? 'size-8' : 'size-12')}/>
-

{comment.user.name}

+

+ {comment.user.name} +