Authenticate a User
This guide will cover registering users, logging in, and making an authenticated request.
You can use a visual API testing tool like Postman or Hoppscotch, a terminal-based based tool like curl or HTTPie, or make HTTP requests directly in a script written in your programming language of choice.
Before You Start
You will need a Directus project.
Everything you need to start building. Provisioned in 90 seconds.Create a posts
collection with at least a title
and content
field. Follow the data modeling quickstart to learn more. Create a single item in the collection.
Creating a Role and a Policy
From your settings, navigate to User Roles and create a new role named "User". This role will later be applied to new users who register.
Within the role page, create a new policy named "Read Posts". Add a permission to the policy to allow Read action on posts
collection.
Allow User Registration
From your settings, enable User Registration. Select the User role that was just created and disable the Verify Email setting.
Registering via the Data Studio
Log out of the Data Studio. From the Sign In screen, you will see a new option to Sign Up. Once a user is signed up, they will immediately be able to log in.
Registering via API
Open your terminal and run the following command to register a new user.
curl \
--request POST \
--header 'Content-Type: application/json' \
--data '{ "email": "hello@example.com", "password": "d1r3ctu5" }' \
--url 'https://directus.example.com/register'
Go to the user directory in the module bar and you should see a new user has been created.
Logging In
curl \
--request POST \
--header 'Content-Type: application/json' \
--data '{ "email": "hello@example.com", "password": "d1r3ctu5" }' \
--url 'https://directus.example.com/auth/login'
Authenticating Requests
You can use the access token while making requests. If your token has expired, you must refresh it.
curl \
--header 'Authorization: Bearer YOUR_ACCESS_TOKEN' \
--url 'https://directus.example.com/items/posts'
Next Steps
Read more about access tokens, access control, and then refer to the Users API reference to manage user accounts.