Authentication Operations in Payload

Login using REST API

const res = await fetch('http://localhost:3000/api/[collection-slug]/login', {
2  method: 'POST',
3  headers: {
4    'Content-Type': 'application/json',
5  },
6  body: JSON.stringify({
7    email: 'dev@payloadcms.com',
8    password: 'this-is-not-our-password...or-is-it?',
9  }),
10})
11

12const json = await res.json()
13

14// JSON will be equal to the following:
15/*
16{
17  user: {
18    email: 'dev@payloadcms.com',
19    createdAt: "2020-12-27T21:16:45.645Z",
20    updatedAt: "2021-01-02T18:37:41.588Z",
21    id: "5ae8f9bde69e394e717c8832"
22  },
23  token: '34o4345324...',
24  exp: 1609619861
25}

By enabling Authentication on a config, the following modifications will automatically be made to your Collection:

  1. email as well as password salt & hash fields will be added to your Collection’s schema
  2. The Admin panel will feature a new set of corresponding UI to allow for changing password and editing email
  3. A new set of operations will be exposed via Payload’s REST, Local, and GraphQL APIs

Once enabled, each document that is created within the Collection can be thought of as a user – who can make use of commonly required authentication functions such as logging in / out, resetting their password, and more.

Leave a comment

Your email address will not be published. Required fields are marked *