mirror of
https://github.com/r-freeman/portfolio.git
synced 2025-05-03 19:50:20 +00:00
Add gh profile url to comments
All checks were successful
Build And Publish / BuildAndPublish (push) Successful in 3m8s
All checks were successful
Build And Publish / BuildAndPublish (push) Successful in 3m8s
This commit is contained in:
parent
b6c7dbdd71
commit
d773342653
@ -4,7 +4,6 @@ import {auth, signIn} from '@/auth'
|
|||||||
import {createClient} from '@/lib/supabase/server'
|
import {createClient} from '@/lib/supabase/server'
|
||||||
import {z} from 'zod'
|
import {z} from 'zod'
|
||||||
import {sendNotification} from '@/lib/ntfy'
|
import {sendNotification} from '@/lib/ntfy'
|
||||||
import {extractUserId} from '@/lib/github'
|
|
||||||
|
|
||||||
export async function loginWithGitHub() {
|
export async function loginWithGitHub() {
|
||||||
await signIn('github')
|
await signIn('github')
|
||||||
@ -56,16 +55,21 @@ export async function addComment(prevState: { message: string }, formData: FormD
|
|||||||
try {
|
try {
|
||||||
const supabase = await createClient()
|
const supabase = await createClient()
|
||||||
const session = await auth()
|
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) {
|
if (!session?.user) {
|
||||||
return {message: authorisation_error}
|
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([
|
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()
|
supabase.from('articles').select('*').eq('slug', slug).single()
|
||||||
])
|
])
|
||||||
|
|
||||||
|
15
auth.ts
15
auth.ts
@ -11,6 +11,21 @@ export const {handlers, signIn, signOut, auth} = NextAuth({
|
|||||||
callbacks: {
|
callbacks: {
|
||||||
async redirect({url, baseUrl}) {
|
async redirect({url, baseUrl}) {
|
||||||
return url
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
@ -3,6 +3,7 @@
|
|||||||
import React, {useActionState, useEffect, useState} from 'react'
|
import React, {useActionState, useEffect, useState} from 'react'
|
||||||
import {useSession} from 'next-auth/react'
|
import {useSession} from 'next-auth/react'
|
||||||
import Image from 'next/image'
|
import Image from 'next/image'
|
||||||
|
import Link from 'next/link'
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
import {addComment, loginWithGitHub} from '@/app/actions/comments'
|
import {addComment, loginWithGitHub} from '@/app/actions/comments'
|
||||||
import {Button} from '@/components/ui/Button'
|
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')}/>
|
className={clsx('rounded-full', isReply ? 'size-8' : 'size-12')}/>
|
||||||
<div className="flex-auto">
|
<div className="flex-auto">
|
||||||
<div className="flex items-baseline gap-x-1">
|
<div className="flex items-baseline gap-x-1">
|
||||||
<p className="font-semibold text-sm text-zinc-800 dark:text-zinc-100">{comment.user.name}</p>
|
<p className="font-semibold text-sm text-zinc-800 dark:text-zinc-100">
|
||||||
|
<Link href={`https://github.com/${comment.user.username ?? ''}`}>{comment.user.name}</Link>
|
||||||
|
</p>
|
||||||
<p className="text-sm text-zinc-500 dark:text-zinc-400">
|
<p className="text-sm text-zinc-500 dark:text-zinc-400">
|
||||||
<time dateTime={comment.created_at}>
|
<time dateTime={comment.created_at}>
|
||||||
<span>· {`${getShortDurationFromNow(comment.created_at)}`}</span>
|
<span>· {`${getShortDurationFromNow(comment.created_at)}`}</span>
|
||||||
|
@ -13,7 +13,7 @@ export async function getComments(slug: string) {
|
|||||||
published,
|
published,
|
||||||
created_at,
|
created_at,
|
||||||
parent_id,
|
parent_id,
|
||||||
user:users!inner(id, name, image),
|
user:users!inner(id, name, image, username),
|
||||||
article:articles!inner(id, title, slug)
|
article:articles!inner(id, title, slug)
|
||||||
`)
|
`)
|
||||||
.eq('article.slug', slug)
|
.eq('article.slug', slug)
|
||||||
|
@ -47,7 +47,3 @@ export async function getPinnedRepos() {
|
|||||||
|
|
||||||
return response.data.user.pinnedItems.nodes
|
return response.data.user.pinnedItems.nodes
|
||||||
}
|
}
|
||||||
|
|
||||||
export function extractUserId(avatarUrl: string) {
|
|
||||||
return new URL(avatarUrl).pathname.split('/')[2]
|
|
||||||
}
|
|
@ -31,6 +31,7 @@ export type Comment = {
|
|||||||
parent_id: number | null
|
parent_id: number | null
|
||||||
user: {
|
user: {
|
||||||
id: number
|
id: number
|
||||||
|
username: string
|
||||||
name: string
|
name: string
|
||||||
image: string
|
image: string
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user