Airbnb clone, log in after registration

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

We now repeat the same process for the registration modal, with one more step: when we register, we need to send back the cookie like we do for the login process.

In pages/api/auth/register.js we first:

import Cookies from 'cookies'

then right before the call to send a successful response:

res.end(JSON.stringify({ status: 'success', message: 'User added' }))

We add the session cookie:

const cookies = new Cookies(req, res)
cookies.set('nextbnb_session', sessionToken, {
  httpOnly: true // true by default
})

In the frontend, in components/RegistrationModal.js, we import useStoreActions from easy-peasy:

import { useStoreActions } from 'easy-peasy'

Then we define setLoggedIn and setHideModal:

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

If the response is an error, we alert and we return immediately after, otherwise we set the user as logged in:

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

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

  setLoggedIn(true)
  setHideModal(true)
}

Try the application: you should be able to create a new user using the registration form, the UI should change to show that you are logged in.

To logout, you can delete the cookie manually using the devtools (you can later implement logout yourself), or open a new incognito browser window.

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 now repeat the same process for the registration modal, with one more step: when we register, we need to send back the cookie like we do for the login process.

In pages/api/auth/register.js we first:

import Cookies from 'cookies'

then right before the call to send a successful response:

res.end(JSON.stringify({ status: 'success', message: 'User added' }))

We add the session cookie:

const cookies = new Cookies(req, res)
cookies.set('nextbnb_session', sessionToken, {
  httpOnly: true // true by default
})

In the frontend, in components/RegistrationModal.js, we import useStoreActions from easy-peasy:

import { useStoreActions } from 'easy-peasy'

Then we define setLoggedIn and setHideModal:

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

If the response is an error, we alert and we return immediately after, otherwise we set the user as logged in:

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

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

  setLoggedIn(true)
  setHideModal(true)
}

Try the application: you should be able to create a new user using the registration form, the UI should change to show that you are logged in.

To logout, you can delete the cookie manually using the devtools (you can later implement logout yourself), or open a new incognito browser window.

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-21T05:00:00+00:00) Airbnb clone, log in after registration. Retrieved from https://www.scien.cx/2021/12/21/airbnb-clone-log-in-after-registration/

MLA
" » Airbnb clone, log in after registration." flaviocopes.com | Sciencx - Tuesday December 21, 2021, https://www.scien.cx/2021/12/21/airbnb-clone-log-in-after-registration/
HARVARD
flaviocopes.com | Sciencx Tuesday December 21, 2021 » Airbnb clone, log in after registration., viewed ,<https://www.scien.cx/2021/12/21/airbnb-clone-log-in-after-registration/>
VANCOUVER
flaviocopes.com | Sciencx - » Airbnb clone, log in after registration. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/21/airbnb-clone-log-in-after-registration/
CHICAGO
" » Airbnb clone, log in after registration." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/12/21/airbnb-clone-log-in-after-registration/
IEEE
" » Airbnb clone, log in after registration." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/12/21/airbnb-clone-log-in-after-registration/. [Accessed: ]
rf:citation
» Airbnb clone, log in after registration | flaviocopes.com | Sciencx | https://www.scien.cx/2021/12/21/airbnb-clone-log-in-after-registration/ |

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.