Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

How to get the current logged in user in nextjs

default discord avatar
awfaaraxlast year
2

How do i get the current logged in user in nextjs, I know there

payload.login

, is there something like

payload.currentUser
  • default discord avatar
    zed0547last year

    Hello!



    For REST API see the

    /me

    endpoint:

    https://payloadcms.com/docs/rest-api/overview#auth-operations

    For Local API you can use

    payload.auth({ headers })

    :

    https://payloadcms.com/docs/local-api/overview#auth
  • default discord avatar
    awfaaraxlast year

    Hi

    @654031862146007055

    , thank you so much, i come up with this and now it works:



    import config from '@payload-config';
    import { headers as getHeaders } from 'next/headers';
    import { getPayload } from 'payload';
    
    export const getCurrentUser = async () => {
        const payload = await getPayload({ config });
    
        const headers = await getHeaders();
    
        const { user } = await payload.auth({ headers });
    
        return user;
    };
Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.