As I work on developing a mern e-commerce site, I encountered an error on the sign-in page.
Here are the steps I've taken to resolve it:
1. Updated 'Cookies->Cookie, set->Set'
2. Changed 'res.refresh_token' to res.refresh_token
3. Downgraded js-cookies version from 3 to 2.2.1
https://i.sstatic.net/3uoHz.png
Cookies.set('refreshtoken', res.refresh_token, {
path: 'api/auth/accessToken',
expires: 7
})
All code (excluding HTML):
import Head from "next/head"
import Link from "next/link"
import { useState, useContext } from "react"
import { DataContext } from "../store/GlobalState"
import { postData } from "../utils/fetchData"
import { Cookies } from 'js-cookie'
const Signin = () => {
const initialState = { email: '', password: '' }
const [userData, setUserData] = useState(initialState)
const { email, password } = userData
const { state, dispatch } = useContext(DataContext)
const handleChangeInput = e => {
const { name, value } = e.target
setUserData({ ...userData, [name]: value })
dispatch({ type: 'NOTIFY', payload: {} })
}
const handleSubmit = async e => {
e.preventDefault()
dispatch({ type: 'NOTIFY', payload: { loading: true } })
const res = await postData('auth/login', userData)
if (res.err) return dispatch({ type: 'NOTIFY', payload: { error: res.err } })
dispatch({ type: 'NOTIFY', payload: { success: res.msg } })
dispatch({
type: 'AUTH', payload: {
token: res.access_token,
user: res.user
}
})
Cookies.set('refreshtoken', res.refresh_token, {
path: 'api/auth/accessToken',
expires: 7
})
localStorage.setItem('firstLogin', true)
}
}
export default Signin