mirror of
https://github.com/kevin-DL/breeze-next-template.git
synced 2026-01-11 18:34:27 +00:00
@@ -13,7 +13,7 @@
|
|||||||
"next": "^12.0.4",
|
"next": "^12.0.4",
|
||||||
"react": "^17.0.2",
|
"react": "^17.0.2",
|
||||||
"react-dom": "^17.0.2",
|
"react-dom": "^17.0.2",
|
||||||
"swr": "^0.3.11"
|
"swr": "^1.2.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@babel/eslint-parser": "^7.12.1",
|
"@babel/eslint-parser": "^7.12.1",
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { useRouter } from 'next/router'
|
|||||||
export const useAuth = ({ middleware, redirectIfAuthenticated } = {}) => {
|
export const useAuth = ({ middleware, redirectIfAuthenticated } = {}) => {
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
const { data: user, error, revalidate } = useSWR('/api/user', () =>
|
const { data: user, error, mutate } = useSWR('/api/user', () =>
|
||||||
axios
|
axios
|
||||||
.get('/api/user')
|
.get('/api/user')
|
||||||
.then(res => res.data)
|
.then(res => res.data)
|
||||||
@@ -26,7 +26,7 @@ export const useAuth = ({ middleware, redirectIfAuthenticated } = {}) => {
|
|||||||
|
|
||||||
axios
|
axios
|
||||||
.post('/register', props)
|
.post('/register', props)
|
||||||
.then(() => revalidate())
|
.then(() => mutate())
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
if (error.response.status !== 422) throw error
|
if (error.response.status !== 422) throw error
|
||||||
|
|
||||||
@@ -42,7 +42,7 @@ export const useAuth = ({ middleware, redirectIfAuthenticated } = {}) => {
|
|||||||
|
|
||||||
axios
|
axios
|
||||||
.post('/login', props)
|
.post('/login', props)
|
||||||
.then(() => revalidate())
|
.then(() => mutate())
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
if (error.response.status !== 422) throw error
|
if (error.response.status !== 422) throw error
|
||||||
|
|
||||||
@@ -90,9 +90,9 @@ export const useAuth = ({ middleware, redirectIfAuthenticated } = {}) => {
|
|||||||
|
|
||||||
const logout = async () => {
|
const logout = async () => {
|
||||||
if (! error) {
|
if (! error) {
|
||||||
await axios.post('/logout')
|
await axios
|
||||||
|
.post('/logout')
|
||||||
revalidate()
|
.then(() => mutate())
|
||||||
}
|
}
|
||||||
|
|
||||||
window.location.pathname = '/login'
|
window.location.pathname = '/login'
|
||||||
|
|||||||
Reference in New Issue
Block a user