Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a User Registration Login and Logout : Feature/register and login #13

Merged
merged 33 commits into from Nov 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
038ff3a
Add login page and mutation for login
imranhsayed Sep 27, 2019
e87b981
Add keyname for storing authtoken in localstorage when user is verified
imranhsayed Sep 28, 2019
1d6735f
Add MyAccount page
imranhsayed Sep 28, 2019
0cb344f
Add ErrorMessage component to display errors
imranhsayed Sep 28, 2019
c46e3ea
Rename ErrorMessage component to to MessageAlert Component for handli…
imranhsayed Sep 28, 2019
288cf53
Add a message that says Processing when the request goes
imranhsayed Sep 28, 2019
e214150
Redirect the user to my account page once he is logged in ( validated )
imranhsayed Sep 28, 2019
04d3a5a
Add information about how to configure the wp-graphql-jwt-authenticat…
imranhsayed Sep 28, 2019
46c5ad2
Update isEmpty function to be accessible in es6 files
imranhsayed Sep 28, 2019
5573790
ogic to redirect the user back to my account page.. if he tries to ac…
imranhsayed Sep 28, 2019
63721e1
Add styles for nprogress
imranhsayed Sep 28, 2019
e9180da
Add submenu
imranhsayed Sep 28, 2019
9382947
Add styles to navigation
imranhsayed Sep 28, 2019
53274ea
Add logout feature
imranhsayed Sep 28, 2019
cdb0e39
Create register user page and add mutation query to register a user
imranhsayed Sep 28, 2019
5c90015
Add dompurify package
imranhsayed Sep 28, 2019
d07bb8a
Add error handling for register
imranhsayed Sep 28, 2019
b8b00ac
Add logic to redirect the user to login page after he is registered
imranhsayed Sep 28, 2019
f813c5d
Add sucess message on successful registration of the user
imranhsayed Sep 28, 2019
d3cd4a7
Update success message
imranhsayed Sep 28, 2019
5d73d55
Update login mutation query to get user nicename
imranhsayed Sep 28, 2019
123890f
Update login mutation query to get user email
imranhsayed Sep 28, 2019
d93b423
If user tries to access my-account page when not logged in it will re…
imranhsayed Sep 28, 2019
8177ea5
Handle client and Server side validation
imranhsayed Oct 16, 2019
55d004f
Update doc comment for login component
imranhsayed Oct 16, 2019
ff6c8fd
Add register validation and sanitization function
imranhsayed Oct 16, 2019
9c53c6a
Add client side and server side validation functions for register
imranhsayed Oct 16, 2019
192aebc
Add doc comments for register form
imranhsayed Oct 16, 2019
f7b5d80
Update doc comments
imranhsayed Oct 16, 2019
fd5b3a7
Update doct comments for login form
imranhsayed Oct 16, 2019
330712b
Update comments on My Account page
imranhsayed Oct 16, 2019
251fc08
Add css to the form
imranhsayed Oct 16, 2019
f9d1a6a
Remove empty space
imranhsayed Oct 16, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 27 additions & 4 deletions README.md
Expand Up @@ -17,23 +17,46 @@ These instructions will get you a copy of the project up and running on your loc
2. `cd wp-decoupled`
3. `npm install`

## Add GraphQl support on WordPress
## Configure Backend( WordPress site ) :wrench:

1. Clone and activate the following plugins , in your WordPress plugin directory:
### 1. Add GraphQl support on WordPress

