11import { defineStore } from 'pinia'
2- import { ref , watch } from 'vue'
3- import { getUser , loginWithEmail , signupWithEmail , loginWithOAuth } from '../api'
2+ import { ref , watch , onMounted } from 'vue'
3+ import {
4+ getUser ,
5+ loginWithEmail ,
6+ signupWithEmail ,
7+ loginWithOAuth ,
8+ getAvailableOAuthProviders ,
9+ } from '../api'
410import type { OAuthProvider } from '../types'
511
612export const useAuthStore = defineStore ( 'auth' , ( ) => {
713 const token = ref < string | null > ( localStorage . getItem ( 'token' ) )
814 const email = ref < string | null > ( localStorage . getItem ( 'email' ) )
15+ const availableProviders = ref < OAuthProvider [ ] > ( [ ] )
916
1017 watch ( token , newToken => {
1118 if ( newToken ) {
@@ -23,6 +30,17 @@ export const useAuthStore = defineStore('auth', () => {
2330 }
2431 } )
2532
33+ const fetchOAuthProviders = async ( ) => {
34+ try {
35+ const response = await getAvailableOAuthProviders ( )
36+ availableProviders . value = response . data . providers
37+ } catch {
38+ availableProviders . value = [ ]
39+ }
40+ }
41+
42+ onMounted ( fetchOAuthProviders )
43+
2644 const login = async ( emailInput : string , password : string ) => {
2745 const response = await loginWithEmail ( { email : emailInput , password } )
2846 token . value = response . data . access_token
@@ -54,6 +72,7 @@ export const useAuthStore = defineStore('auth', () => {
5472 return {
5573 token,
5674 email,
75+ availableProviders,
5776 login,
5877 signup,
5978 loginWithOAuthCode,
0 commit comments