|
|
@@ -1,26 +1,54 @@
|
|
|
import { storeToRefs } from 'pinia'
|
|
|
import { useAuthStore } from '~/stores/auth'
|
|
|
+import { userApi } from '~/api/user'
|
|
|
|
|
|
// Authentication composable for managing user auth state via Pinia
|
|
|
export const useAuth = () => {
|
|
|
const authStore = useAuthStore()
|
|
|
- const { isAuthenticated, token } = storeToRefs(authStore)
|
|
|
-
|
|
|
- const setAuth = (value: string) => {
|
|
|
- authStore.setAuth(value)
|
|
|
- }
|
|
|
+ const { isAuthenticated, token, userProfile } = storeToRefs(authStore)
|
|
|
|
|
|
const restoreAuth = () => {
|
|
|
authStore.restoreAuth()
|
|
|
}
|
|
|
|
|
|
- const getToken = (): string | null => {
|
|
|
- return token.value
|
|
|
+ const refreshAuth = async () => {
|
|
|
+ try {
|
|
|
+ await userApi.renewalToken()
|
|
|
+ }
|
|
|
+ catch (error) {
|
|
|
+ console.error('Failed to refresh token:', error)
|
|
|
+ throw error
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ const refreshUser = async () => {
|
|
|
+ try {
|
|
|
+ const result = await userApi.getMyInfo()
|
|
|
+
|
|
|
+ authStore.setUserProfile(result.userProfile)
|
|
|
+ }
|
|
|
+ catch (error) {
|
|
|
+ console.error('Failed to fetch user:', error)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // Get user info (currently just returns token)
|
|
|
- const getUser = (): string | null => {
|
|
|
- return token.value
|
|
|
+ const login = async (type: 'google', googleData: string) => {
|
|
|
+ try {
|
|
|
+ const result = await userApi.loginWithGoogle({ data: googleData })
|
|
|
+
|
|
|
+ if (result) {
|
|
|
+ authStore.setAuth(result.token)
|
|
|
+ authStore.setUserProfile(result.userProfile)
|
|
|
+
|
|
|
+ return result
|
|
|
+ }
|
|
|
+
|
|
|
+ return null
|
|
|
+ }
|
|
|
+ catch (error) {
|
|
|
+ console.error('Failed to login:', error)
|
|
|
+ return null
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
const logout = () => {
|
|
|
@@ -29,10 +57,12 @@ export const useAuth = () => {
|
|
|
|
|
|
return {
|
|
|
isAuthenticated,
|
|
|
- setAuth,
|
|
|
+ token,
|
|
|
+ userProfile,
|
|
|
restoreAuth,
|
|
|
- getToken,
|
|
|
- getUser,
|
|
|
+ refreshAuth,
|
|
|
+ refreshUser,
|
|
|
+ login,
|
|
|
logout,
|
|
|
}
|
|
|
}
|