npm-pkg-hook 1.4.8 → 1.4.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/hooks/useLogout/index.js +45 -23
package/package.json
CHANGED
|
@@ -2,19 +2,43 @@ import { useApolloClient } from '@apollo/client'
|
|
|
2
2
|
import { useState } from 'react'
|
|
3
3
|
import { Cookies } from '../../cookies/index'
|
|
4
4
|
import { signOutAuth } from './helpers'
|
|
5
|
+
import { getCurrentDomain } from '../../utils'
|
|
5
6
|
export { signOutAuth } from './helpers'
|
|
6
7
|
|
|
7
|
-
export const useLogout = ({ setAlertBox = () => {
|
|
8
|
+
export const useLogout = ({ setAlertBox = () => {} } = {}) => {
|
|
8
9
|
const [loading, setLoading] = useState(false)
|
|
9
10
|
const [error, setError] = useState(false)
|
|
10
11
|
const client = useApolloClient()
|
|
11
12
|
|
|
13
|
+
const eliminarCookie = async (nombreCookie) => {
|
|
14
|
+
try {
|
|
15
|
+
let domain = getCurrentDomain()
|
|
16
|
+
|
|
17
|
+
// Si estás en entorno local, usa 'localhost' como dominio
|
|
18
|
+
if (domain === 'localhost') {
|
|
19
|
+
domain = undefined // Esto permitirá la cookie en 'localhost'
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const expirationTime = new Date()
|
|
23
|
+
expirationTime.setTime(expirationTime.getTime() - 1000) // Establece una fecha de expiración en el pasado
|
|
24
|
+
|
|
25
|
+
const formattedDomain = domain || getCurrentDomain()
|
|
26
|
+
|
|
27
|
+
await Cookies.remove(nombreCookie, { domain: formattedDomain, path: '/', secure: process.env.NODE_ENV === 'production' })
|
|
28
|
+
|
|
29
|
+
console.log('Cookie eliminada correctamente.')
|
|
30
|
+
} catch (error) {
|
|
31
|
+
console.error('Error al eliminar la cookie:', error)
|
|
32
|
+
throw new Error('Error al eliminar la cookie. aaaaaaaaaaaa')
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
12
36
|
const onClickLogout = async () => {
|
|
13
37
|
try {
|
|
14
38
|
setLoading(true)
|
|
15
39
|
|
|
16
40
|
// Logout from the server
|
|
17
|
-
const logoutResponse = await
|
|
41
|
+
const logoutResponse = await fetch(`${process.env.URL_BASE}/api/auth/logout/`, {
|
|
18
42
|
method: 'POST',
|
|
19
43
|
headers: {
|
|
20
44
|
'Content-Type': 'application/json'
|
|
@@ -24,33 +48,31 @@ export const useLogout = ({ setAlertBox = () => { } } = {}) => {
|
|
|
24
48
|
|
|
25
49
|
if (!logoutResponse.ok) {
|
|
26
50
|
setLoading(false)
|
|
27
|
-
//
|
|
51
|
+
// Handle unsuccessful logout request, e.g., show an error message
|
|
28
52
|
return
|
|
29
53
|
}
|
|
30
54
|
|
|
31
|
-
const response = await logoutResponse.json()
|
|
55
|
+
const response = await logoutResponse.json()
|
|
32
56
|
const { status } = response || {}
|
|
33
57
|
console.log('Intentando borrar cookies...')
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
setLoading(false)
|
|
46
|
-
console.log('Borrado todo')
|
|
47
|
-
signOutAuth({ redirect: true, callbackUrl: '/' })
|
|
48
|
-
.catch(() => {
|
|
49
|
-
setError(true)
|
|
50
|
-
setAlertBox({ message: 'Ocurrió un error al cerrar session' })
|
|
51
|
-
})
|
|
52
|
-
}
|
|
58
|
+
|
|
59
|
+
// Eliminar la cookie process.env.SESSION_NAME
|
|
60
|
+
await eliminarCookie(process.env.SESSION_NAME)
|
|
61
|
+
Cookies.remove(process.env.LOCAL_SALES_STORE)
|
|
62
|
+
Cookies.remove('restaurant')
|
|
63
|
+
Cookies.remove('usuario')
|
|
64
|
+
Cookies.remove('session')
|
|
65
|
+
|
|
66
|
+
// Clear Apollo Client cache
|
|
67
|
+
client?.clearStore()
|
|
68
|
+
|
|
53
69
|
setLoading(false)
|
|
70
|
+
console.log('Cookies eliminadas y sesión cerrada con éxito')
|
|
71
|
+
signOutAuth({ redirect: true, callbackUrl: '/' })
|
|
72
|
+
.catch(() => {
|
|
73
|
+
setError(true)
|
|
74
|
+
setAlertBox({ message: 'Ocurrió un error al cerrar sesión' })
|
|
75
|
+
})
|
|
54
76
|
} catch (error) {
|
|
55
77
|
setLoading(false)
|
|
56
78
|
setError(true)
|