Unverified Commit c1b76bd3 authored by Adem ilter's avatar Adem ilter Committed by GitHub

example fix url (#42695)

Co-authored-by: 's avatarSteven Tey <stevensteel97@gmail.com>
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent bb200b92
...@@ -34,8 +34,11 @@ export default function CommentForm({ ...@@ -34,8 +34,11 @@ export default function CommentForm({
<button className="py-2 px-4 rounded bg-blue-600 text-white disabled:opacity-40 hover:bg-blue-700"> <button className="py-2 px-4 rounded bg-blue-600 text-white disabled:opacity-40 hover:bg-blue-700">
Send Send
</button> </button>
<button className="text-gray-500" onClick={() => logout()}> <button
Log out className="text-gray-500"
onClick={() => logout({ returnTo: window.location.origin })}
>
Log Out
</button> </button>
</div> </div>
) : ( ) : (
......
import type { Comment } from '../interfaces' import type { Comment } from '../interfaces'
import React, { useState, useEffect } from 'react' import React, { useState } from 'react'
import useSWR from 'swr' import useSWR from 'swr'
import { useAuth0 } from '@auth0/auth0-react' import { useAuth0 } from '@auth0/auth0-react'
async function fetcher(url: string) { const fetcher = (url) => fetch(url).then((res) => res.json())
const query = new URLSearchParams({ url })
const queryUrl = `${url}?${query.toString()}`
return fetch(queryUrl).then((res) => res.json())
}
export default function useComments() { export default function useComments() {
const { getAccessTokenSilently } = useAuth0() const { getAccessTokenSilently } = useAuth0()
const [text, setText] = useState('') const [text, setText] = useState('')
const [url, setUrl] = useState<string | null>(null)
const { data: comments, mutate } = useSWR<Comment[]>( const { data: comments, mutate } = useSWR<Comment[]>(
'/api/comment', '/api/comment',
...@@ -21,11 +15,6 @@ export default function useComments() { ...@@ -21,11 +15,6 @@ export default function useComments() {
{ fallbackData: [] } { fallbackData: [] }
) )
useEffect(() => {
const url = window.location.origin + window.location.pathname
setUrl(url)
}, [])
const onSubmit = async (e: React.FormEvent) => { const onSubmit = async (e: React.FormEvent) => {
e.preventDefault() e.preventDefault()
const token = await getAccessTokenSilently() const token = await getAccessTokenSilently()
...@@ -33,7 +22,7 @@ export default function useComments() { ...@@ -33,7 +22,7 @@ export default function useComments() {
try { try {
await fetch('/api/comment', { await fetch('/api/comment', {
method: 'POST', method: 'POST',
body: JSON.stringify({ url, text }), body: JSON.stringify({ text }),
headers: { headers: {
Authorization: token, Authorization: token,
'Content-Type': 'application/json', 'Content-Type': 'application/json',
...@@ -52,7 +41,7 @@ export default function useComments() { ...@@ -52,7 +41,7 @@ export default function useComments() {
try { try {
await fetch('/api/comment', { await fetch('/api/comment', {
method: 'DELETE', method: 'DELETE',
body: JSON.stringify({ url, comment }), body: JSON.stringify({ comment }),
headers: { headers: {
Authorization: token, Authorization: token,
'Content-Type': 'application/json', 'Content-Type': 'application/json',
......
const clearUrl = (url: string) => {
const { origin, pathname } = new URL(url)
return `${origin}${pathname}`
}
export default clearUrl
...@@ -3,15 +3,17 @@ import type { Comment } from '../interfaces' ...@@ -3,15 +3,17 @@ import type { Comment } from '../interfaces'
import redis from './redis' import redis from './redis'
import { nanoid } from 'nanoid' import { nanoid } from 'nanoid'
import getUser from './getUser' import getUser from './getUser'
import clearUrl from './clearUrl'
export default async function createComments( export default async function createComments(
req: NextApiRequest, req: NextApiRequest,
res: NextApiResponse res: NextApiResponse
) { ) {
const { url, text } = req.body const url = clearUrl(req.headers.referer)
const { text } = req.body
const { authorization } = req.headers const { authorization } = req.headers
if (!url || !text || !authorization) { if (!text || !authorization) {
return res.status(400).json({ message: 'Missing parameter.' }) return res.status(400).json({ message: 'Missing parameter.' })
} }
......
...@@ -2,15 +2,17 @@ import type { NextApiRequest, NextApiResponse } from 'next' ...@@ -2,15 +2,17 @@ import type { NextApiRequest, NextApiResponse } from 'next'
import type { User, Comment } from '../interfaces' import type { User, Comment } from '../interfaces'
import redis from './redis' import redis from './redis'
import getUser from './getUser' import getUser from './getUser'
import clearUrl from './clearUrl'
export default async function deleteComments( export default async function deleteComments(
req: NextApiRequest, req: NextApiRequest,
res: NextApiResponse res: NextApiResponse
) { ) {
const { url, comment }: { url: string; comment: Comment } = req.body const url = clearUrl(req.headers.referer)
const { comment }: { url: string; comment: Comment } = req.body
const { authorization } = req.headers const { authorization } = req.headers
if (!url || !comment || !authorization) { if (!comment || !authorization) {
return res.status(400).json({ message: 'Missing parameter.' }) return res.status(400).json({ message: 'Missing parameter.' })
} }
......
import type { NextApiRequest, NextApiResponse } from 'next' import type { NextApiRequest, NextApiResponse } from 'next'
import type { Comment } from '../interfaces' import type { Comment } from '../interfaces'
import redis from './redis' import redis from './redis'
import clearUrl from './clearUrl'
export default async function fetchComment( export default async function fetchComment(
req: NextApiRequest, req: NextApiRequest,
res: NextApiResponse res: NextApiResponse
) { ) {
const { url }: { url?: string } = req.query const url = clearUrl(req.headers.referer)
if (!url) {
return res.status(400).json({ message: 'Missing parameter.' })
}
if (!redis) { if (!redis) {
return res.status(500).json({ message: 'Failed to connect to redis.' }) return res.status(500).json({ message: 'Failed to connect to redis.' })
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
"@tailwindcss/typography": "^0.5.7", "@tailwindcss/typography": "^0.5.7",
"date-fns": "^2.29.3", "date-fns": "^2.29.3",
"gray-matter": "4.0.3", "gray-matter": "4.0.3",
"ioredis": "^5.2.3", "ioredis": "^5.2.4",
"nanoid": "^4.0.0", "nanoid": "^4.0.0",
"next": "latest", "next": "latest",
"react": "^18.2.0", "react": "^18.2.0",
......
...@@ -3,7 +3,11 @@ import { Html, Head, Main, NextScript } from 'next/document' ...@@ -3,7 +3,11 @@ import { Html, Head, Main, NextScript } from 'next/document'
export default function Document() { export default function Document() {
return ( return (
<Html lang="en"> <Html lang="en">
<Head /> <Head>
<meta charSet="utf-8" />
<meta name="robots" content="follow, index" />
<meta httpEquiv="x-ua-compatible" content="ie=edge" />
</Head>
<body className="bg-white text-gray-700 antialiased"> <body className="bg-white text-gray-700 antialiased">
<Main /> <Main />
<NextScript /> <NextScript />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment