Files
supabase/apps/cms/src/utilities/getMeUser.ts
Francesco SansalvadoreandIvan Vasilov f7d90bbc30 feat: new cms app (#35999)
* add cms turbo app

* add cms scripts to package.json

* update README

* update README

* update files

* fix formatting

* fix populateAuthors

* sync dependencies

* update launch week field

* update lint config

* Coordinate the deps to be the same as the other apps.

* Remove extra files.

* Run prettier on the CMS files.

* Add commands for cleaning and typechecking.

* Fix weird version of @types/react in docs.

* run pnpm install

* fix pnpm-lock

* fix cms lint

---------

Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2025-06-03 14:26:43 +03:00

44 lines
988 B
TypeScript

import { cookies } from 'next/headers'
import { redirect } from 'next/navigation'
import type { User } from '../payload-types'
import { getClientSideURL } from './getURL'
export const getMeUser = async (args?: {
nullUserRedirect?: string
validUserRedirect?: string
}): Promise<{
token: string
user: User
}> => {
const { nullUserRedirect, validUserRedirect } = args || {}
const cookieStore = await cookies()
const token = cookieStore.get('payload-token')?.value
const meUserReq = await fetch(`${getClientSideURL()}/api/users/me`, {
headers: {
Authorization: `JWT ${token}`,
},
})
const {
user,
}: {
user: User
} = await meUserReq.json()
if (validUserRedirect && meUserReq.ok && user) {
redirect(validUserRedirect)
}
if (nullUserRedirect && (!meUserReq.ok || !user)) {
redirect(nullUserRedirect)
}
// Token will exist here because if it doesn't the user will be redirected
return {
token: token!,
user,
}
}