Skip to content

Commit

Permalink
refactor(cookies, params): update for Next.js 15 release after canary…
Browse files Browse the repository at this point in the history
… changes
  • Loading branch information
DiegoPorfirio01 committed Dec 31, 2024
1 parent 20de569 commit 9f49a9b
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 49 deletions.
11 changes: 9 additions & 2 deletions apps/api/src/http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import { getChatGroups } from './routes/chat/get-chat-groups'
import { getChatGroupsUser } from './routes/chat/get-chat-groups-user'
import { getChats } from './routes/chat/get-chats'
import { updateChatGroup } from './routes/chat/update-chat-group'
import { getPing } from './routes/cron/ping'

// Extend the FastifyInstance type to include the 'io' property
interface CustomFastifyInstance extends FastifyInstance {
Expand All @@ -50,7 +49,15 @@ app.setValidatorCompiler(validatorCompiler)

// Register CORS
app.register(fastifyCors, {
origin: [env.NEXT_PUBLIC_APP_URL, 'https://admin.socket.io'],
origin: [
env.NEXT_PUBLIC_APP_URL,
'https://admin.socket.io',
'http://localhost:3000',
'https://chat-lucy.vercel.app',
],
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true,
})

// Register Socket.io
Expand Down
6 changes: 3 additions & 3 deletions apps/web/src/app/[locale]/(marketing)/(home)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import UserReviews from '@/components/marketing/users-review'
import { ScrollToTop } from '@/components/scroll-to-up'

export async function generateMetadata({
params: { locale },
params,
}: {
params: { locale: string }
params: Promise<{ locale: string }>
}) {
const { locale } = await params
const messages = (await getMessages({ locale })) as Messages

const title = messages.marketing.menu.home || 'Home'

return {
Expand Down
5 changes: 3 additions & 2 deletions apps/web/src/app/[locale]/(marketing)/about/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { About } from '@/components/marketing/about'
import { Sponsors } from '@/components/marketing/sponsors'

export async function generateMetadata({
params: { locale },
params,
}: {
params: { locale: string }
params: Promise<{ locale: string }>
}) {
const { locale } = await params
const messages = (await getMessages({ locale })) as Messages

const title = messages.marketing.menu.about || 'About'
Expand Down
5 changes: 3 additions & 2 deletions apps/web/src/app/[locale]/(marketing)/prices/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import { Cta } from '@/components/marketing/cts'
import PricingTable from '@/components/marketing/pricing-table'

export async function generateMetadata({
params: { locale },
params,
}: {
params: { locale: string }
params: Promise<{ locale: string }>
}) {
const { locale } = await params
const messages = (await getMessages({ locale })) as Messages

const title = messages.marketing.menu.prices || 'Prices'
Expand Down
5 changes: 3 additions & 2 deletions apps/web/src/app/[locale]/(marketing)/services/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import type { Messages } from '@/@types'
import { Services } from '@/components/marketing/services'

export async function generateMetadata({
params: { locale },
params,
}: {
params: { locale: string }
params: Promise<{ locale: string }>
}) {
const { locale } = await params
const messages = (await getMessages({ locale })) as Messages

const title = messages.marketing.menu.services || 'Services'
Expand Down
5 changes: 3 additions & 2 deletions apps/web/src/app/[locale]/(marketing)/testimonials/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import type { Messages } from '@/@types'
import { UsersReviewCards } from '@/components/marketing/users-review-cards'

export async function generateMetadata({
params: { locale },
params,
}: {
params: { locale: string }
params: Promise<{ locale: string }>
}) {
const { locale } = await params
const messages = (await getMessages({ locale })) as Messages

const title = messages.marketing.menu.testimonials || 'Testimonials'
Expand Down
9 changes: 5 additions & 4 deletions apps/web/src/app/[locale]/auth/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { ArrowLeftSquare } from 'lucide-react'
import Link from 'next/link'
import { redirect } from 'next/navigation'
import { useLocale } from 'next-intl'

import { isAuthenticated } from '@/auth/auth'

export default function AuthLayout({
export default async function AuthLayout({
children,
params,
}: Readonly<{
children: React.ReactNode
params: Promise<{ locale: string }>
}>) {
const locale = useLocale()
const { locale } = await params

if (isAuthenticated()) {
if (await isAuthenticated()) {
return redirect(`/${locale}/dashboard`)
}

Expand Down
3 changes: 2 additions & 1 deletion apps/web/src/app/[locale]/auth/sign-in/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export async function signInWithEmailAndPassoword(data: FormData) {
password,
})

cookies().set('token', token, {
const storedCookies = await cookies()
storedCookies.set('token', token, {
path: '/',
maxAge: 60 * 70 * 24 * 7, // 7days
})
Expand Down
5 changes: 3 additions & 2 deletions apps/web/src/app/[locale]/auth/sign-in/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import type { Messages } from '@/@types'
import { SignInForm } from './sign-in-form'

export async function generateMetadata({
params: { locale },
params,
}: {
params: { locale: string }
params: Promise<{ locale: string }>
}) {
const { locale } = await params
const messages = (await getMessages({ locale })) as Messages

const title = messages.marketing.menu.login || 'Login'
Expand Down
4 changes: 3 additions & 1 deletion apps/web/src/app/[locale]/auth/sign-up/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ export async function signUpAction(data: FormData) {

const { name, email, password } = result.data

const storedCookies = await cookies()

try {
const { token } = await signUp({
email,
password,
name,
})

cookies().set('token', token, {
await storedCookies.set('token', token, {
path: '/',
maxAge: 60 * 70 * 24 * 7, // 7days
})
Expand Down
5 changes: 3 additions & 2 deletions apps/web/src/app/[locale]/auth/sign-up/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import type { Messages } from '@/@types'
import { SignUpForm } from './sign-up-form'

export async function generateMetadata({
params: { locale },
params,
}: {
params: { locale: string }
params: Promise<{ locale: string }>
}) {
const { locale } = await params
const messages = (await getMessages({ locale })) as Messages

const title = messages.marketing.menu.signUp || 'Sign Up'
Expand Down
9 changes: 5 additions & 4 deletions apps/web/src/app/[locale]/dashboard/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Metadata } from 'next'
import { redirect } from 'next/navigation'
import { useLocale } from 'next-intl'

import { isAuthenticated } from '@/auth/auth'
import Header from '@/components/dashboard/header'
Expand All @@ -14,16 +13,18 @@ export const metadata: Metadata = {
},
}

export default function DashboardLayout({
export default async function DashboardLayout({
children,
sheet,
params,
}: Readonly<{
children: React.ReactNode
sheet: React.ReactNode
params: Promise<{ locale: string }>
}>) {
const locale = useLocale()
const { locale } = await params

if (!isAuthenticated()) {
if (!(await isAuthenticated())) {
return redirect(`/${locale}`)
}

Expand Down
11 changes: 3 additions & 8 deletions apps/web/src/app/[locale]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { cn } from '@/lib/utils'

import { Providers } from '../providers'

// Definição do metadata
export const metadata: Metadata = {
title: {
default: siteConfig.name,
Expand All @@ -33,16 +32,12 @@ const sansFont = Plus_Jakarta_Sans({

type Props = {
children: ReactNode
params: { locale: string }
params: Promise<{ locale: string }>
sheet: ReactNode
}

// Definição do componente LocaleLayout
export default async function LocaleLayout({
children,
params: { locale },
sheet,
}: Props) {
export default async function LocaleLayout({ children, params, sheet }: Props) {
const { locale } = await params
const messages = await getMessages()

return (
Expand Down
8 changes: 7 additions & 1 deletion apps/web/src/app/api/auth/sign-out/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { cookies } from 'next/headers'
import { NextRequest, NextResponse } from 'next/server'

export async function GET(request: NextRequest) {
cookies().delete('token')
const cookieStore = await cookies()
cookieStore.delete('token')

cookieStore.set('token', '', {
path: '/',
maxAge: 0,
})

const redirectURL = request.nextUrl.clone()
redirectURL.pathname = '/'
Expand Down
13 changes: 7 additions & 6 deletions apps/web/src/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@ import { redirect } from 'next/navigation'

import { getProfile } from '@/http/get-profile'

export function isAuthenticated() {
return !!cookies().get('token')?.value
export async function isAuthenticated() {
const cookieStore = await cookies()
return !!cookieStore.get('token')?.value
}

export async function getCurrentToken() {
try {
const token = cookies().get('token')?.value

const cookieStore = await cookies()
const token = cookieStore.get('token')?.value
return token
} catch (error) {}
}

export async function auth({ locale }: { locale: string }) {
const token = cookies().get('token')?.value
const cookieStore = await cookies()
const token = cookieStore.get('token')?.value

if (!token) {
redirect(`${locale}/auth/sign-in`)
}

try {
const { user } = await getProfile()

return user
} catch (error) {}

Expand Down
11 changes: 6 additions & 5 deletions apps/web/src/http/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ export const api = ky.create({
hooks: {
beforeRequest: [
async (request) => {
let cookieStore: CookiesFn | undefined
let token: string | undefined

if (typeof window === 'undefined') {
const { cookies: serverCookies } = await import('next/headers')

cookieStore = serverCookies
const { cookies } = await import('next/headers')
const cookieStore = await cookies()
token = cookieStore.get('token')?.value
} else {
token = getCookie('token')
}
const token = getCookie('token', { cookies: cookieStore })

if (token) {
request.headers.set('Authorization', `Bearer ${token}`)
Expand Down
11 changes: 9 additions & 2 deletions apps/web/src/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { headers } from 'next/headers'
import { notFound } from 'next/navigation'
import { getRequestConfig } from 'next-intl/server'

Expand All @@ -7,10 +8,16 @@ import type { Locale } from './@types'

const locales = i18nConfig.locales

export default getRequestConfig(async ({ locale }) => {
if (!locales.includes(locale as Locale)) notFound()
export default getRequestConfig(async () => {
const headersList = await headers()
const locale = headersList.get('X-NEXT-INTL-LOCALE')

if (!locale || !locales.includes(locale as Locale)) {
notFound()
}

return {
locale,
messages: (await import(`../messages/${locale}.json`)).default,
}
})

0 comments on commit 9f49a9b

Please sign in to comment.