Skip to content

Commit

Permalink
Merge API composable into auth store
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Jul 12, 2024
1 parent 5c53caf commit 572b261
Show file tree
Hide file tree
Showing 20 changed files with 1,790 additions and 1,914 deletions.
36 changes: 0 additions & 36 deletions app/assets/composables/authCheckApi.ts

This file was deleted.

5 changes: 0 additions & 5 deletions app/assets/composables/index.ts

This file was deleted.

41 changes: 0 additions & 41 deletions app/assets/composables/loginApi.ts

This file was deleted.

37 changes: 0 additions & 37 deletions app/assets/composables/logoutApi.ts

This file was deleted.

61 changes: 59 additions & 2 deletions app/assets/stores/auth.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { defineStore } from 'pinia'
import type { UserInterface } from '../interfaces'
import axios from 'axios'
import type { UserInterface, LoginForm, AlertInterface } from '../interfaces'
import { AlertStyle } from '../interfaces'

export const useAuthStore = defineStore('auth', {
persist: true,
state: () => {
return {
user: null as UserInterface | null
user: null as UserInterface | null,
loading: false,
error: null as AlertInterface[] | null
}
},
getters: {
Expand All @@ -17,6 +21,59 @@ export const useAuthStore = defineStore('auth', {
},
unsetUser(): void {
this.user = null
},
async login(form: LoginForm) {
this.loading = true
this.error = null
axios
.post('/account/login', form)
.then((response) => {
this.setUser(response.data)
})
.catch((err) => {
this.error = {
...err.response.data,
...{ style: AlertStyle.Danger, closeBtn: true }
}
})
.finally(() => {
this.loading = false
})
},
async check() {
this.loading = true
this.error = null
axios
.get('/account/auth-check')
.then((response) => {
this.setUser(response.data.user)
})
.catch((err) => {
this.unsetUser()
this.error = {
...err.response.data,
...{ style: AlertStyle.Danger, closeBtn: true }
}
})
.finally(() => {
this.loading = false
})
},
async logout() {
this.loading = true
this.error = null
this.unsetUser()
axios
.get('/account/logout')
.catch((err) => {
this.error = {
...err.response.data,
...{ style: AlertStyle.Danger, closeBtn: true }
}
})
.finally(() => {
this.loading = false
})
}
}
})
3 changes: 0 additions & 3 deletions app/assets/stores/index.ts

This file was deleted.

6 changes: 0 additions & 6 deletions dist/api.cjs

This file was deleted.

Loading

0 comments on commit 572b261

Please sign in to comment.