1. Clone and activate the following plugins, in your WordPress plugin directory:
* [wp-graphql](https://github.com/wp-graphql/wp-graphql) Exposes graphql for WordPress
* [wp-graphql-jwt-authentication](https://github.com/wp-graphql/wp-graphql-jwt-authentication) This plugin extends the [wp-graphql](https://github.com/wp-graphql/wp-graphql) plugin to provide authentication using JWT.
* [wp-graphiql](https://github.com/wp-graphql/wp-graphiql) Provides GraphiQL IDE (playground) to the WP-Admin
* [wp-graphql-woocommerce](https://github.com/wp-graphql/wp-graphql-woocommerce) Adds Woocommerce functionality to a WPGraphQL schema.


2. You can also import default wooCommerce products that come with wooCommerce Plugin for development ( if you don't have any products in your WordPress install )
WP Dashboard > Tools > WooCommerce products(CSV) : The WooCommerce default products csv file is available at `wp-content/plugins/woocommerce/sample-data/sample_products.csv`

### 2. For Authentication.

a. You can use any secret token of your choice, however it would be best if you generate one using WordPress Salt generator (https://api.wordpress.org/secret-key/1.1/salt/) to generate a Secret.
And just pick up any one of the token and add it in place of 'your-secret-token' below:

Define a Secret in `wp-config.php` of your WordPress directory:
```
define( 'GRAPHQL_JWT_AUTH_SECRET_KEY', 'your-secret-token' );
```

b. Depending on your particular environment, you may have to research how to enable these headers, but in Apache, you can do the following in your `.htaccess`:

```
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
```

## Configure :wrench:
## Configure Front end:wrench:

* Rename `client-config-example.js` to `client-config.js` and update your React Site URL

```ruby
const config = {
siteUrl: 'http://localhost:3000',
graphqlUrl: 'http://wordpressSiteUrl/graphql'
graphqlUrl: 'http://wordpressSiteUrl/graphql',
postCodeLocale: 'IN',
authTokenName: 'wp-decoupled/AUTH_TOKEN' ,
};
```

Expand Down
2 changes: 1 addition & 1 deletion components/cart/AddToCartButton.js
@@ -1,6 +1,6 @@
import { useState, useContext } from 'react';
import { AppContext } from "../context/AppContext";
import { addFirstProduct, updateCart } from "../../utils/functions";
import { addFirstProduct, updateCart } from "../../utils/cart-functions";
import Link from 'next/link';

const AddToCartButton = ( props ) => {
Expand Down
2 changes: 1 addition & 1 deletion components/cart/cart-page/CartBlocks.js
@@ -1,7 +1,7 @@
import Link from 'next/link';
import { useContext } from 'react';
import { AppContext } from "../../context/AppContext";
import { removeItemFromCart } from '../../../utils/functions';
import { removeItemFromCart } from '../../../utils/cart-functions';
import CartItem from "./CartItem";

const CartBlocks = () => {
Expand Down
2 changes: 1 addition & 1 deletion components/cart/cart-page/CartItem.js
@@ -1,5 +1,5 @@
import { useState } from 'react';
import { updateCart } from "../../../utils/functions";
import { updateCart } from "../../../utils/cart-functions";

const CartItem = ( { item, handleRemoveProductClick, setCart } ) => {

Expand Down
17 changes: 2 additions & 15 deletions components/layouts/Header.js
@@ -1,7 +1,6 @@
import Link from 'next/link';
import Router from "next/router";
import NProgress from "nprogress";
import CartIcon from "../cart/CartIcon";
import Nav from "./Nav";

Router.onRouteChangeStart = () => {
NProgress.start();
Expand All @@ -15,19 +14,7 @@ Router.onRouteChangeError = () => {
};

const Header = () => (
<nav className="wd-navbar navbar navbar-expand-lg navbar-dark bg-primary">
<Link href="/"><a className="navbar-brand">WP Decoupled</a></Link>
<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01"
aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation">
</button>

<div className="collapse navbar-collapse" id="navbarColor01">

</div>
<div className="cart">
<CartIcon />
</div>
</nav>
<Nav/>
);

export default Header;
50 changes: 50 additions & 0 deletions components/layouts/Menu.js
@@ -0,0 +1,50 @@
import Link from "next/link";
import { isUserValidated, logoutUser } from "../../utils/auth-functions";
import { useEffect, useState } from 'react';
import isEmpty from "../../validator/isEmpty";

const Menu = () => {

const [ loggedIn, setLoggedIn ] = useState( false );


const handleLogout = () => {

if ( process.browser ) {

logoutUser( '/login' );
}

};

useEffect( () => {

if ( process.browser ) {

const userValidated = isUserValidated();

// If user is not validated, then logout button should be shown.
if ( ! isEmpty( userValidated ) ) {
setLoggedIn( true );
}
}
} );

return (
<ul className="wpd-main-nav">
<li className="wpd-main-nav__list">
<Link href="/login"><a className="wpd-main-nav__link">Login</a></Link>
</li>
<li className="wpd-main-nav__list">
<Link href="/register"><a className="wpd-main-nav__link">Register</a></Link>
</li>
{ loggedIn ? (
<li className="wpd-main-nav__list">
<a className="wpd-main-nav__link" onClick={ handleLogout }>Logout</a>
</li>
) : '' }
</ul>
)
};

export default Menu;
26 changes: 26 additions & 0 deletions components/layouts/Nav.js
@@ -0,0 +1,26 @@
import Link from "next/link";
import CartIcon from "../cart/CartIcon";
import Menu from "./Menu";

const Nav = () => {
return (
<nav className="wd-navbar navbar navbar-expand-lg navbar-dark bg-primary">

<Link href="/"><a className="navbar-brand">WP Decoupled</a></Link>
<Menu/>
<button className="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01"
aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation">
</button>

<div className="collapse navbar-collapse" id="navbarColor01">

</div>
<div className="cart">
<CartIcon />
</div>

</nav>
)
};

export default Nav;
5 changes: 5 additions & 0 deletions components/message-alert/Loading.js
@@ -0,0 +1,5 @@
const Loading = ( { message } ) => (
<div className="mt-2 mb-2 text-info">{ message }</div>
);

export default Loading;
22 changes: 22 additions & 0 deletions components/message-alert/MessageAlert.js
@@ -0,0 +1,22 @@

import DOMPurify from 'dompurify';
import { wpdDecodeHtml } from "../../utils/commmon-functions";

const MessageAlert = ( { message, success, onCloseButtonClick } ) => {

return (

<div className="alert alert-dismissible mt-3" style={ { border: '1px solid #cecdcd' } }>
<button type="button" onClick={ onCloseButtonClick } className="close text-muted" data-dismiss="alert">
<small>&times;</small>
</button>
<span
className={ success ? 'text-success' : 'text-danger' }
dangerouslySetInnerHTML={ { __html: DOMPurify.sanitize( wpdDecodeHtml( message ) ) } }
/>
</div>

);
};

export default MessageAlert;
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -34,6 +34,7 @@
"apollo-cache-inmemory": "^1.6.2",
"apollo-link-http": "^1.5.14",
"cookie-parser": "^1.4.4",
"dompurify": "^2.0.3",
"dotenv": "^8.0.0",
"express": "^4.17.1",
"graphql": "^14.3.1",
Expand Down