auth.client.ts 824 B

12345678910111213141516171819202122232425262728293031
  1. export default defineNuxtPlugin({
  2. name: 'auth',
  3. async setup(nuxtApp) {
  4. const { isAuthenticated, isPlaymate, restoreAuth, refreshAuth, refreshUser, refreshPlaymateInfo } = useAuth()
  5. const firstOrderDiscountStore = useFirstOrderDiscountStore()
  6. nuxtApp.hook('app:mounted', () => {
  7. restoreAuth()
  8. watch(isAuthenticated, async (newIsAuthenticated) => {
  9. if (newIsAuthenticated) {
  10. await refreshAuth()
  11. await refreshUser()
  12. await firstOrderDiscountStore.fetchChance()
  13. }
  14. else {
  15. firstOrderDiscountStore.clear()
  16. }
  17. }, {
  18. immediate: true,
  19. })
  20. watch(isPlaymate, (newIsPlaymate) => {
  21. if (newIsPlaymate) {
  22. refreshPlaymateInfo()
  23. }
  24. }, {
  25. immediate: false,
  26. })
  27. })
  28. },
  29. })