-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Login and Logout components
- Loading branch information
Showing
7 changed files
with
125 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useAuthStore } from "@/store/authStore"; | ||
import { notificationService } from "@/_services"; | ||
import { LocationQuery, LocationQueryValue, useRouter } from "vue-router"; | ||
|
||
/** | ||
* Wrapper para authStore. Interactúa con el store de autenticación y además con el router, notificaciones etc. | ||
*/ | ||
export function useAuth() { | ||
|
||
const authStore = useAuthStore(); | ||
const router = useRouter(); | ||
|
||
const login = async (payload: { username: string, password: string }, redirectTo: LocationQueryValue | null) => { | ||
try { | ||
await authStore.login(payload); | ||
|
||
notificationService.showNotification("¡Has iniciado sesión correctamente!"); | ||
if (redirectTo) { | ||
// Enviar a la redirección | ||
router.push(redirectTo as string); | ||
} else { | ||
// Redirigir al home en caso normal | ||
router.push({name: "home"}); | ||
} | ||
} catch (error: any) { | ||
let message: string; | ||
if (error.code === 600) { | ||
message = "Fallo al iniciar sesión: Credenciales inválidas"; | ||
} else { | ||
message = "Fallo al iniciar sesión: " + error.message; | ||
} | ||
notificationService.showNotification(message, 'error'); | ||
throw error; | ||
} | ||
} | ||
|
||
const logout = async () => { | ||
await authStore.signOut(); | ||
router.push({name: 'login'}); | ||
notificationService.showNotification('Has cerrado sesión correctamente'); | ||
} | ||
|
||
return { login, logout }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters