Headless WordPress: Implementing Login To Your Application Using JWT

From Beyondcoin Wiki
Jump to navigation Jump to search

Headless WordPress is an idea that is being used more and more actively among developers. I will try to disclose several topics related to this approach. In particular, I will show how to implement application login using JWT. We will create a simple application that will allow users to log in using their username. After logging in, we will display the user his data.wpengine.com To implement login to the application using JWT, we need to install and activate the JWT Authentication for WP REST API plugin . Application Preparation Let's create our own React app using the Create React App . We open the terminal (command line) and go to the directory in which the application will be developed. Then use the command: 01 npx create-react-app my-app Here you can replace my-app with your name. This will be your directory in which all the code will be stored. I decided to name the headless directory. Then browse to this folder. You can start the application now using the npm start command. However, let's first install Gutenberg Components, the npm package @ wordpress / components. This will allow us to use various components in our application. Install Gutenberg Components using the command 01 npm install @wordpress/components Since the Gutenberg Components package does not include markup classes, we can create our own CSS style guide or use the existing style frameworks so as not to waste our time on this. If you want to create an application that will be used in production - be sure to think about creating your own CSS markup. We will use Bootstrap 4 for styles. You can install it using the command: 01 npm install bootstrap We also need SASS processing (SCSS), so we need our application to be able to process such files. To do this, use the following command: 01 npm install node-sass --save Since we will use the Bootstrap and Gutenberg styles, we can remove App.css and create App.scss. Put the following inside this new file: 01 @import '~bootstrap/scss/bootstrap.scss'; 02 03 @import '~@wordpress/components/build-style/style.css'; Now it will be compiled into a CSS file that will include both Boostrap and Gutenberg styles. We will need to fulfill POST and GET requests to our WordPress site, so we need a way to implement this. You can use fetch from the browser API, or a library. We will use the axios library. It is installed using the command 01 npm install axios Add login logic to our Headless WordPress Open App.js to start adding login logic. We will check whether the user is logged in or not. If not, we will show the login form. Otherwise, the console will be displayed. Let's adjust all the imports in the application: 01 import React, useState, useEffect from 'react'; 02 import './App.scss'; 03 import '@wordpress/components/build-style/style.css'; 04 import Login from './components/Login'; 05 import Dashboard from './components/Dashboard'; Do not worry about errors that appear. The Dashboard and Login components do not exist yet, therefore you may receive compilation errors. Now we can replace the App function with the following: 01 function App() 02 const [login, setLogin] = useState( ); 03 const siteURL = 'yoursite_here'; 04 05 useEffect(() => 06 const Merchandiser Ovent v. Free Download v.1.9.16. Best blogging app ever 22 July 20 Download 1 Licence Key localLogin = WooCommerce Wholesale Pricing Pro Suite Plugin V. Nulled localStorage.