Skip to content

How to get authenticated user in custom express routes? #675

Answered by jmikrut
wojciechkrol asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @wojciechkrol — this is totally possible!

Check this page:
https://payloadcms.com/docs/authentication/using-middleware

We've got an Express middleware that allows you to re-use Payload's built-in auth easily. Should be exactly what you need. Note that this will only expose req.user for you to do what you want, but you still have to make sure to still enforce your own access control there.

One other thing you can do is also pass your user from your own Express endpoint into the Payload Local API, so it's used seamlessly.

Bam!

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@wojciechkrol
Comment options

Answer selected by wojciechkrol
Comment options

You must be logged in to vote
1 reply
@wojciechkrol
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants