Getting Started with Appwrite and creating a Login Page with Appwrite and React! (Part -1)
Tags: react, appwrite, hacktoberfest
Published: 2021-10-21 14:56:45

> Appwrite is a self-hosted solution that provides developers with a set of easy-to-use and integrate REST APIs to manage their core backend needs.

**Some key features :**
...

**1. Database**
> Store, query and manage access control to your app documents

**2. Storage** 
> Upload, download and preview your app and users files and media

**3. Users**
> Authenticate, confirm and manage your users using multiple signin methods

**4. GEO & Localization**
> Detect your users location, locale and fetch GEO related data

**5. Functions**
> Run your backend code in a secure and isolated environment to customize your app

**6. Console**
> Track your backend API usage and manage your project resources from a modern UI

**7. Privacy**
> Track your backend API usage and manage your project resources from a modern UI

**8. Security**
> Built in end to end security for your backend API both in transit and at rest



So now, without any futher delay let's get started! 😃

###✅Installing Appwrite : 
Before installing please make sure your systems meets the minimum requirement of 1 CPU core and 2GB of RAM, and an operating system that supports Docker

In this tutorial we'll be using **Docker** to install Appwrite.
So before moving forward please make sure you've docker installed [Install Docker](https://www.docker.com/products/docker-desktop).

The easiest way to start running your Appwrite server is by running our Docker installer tool from your terminal.

**Unix :**
```
docker run -it --rm \
    --volume /var/run/docker.sock:/var/run/docker.sock \
    --volume "$(pwd)"/appwrite:/usr/src/code/appwrite:rw \
    --entrypoint="install" \
    appwrite/appwrite:0.11.0
```
**Windows :** 
CMD : 
```
docker run -it --rm ^
    --volume //var/run/docker.sock:/var/run/docker.sock ^
    --volume "%cd%"/appwrite:/usr/src/code/appwrite:rw ^
    --entrypoint="install" ^
    appwrite/appwrite:0.11.0
```
Powershell : 
```
docker run -it --rm ,
    --volume /var/run/docker.sock:/var/run/docker.sock ,
    --volume ${pwd}/appwrite:/usr/src/code/appwrite:rw ,
    --entrypoint="install" ,
    appwrite/appwrite:0.11.0
```


...


###✅Setting Up Appwrite : 
Once you've appwrite installed and running on your local machine, go ahead open up  **http://localhost:3000/** in your favourite Browsers and you should see this : 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1nec21nxidvnr54bchxc.png)
Go ahead and create your account, and login to you appwrite account. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p1es8cktltsljmksnz6n.png)
Then click on create project and Enter your project's Name
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vozvbew53hqkqz2r6bu4.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ib6y5atllavsdzv7dz1s.png)
 
**If you see a console like this 👆 . Congratulations you've just  successfully setup your first App with Appwrite!🥳** 

Hold on we're not done yet!😅
 

Let's move on to creating a complete authentication with Appwrite.

> Under platforms tab Click on **Add Platform** and select **New Web App** Then name your web app and set your hostname to **localhost** (If you're hosting your appwrite somewhere else then feel free to enter domain of that address.)

As you hit register you'll find your platform added : 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5t0q8sw4dyk1lu83yjho.png)

...


###✅Getting Started with our Login App :
Navigate to Users Tab :
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yjthx7zpj8dylfdge69e.png)
And make sure that **Email/Password** Auth is enabled. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ma05bakjfdvrowadrm17.png)

...


###✅Setting up React project :
Before moving on Make sure you've installed npm or yarn.
To create react app,  create a new folder in your directory and open terminal in that folder then simply run 
```
npx create-react-app <Name of the App>
```
then open that folder in your code editor. 

To start your development server, run :
```
npm start 
```
or 
```
yarn start
```
in your terminal. Once your development server starts open up http://localhost:3000/ in your browser .
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mu66foodxq6wqjyktt49.png)
...

###✅Installing Required Packages/Dependencies :

To move forward we'll first install all the packages that we're going to need while developing our app.
1. Installing Appwrite : 
```
npm install appwrite
```
or
```
yarn add appwrite
```
2. Installing SASS (**optional if you want to use SASS for styling**)
```
npm install node-sass@5.0.0
```
or
```
yarn add node-sass@5.0.0
```
 > Why older version? Latest version of sass happens to be conflicting with some React's dependencies but 5.0.0 works fine so to avoid any error we'll be using SASS 5.0.0


...


###✅Let's start building!  :
Now, we have completely setup our React Application. Let's start with building our App : 
Open up your react project in your code-editor and navigate to **src/App.js**
You should see default code : 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ymk6i51owdp08eulu157.png)
Clear all the code in this file and we'll first start by importing React and setting up App Class.
```
import React from 'react'


class App extends React.Component {

  render() {
    return <h1> Hello World</h1>
  }
 
}

export default App
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u3td8vpqgfzlltndgtu7.png)
And you should see *Hello world* Printed in your browser.
 

> **We'll not be using Routing or Hooks in this tutorial just for the sake of simplicity, we'll keep this tutorial beginner friendly.** 

...


> ** *From here you have two options, either follow along with my code snippets or create and design your own signin and signup page with two functions handle submit and handle change and then following along from step next to the following step* **

...


###✅Creating and designing Signin and Signup Page:
We'll begin by creating a new file for our sign and signup page **src/SignInAndSignUp.jsx**
we'll import basic files in here and setup this file :
```
import React from 'react';

import './SignInAndSignUp.styles.scss'
;



const SignInAndSignUpPage = ()=>(
    <div className="sign-in-and-sign-up">

    </div>
);

export default SignInAndSignUpPage; 

```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lvg3fl5f43i76kw92qqk.png)

Let's create Sign In and SignUp components : 
Create a new directory in src name it Components **src/Components**


inside components create a new file with name SignIn.component.jsx
and SignIn.styles.scss


**src/Component/SignIn.component.jsx**
**src/Component/SignIn.styles.scss**


Now, In SignIn.component.jsx we'll create our form for SignIn.
Before that to keep our app well styled let's create our custom buttons and Form Inputs

In **/src/** create a new folder called *Utils/* Then a new file 

**src/Utils/FormInput.jsx** and 
**src/Utils/FormInput.styles.scss**


and two files for custom buttons : 
**src/Utils/CustomButton.jsx** and 
**src/Utils/CustomButton.styles.scss**

> **Why these many files?** It is usually the best practise in react to modularise our code in such a way that its easily 
manageable and scalable.



Now without extending this tutorial long I'll be providing code snippets to these files as they don't require any explanation.

**CustomButton.jsx**
```
import React from 'react'

import './custom-button.styles.scss'


const CustomButton = ({children, isGoogleSignIn, ...otherProps})=>(
    <button className={ `${isGoogleSignIn ? 'google-sign-in' : ''} custom-button` } {...otherProps}>
        {children}
    </button>
)

export default CustomButton;

```
> Styles for Custom Button [Custombutton.styles.scss File](https://github.com/kunals131/Login-App-With-Appwrite/blob/main/src/Utils/CustomButton.styles.scss)

...

**FormInput.jsx**
```
import React from 'react'

import './CustomButton.styles.scss'


const CustomButton = ({children, isGoogleSignIn, ...otherProps})=>(
    <button className={ `${isGoogleSignIn ? 'google-sign-in' : ''} custom-button` } {...otherProps}>
        {children}
    </button>
)

export default CustomButton;

```

> Styles for FormInput [FormInput.styles.scss File](https://github.com/kunals131/Login-App-With-Appwrite/blob/main/src/Utils/FormInput.styles.scss)

...


Now Navigate back to SignIn.component.jsx file and we'll start building our signin page.
Firstly we'll import all the required files and packages
and then create SignIn Class Component with state that contians relevant fields :
```
import React from 'react'

import CustomButton from '../Utils/CustomButton';
import FormInput from '../Utils/FormInput';

class SignIn extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            email : '',
            password : ''
        }
    }
}
```
Your Code and file structure should look somewhere like this  :
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ak81mft5npzcbid8238k.png)
 Now Let's render our form through this : 
```
import React from 'react'

import CustomButton from '../Utils/CustomButton';
import FormInput from '../Utils/FormInput';

class SignIn extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            email : '',
            password : ''
        }
    }

    handleSubmit = async event=>{
        console.log('Form Submitted')
        
    }

    handleChange  = event=>{
        console.log('Input value changed')
    }

    render() {
        return(
            <div className= 'sign-in'>
                <h2>I already have an account</h2>
                <span>Sign in with email and password</span>

                <form onSubmit={this.handleSubmit}>
                    <FormInput type="email" name="email" label = "Email" value = {this.state.email}
                        onChange = {this.handleChange} required
                        
                    />
                    <FormInput type="password" name="password" value = {this.state.password}
                     onChange = {this.handleChange} label="Password"
                     required />

                     <div className="buttons">
                    <CustomButton type="submit">Sign In </CustomButton>
                    <CustomButton onClick={this.handleSubmit} isGoogleSignIn>
                        {''}
                        Sign in with google {''}
                    </CustomButton>
                    </div>     
                                        
                </form>
            </div>
        )
    }
}

export default SignIn;
```
> Styles for SignIn Page [SignIn.styles.scss File](https://github.com/kunals131/Login-App-With-Appwrite/blob/main/src/Component/SignIn.styles.scss)


For now we're not doing anything with handle Submit or handle Change, we'll add functionalities to those function as we integrate our appwrite.

Now Let's add our SignIn Component to our SignIn and SignIn Page :
Navigate to `src/SignInAndSignUp.jsx` and import SignIn component and add it to the return statement
```
import React from 'react';

import SignIn from './Component/SignIn.component';
import './SignInAndSignUp.styles.scss'



const SignInAndSignUpPage = ()=>(
    <div className="sign-in-and-sign-up">
        <SignIn></SignIn>
    </div>
);

export default SignInAndSignUpPage; 
```

> Styles for SignInAndSignUpPage Page [SignInAndSignUp.styles.scss File](https://github.com/kunals131/Login-App-With-Appwrite/blob/main/src/SignInAndSignUp.styles.scss)

...



Let's see how our app looks so far, render the SignInAndSignUp Page from App.js
Navigate to `src/App.js` and import SignInAndSignUpPage and call it in the render function. 
```
 render() {
    return (
      <div>
        <SignInAndSignUpPage></SignInAndSignUpPage>
      </div>
    )
  }
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3doydymzcvopj32jsaj.png)
#####Now Let's move on to SignUp Page
In components directory create two new files SignUp.component.jsx and SignUp.styles.scss

Steps to understand the below Code :
1. Import all required files and packages
2. Define State with relevant fields.
3. Then We'll design our form using our FormInput components and Custom Buttons.
4. On Submission we'll trigger the function handle change which doesn't do anything for now, we'll assign it the functionality as we integrate appwrite.
5. Export the class Component.
```
import React from 'react'
import CustomButton from '../Utils/CustomButton';
import FormInput from '../Utils/FormInput';
import './SignUp.styles.scss'


class SignUp extends React.Component{
    constructor(){
        super();

        this.state = {
            displayName : '',
            email : '',
            password : '',
            confirmPassword : ''
        }
    }

    handleSubmit = async event =>{
        
    }

    handleChange = event=>{


    }

    render() {
        const {displayName, email,password,confirmPassword} = this.state;
        return(
            <div className="sign-up">
                <h2>I do not have a account!</h2>
                <span>Sign Up with your email and password</span>
                <form className='sign-up-form' onSubmit={this.handleSubmit}>
                <FormInput
                    type = 'text'
                    name = 'displayName'
                    value = {displayName}
                    onChange={this.handleChange}
                    label = 'Display Name'
                    required
                />
                <FormInput
                    type = 'email'
                    name = 'email'
                    value = {email}
                    onChange={this.handleChange}
                    label = 'Email'
                />
                <FormInput
                    type = 'password'
                    name = 'password'
                    value = {password}
                    onChange={this.handleChange}
                    label = 'Password'
                />
                <FormInput
                    type = 'password'
                    name = 'confirmPassword'
                    value = {confirmPassword}
                    onChange={this.handleChange}
                    label = 'Confirm Password'
                />
                <CustomButton type='submit'>SIGN UP</CustomButton>
                </form>
            </div>
        )
    }
}

export default SignUp

```

> Styles for SignUp Page [SignUp.styles.scss File](https://github.com/kunals131/Login-App-With-Appwrite/blob/main/src/Component/SignUp.styles.scss)

Now Let's Import our SignUp Component in SignInAndSignUpPage.jsx file
`import SignUp from './Components/SignUp.component`
and then include that in return statement as well
```
const SignInAndSignUpPage = ()=>(
    <div className="sign-in-and-sign-up">
        <SignIn></SignIn>
        <SignUp></SignUp>
    </div>
);
```

**Let's see how our App looks so far!**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/srtahvfzgc2r9c7bca4b.png)

Now, without extending this tutorial any longer, we'll integrate Appwrite to our app in part 2. 


### About Appwrite Community :
I hope you enjoyed this tutorial so far, and you must be excited to use appwrite in your future projects. And If you need any help then join appwrite's community of developers : [Discord](discord.com)

**Other links and resources :**
[Github Repo of project](https://github.com/kunals131/Login-App-With-Appwrite)
[Appwrite Docs](https://appwrite.io/docs/)
[Appwrite Community](https://appwrite.io/community)

## Happy Appwriting! ♥

{% link https://dev.to/kunals131/getting-started-with-appwrite-and-creating-a-login-page-with-appwrite-and-react-part-2-4fgd %}