Merge pull request #9 from andriesreitsma/master

Upgrade SWR to v1
This commit is contained in:
Taylor Otwell
2022-04-04 10:10:41 -05:00
committed by GitHub
2 changed files with 7 additions and 7 deletions

View File

@@ -13,7 +13,7 @@
"next": "^12.0.4",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"swr": "^0.3.11"
"swr": "^1.2.2"
},
"devDependencies": {
"@babel/eslint-parser": "^7.12.1",

View File

@@ -6,7 +6,7 @@ import { useRouter } from 'next/router'
export const useAuth = ({ middleware, redirectIfAuthenticated } = {}) => {
const router = useRouter()
const { data: user, error, revalidate } = useSWR('/api/user', () =>
const { data: user, error, mutate } = useSWR('/api/user', () =>
axios
.get('/api/user')
.then(res => res.data)
@@ -26,7 +26,7 @@ export const useAuth = ({ middleware, redirectIfAuthenticated } = {}) => {
axios
.post('/register', props)
.then(() => revalidate())
.then(() => mutate())
.catch(error => {
if (error.response.status !== 422) throw error
@@ -42,7 +42,7 @@ export const useAuth = ({ middleware, redirectIfAuthenticated } = {}) => {
axios
.post('/login', props)
.then(() => revalidate())
.then(() => mutate())
.catch(error => {
if (error.response.status !== 422) throw error
@@ -90,9 +90,9 @@ export const useAuth = ({ middleware, redirectIfAuthenticated } = {}) => {
const logout = async () => {
if (! error) {
await axios.post('/logout')
revalidate()
await axios
.post('/logout')
.then(() => mutate())
}
window.location.pathname = '/login'