Airbnb clone, change state after we login

This post is part of a new series where we build a clone of Airbnb with Next.js. See the first post here.

We have a missing piece now. If we try to login, we are not immediately logged in – we must do a page reload, which is not nice.

Let’s login the user immediately after the login process is successful.

In components/LoginModal.js we need to import the useStoreActions object from easy-peasy:

import { useStoreActions } from 'easy-peasy'

then inside the LoginModal component we initialize setLoggedIn and setHideModal:

const setLoggedIn = useStoreActions((actions) => actions.login.setLoggedIn)
const setHideModal = useStoreActions((actions) => actions.modals.setHideModal)

Then we call them inside the submit() function after we get a response:

const submit = async () => {
  const response = await axios.post('/api/auth/login', {
    email,
    password
  })

  if (response.data.status === 'error') {
    alert(response.data.message)
    return
  }

  setLoggedIn(true)
  setHideModal(true)
}

Here is the full component source code:

import { useState } from 'react'
import axios from 'axios'
import { useStoreActions } from 'easy-peasy'

export default function LoginModal(props) {
  const [email, setEmail] = useState('')
  const [password, setPassword] = useState('')

  const setLoggedIn = useStoreActions((actions) => actions.login.setLoggedIn)
  const setHideModal = useStoreActions((actions) => actions.modals.setHideModal)

  const submit = async () => {
    const response = await axios.post('/api/auth/login', {
      email,
      password
    })

    if (response.data.status === 'error') {
      alert(response.data.message)
      return
    }

    setLoggedIn(true)
    setHideModal(true)
  }

  return (
    <>
      <h2>Log in</h2>
      <div>
        <form
          onSubmit={(event) => {
            submit()
            event.preventDefault()
          }}
        >
          <input
            id="email"
            type="email"
            placeholder="Email address"
            onChange={(event) => setEmail(event.target.value)}
          />
          <input
            id="password"
            type="password"
            placeholder="Password"
            onChange={(event) => setPassword(event.target.value)}
          />
          <button>Log in</button>
        </form>
      </div>
      <p>
        Don't have an account yet?{' '}
        <a href="javascript:;" onClick={() => props.showSignup()}>
          Sign up
        </a>
      </p>
    </>
  )
}

See the code on GitHub


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

This post is part of a new series where we build a clone of Airbnb with Next.js. See the first post here.

We have a missing piece now. If we try to login, we are not immediately logged in - we must do a page reload, which is not nice.

Let’s login the user immediately after the login process is successful.

In components/LoginModal.js we need to import the useStoreActions object from easy-peasy:

import { useStoreActions } from 'easy-peasy'

then inside the LoginModal component we initialize setLoggedIn and setHideModal:

const setLoggedIn = useStoreActions((actions) => actions.login.setLoggedIn)
const setHideModal = useStoreActions((actions) => actions.modals.setHideModal)

Then we call them inside the submit() function after we get a response:

const submit = async () => {
  const response = await axios.post('/api/auth/login', {
    email,
    password
  })

  if (response.data.status === 'error') {
    alert(response.data.message)
    return
  }

  setLoggedIn(true)
  setHideModal(true)
}

Here is the full component source code:

import { useState } from 'react'
import axios from 'axios'
import { useStoreActions } from 'easy-peasy'

export default function LoginModal(props) {
  const [email, setEmail] = useState('')
  const [password, setPassword] = useState('')

  const setLoggedIn = useStoreActions((actions) => actions.login.setLoggedIn)
  const setHideModal = useStoreActions((actions) => actions.modals.setHideModal)

  const submit = async () => {
    const response = await axios.post('/api/auth/login', {
      email,
      password
    })

    if (response.data.status === 'error') {
      alert(response.data.message)
      return
    }

    setLoggedIn(true)
    setHideModal(true)
  }

  return (
    <>
      <h2>Log in</h2>
      <div>
        <form
          onSubmit={(event) => {
            submit()
            event.preventDefault()
          }}
        >
          <input
            id="email"
            type="email"
            placeholder="Email address"
            onChange={(event) => setEmail(event.target.value)}
          />
          <input
            id="password"
            type="password"
            placeholder="Password"
            onChange={(event) => setPassword(event.target.value)}
          />
          <button>Log in</button>
        </form>
      </div>
      <p>
        Don't have an account yet?{' '}
        <a href="javascript:;" onClick={() => props.showSignup()}>
          Sign up
        </a>
      </p>
    </>
  )
}

See the code on GitHub


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-12-20T05:00:00+00:00) Airbnb clone, change state after we login. Retrieved from https://www.scien.cx/2021/12/20/airbnb-clone-change-state-after-we-login/

MLA
" » Airbnb clone, change state after we login." flaviocopes.com | Sciencx - Monday December 20, 2021, https://www.scien.cx/2021/12/20/airbnb-clone-change-state-after-we-login/
HARVARD
flaviocopes.com | Sciencx Monday December 20, 2021 » Airbnb clone, change state after we login., viewed ,<https://www.scien.cx/2021/12/20/airbnb-clone-change-state-after-we-login/>
VANCOUVER
flaviocopes.com | Sciencx - » Airbnb clone, change state after we login. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/20/airbnb-clone-change-state-after-we-login/
CHICAGO
" » Airbnb clone, change state after we login." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/12/20/airbnb-clone-change-state-after-we-login/
IEEE
" » Airbnb clone, change state after we login." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/12/20/airbnb-clone-change-state-after-we-login/. [Accessed: ]
rf:citation
» Airbnb clone, change state after we login | flaviocopes.com | Sciencx | https://www.scien.cx/2021/12/20/airbnb-clone-change-state-after-we-login/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.