Skip to content

Commit

Permalink
Get store only when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Jul 11, 2024
1 parent be6b688 commit 7885b4c
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 193 deletions.
5 changes: 3 additions & 2 deletions app/assets/composables/authCheckApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { ref } from 'vue'
import axios from 'axios'
import { type AlertInterface, AlertStyle } from '../interfaces'
import { useAuthStore } from '../stores'
const authStore = useAuthStore()

/**
* Composable used to communicate with the `/auth/check` api. Calling "check"
* will fetch the user info from the server and set the frontend object.
*/
export function useCheckApi(auth: typeof authStore) {
export function useCheckApi() {
const loading = ref(false)
const error = ref<AlertInterface | undefined>()

Expand All @@ -18,9 +17,11 @@ export function useCheckApi(auth: typeof authStore) {
axios
.get('/account/auth-check')
.then((response) => {
const auth = useAuthStore()
auth.setUser(response.data.user)
})
.catch((err) => {
const auth = useAuthStore()
auth.unsetUser()
error.value = {
...err.response.data,
Expand Down
4 changes: 2 additions & 2 deletions app/assets/composables/loginApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import axios from 'axios'
import { type AlertInterface, AlertStyle } from '../interfaces'
import { useAuthStore } from '../stores'
import type { LoginForm } from '../interfaces'
const authStore = useAuthStore()

/**
* Composable used to communicate with the `/auth/login` api. Calling "login"
* with the user login data will validate the data with the server. If login is
* successful, the user will be set on the frontend object. Otherwise, an error
* will be defined.
*/
export function useLoginApi(auth: typeof authStore) {
export function useLoginApi() {
const loading = ref(false)
const error = ref<AlertInterface | undefined>()

Expand All @@ -22,6 +21,7 @@ export function useLoginApi(auth: typeof authStore) {
axios
.post('/account/login', form)
.then((response) => {
const auth = useAuthStore()
auth.setUser(response.data)
})
.catch((err) => {
Expand Down
7 changes: 5 additions & 2 deletions app/assets/composables/logoutApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ import { ref } from 'vue'
import axios from 'axios'
import { type AlertInterface, AlertStyle } from '../interfaces'
import { useAuthStore } from '../stores'
const authStore = useAuthStore()

/**
* Composable used to communicate with the `/auth/logout` api. Calling "logout"
* will send the request to logout the user server side and delete the frontend
* user object.
*/
export function useLogoutApi(auth: typeof authStore) {
export function useLogoutApi() {
const loading = ref(false)
const error = ref<AlertInterface | undefined>()

const logout = () => {
loading.value = true
error.value = undefined

// Unset user in store
const auth = useAuthStore()
auth.unsetUser()

axios
.get('/account/logout')
.catch((err) => {
Expand Down
10 changes: 5 additions & 5 deletions dist/api.cjs

Large diffs are not rendered by default.

Loading

0 comments on commit 7885b4c

Please sign in to comment.