All files / src/stores auth.ts

97.5% Statements 39/40
66.66% Branches 6/9
100% Functions 8/8
97.43% Lines 38/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110                      22x 528x 528x 528x     3x 3x 2x 2x   1x 1x         3x 3x                   2x   1x 1x         2x 2x   1x 1x         3x 3x 2x 2x   1x 1x         5x 5x 4x 4x       5x     1x 1x         12x 12x   11x         11x   1x       528x                        
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import {
  signIn,
  signOut,
  signUp,
  confirmSignUp,
  getCurrentUser,
  fetchAuthSession
} from 'aws-amplify/auth'
 
export const useAuthStore = defineStore('auth', () => {
  const user = ref<any>(null)
  const isAuthenticated = computed(() => !!user.value)
  const currentTenant = ref<string | null>(null)
 
  async function login(email: string, password: string) {
    try {
      const result = await signIn({ username: email, password })
      await loadUser()
      return result
    } catch (error) {
      console.error('Login error:', error)
      throw error
    }
  }
 
  async function register(email: string, password: string, name: string) {
    try {
      const result = await signUp({
        username: email,
        password,
        options: {
          userAttributes: {
            email,
            name
          }
        }
      })
      return result
    } catch (error) {
      console.error('Signup error:', error)
      throw error
    }
  }
 
  async function confirm(email: string, code: string) {
    try {
      await confirmSignUp({ username: email, confirmationCode: code })
    } catch (error) {
      console.error('Confirmation error:', error)
      throw error
    }
  }
 
  async function logout() {
    try {
      await signOut()
      user.value = null
      currentTenant.value = null
    } catch (error) {
      console.error('Logout error:', error)
      throw error
    }
  }
 
  async function loadUser() {
    try {
      const currentUser = await getCurrentUser()
      const session = await fetchAuthSession()
      user.value = {
        ...currentUser,
        attributes: session.tokens?.idToken?.payload
      }
      currentTenant.value =
        (session.tokens?.idToken?.payload?.['custom:tenantId'] as string) || null
    } catch {
      user.value = null
      currentTenant.value = null
    }
  }
 
  async function getAccessToken(forceRefresh = false): Promise<string | null> {
    try {
      const session = await fetchAuthSession({ forceRefresh })
      // Sync currentTenant when force-refreshing so store stays consistent
      Iif (forceRefresh) {
        currentTenant.value =
          (session.tokens?.idToken?.payload?.['custom:tenantId'] as string) || null
      }
      // Use ID token for Cognito User Pool authorizer
      return session.tokens?.idToken?.toString() || null
    } catch {
      return null
    }
  }
 
  return {
    user,
    isAuthenticated,
    currentTenant,
    login,
    register,
    confirm,
    logout,
    loadUser,
    getAccessToken
  }
})