auth.client.ts 886 B

1234567891011121314151617181920212223242526272829303132
  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. console.log('newIsAuthenticated', newIsAuthenticated)
  10. if (newIsAuthenticated) {
  11. await refreshAuth()
  12. await refreshUser()
  13. await firstOrderDiscountStore.fetchChance()
  14. }
  15. else {
  16. firstOrderDiscountStore.clear()
  17. }
  18. }, {
  19. immediate: true,
  20. })
  21. watch(isPlaymate, (newIsPlaymate) => {
  22. if (newIsPlaymate) {
  23. refreshPlaymateInfo()
  24. }
  25. }, {
  26. immediate: false,
  27. })
  28. })
  29. },
  30. })