From Payload CMS Documentation - A headless CMS is a system that sticks to what it's good at—managing content. It concentrates solely on granting administrators an effective way to author and maintain content but doesn't control how and where that content is used.
Another definition I found
A headless CMS is a content management system that separates the presentation layer (where content is presented) from the backend (where content is managed). A headless CMS allows you to manage content in one place and be able to deploy that content on any digital channel you choose.
Video Series
In this video series, we will set up Payload CMS Headless CMS, create a Customers collection, log in as a Customer, create an account as a Customer. Then build a simple vuejs website that can login using the API created by Payload CMS and finally look at the changes needed to go from website to mobile application with Ionic Framework.
Install payload with the following command in your terminal.
npxcreate-payload-app
the database…
At this time Payload CMS only supports MongoDB.
You can create a free account at Mongo DB Atlas for a cloud-hosted solution - See Website
In this example, I will be using the docker file, docker-compose.yml provided, and only launch the Mongo database service.
Project Configuration
After it is installed and you switch to the project directory you can configure the server. Open up the .env file and make the appropriate changes.
The installation process will add the basics, so all you will need to add is the PAYLOAD_PUBLIC_SERVER_URL, that value will be specific to your environment
you can open the server.ts file to make any additional configurations for the express server that is running Payload CMS.
I will ensure the server port is the same as in my .env file.
// server.tsimportexpressfrom'express';importpayloadfrom'payload';require('dotenv').config();constapp=express();// Redirect root to Admin panelapp.get('/',(_,res)=>{res.redirect('/admin');});conststart=async ()=>{// Initialize Payloadawaitpayload.init({secret:process.env.PAYLOAD_SECRET,mongoURL:process.env.MONGODB_URI,express:app,onInit:async ()=>{payload.logger.info("Payload Admin URL: "+payload.getAdminURL())},})// Add your own express routes hereapp.listen(3100);}start();
You can see I adjusted the port that the server is listening on to match what is in my .env file
app.listen(3100);
Creating the Customer Collection
Finally, let's create a new collection called Customers, this will be the collection of application users. Payload CMS creates a default collection Users, which in this example will be system administrators and people who need to login directly to the Payload CMS console
The code for the Customer Collection is listed below.
// /collections/Customers.tsimport{CollectionConfig}from"payload/types";constCustomers:CollectionConfig={slug:"customers",auth:true,admin:{useAsTitle:"email",},fields:[// Email added by default// Add more fields as needed{name:"full_name",type:"text",label:"Full Name",required:true,},{name:"birthday",type:"date",label:"Birthday",required:true,},],};exportdefaultCustomers;
Update configuration in payload.config.ts to include the new Collection Customers
You also get GraphQL support out of the box, see the playground here http://localhost:3100/api/graphql-playground. Be sure to use the URL that is appropriate for your configuration.
Ionic Framework UI Components are used to build a website and then a mobile application is built using Ionic Capacitor. Ionic UI components are not required but are used for UX. The vue js code presented here will work fine in a separate application.
Use Ionic Framework cli to create a new Vue js project using the blank template.
npxionicstart--typevue
Update the router in the new project to support the two new routes, /sign-in and /sign-up. Make the following changes to /router/index.ts
The login function is going to make a REST API call to the Payload CMS backend to authenticate the user.
We use the refs we created above to pass the email and password values to the API call and if successful it will return the user information and the JWT for for authenticating the user in future API calls
consthandleLogin=async ()=>{try{constresp=awaitfetch("http://localhost:3100/api/customers/login",{method:"POST",credentials:"include",headers:{"Content-Type":"application/json",},body:JSON.stringify({email:email.value,password:password.value,}),});if (!resp.ok){consterrorMsg=(awaitresp.json())?.errors[0].message;thrownewError(errorMsg);}constuser=awaitresp.json();console.log(user);// goto homerouter.replace("/home");}catch (error:any){alert("Sign In Error "+error.message);}};
Update server to address CORS issue; add the two lines below to the server configuration in payload.config.ts
Source code from the video series, Getting Started with PayloadCMS & Vue JS - Free, Open Source, Typescript, Extensible.
payload-vue-ionic-video
Source code from the video series, Getting Started with PayloadCMS & Vue JS - Free, Open Source, Typescript, Extensible. See Video Here
In this video series, we will set up PayloadCMS Headless CMS, create a Customers collection, log in as a Customer, create an account as a Customer. Then build a simple vuejs website that can login using the API created by PayloadCMS and finally look at the changes needed to go from website to mobile application with Ionic Framework.
PayloadCMS - The best way to build a modern backend + admin UI. No black magic, all TypeScript, and fully open-source, Payload is both an app framework and a headless CMS. @payloadcms
Ionic Framework - The mobile SDK for the Web
An open-source mobile UI toolkit for building modern, high-quality cross-platform mobile apps from a single code base in React.Vue.Angular. @IonicFramework