All files / src/composables useAuth.ts

100% Statements 55/55
92.3% Branches 12/13
53.33% Functions 8/15
100% Lines 55/55

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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128  1x 1x 1x                 1x                                                               1x 6x 6x 6x 5x 5x 5x 5x 5x 5x 5x 6x 6x             1x 1x 1x 1x             1x 1x 1x 1x           1x 2x 2x 2x 2x         1x 1x 1x 1x   1x 1x 1x       1x 1x 1x   1x 1x 1x 1x     1x 1x 1x   1x 1x 1x 1x   1x   1x 1x 1x    
// region Dependency Injection
import {useAuthStore} from '@/stores/auth'
import api from '@/libs/axios'
import {
  createUserWithEmailAndPassword,
  GithubAuthProvider,
  GoogleAuthProvider,
  sendPasswordResetEmail,
  signInWithEmailAndPassword,
  signInWithPopup,
  signOut,
} from "firebase/auth";
import {auth} from '@/config/firebase'
// endregion Dependency Injection
 
// region Component Import
// endregion Component Import
 
// region interface
// endregion interface
 
// region constants
// endregion constants
 
// region props
// endregion props
 
// region variable
// endregion variable
 
// region properties
// endregion properties
 
// region emits
// endregion emits
 
// region validator
// endregion validator
 
// region methods
/**
 * バックエンドと疎通
 * @param force
 */
export const syncWithBackend = async (force: boolean = true) => {
  const store = useAuthStore()
  const token = await store.refreshIdToken(force)
  if (!token) return
  const { data } = await api.post(
    '/auth/fl/login',
    null,
    {
      headers: { Authorization: `Bearer ${token}` },
    }
  )
  store.setAppUser(data ?? null)
}
 
/**
 * アカウント作成・登録
 * @param email
 * @param password
 */
export const register = async (email: string, password: string) => {
  await createUserWithEmailAndPassword(auth, email, password)
  await syncWithBackend(true)
}
 
/**
 * メアド+パスワードでログイン
 * @param email
 * @param password
 */
export const login = async (email: string, password: string) => {
  await signInWithEmailAndPassword(auth, email, password)
  await syncWithBackend(false)
}
 
/**
 * ソーシャルボタンでログイン
 * @param provider
 */
export const signInWithProvider = async (provider: 'google' | 'github') => {
  const p = provider === 'github' ? new GithubAuthProvider() : new GoogleAuthProvider()
  await signInWithPopup(auth, p)
  await syncWithBackend(false)
}
 
/**
 * ログアウト
 */
export const logout = async () => {
  useAuthStore().clear()
  await signOut(auth)
}
 
export const resetPassword = async (email: string) => {
  await sendPasswordResetEmail(auth, email)
}
// endregion methods
 
// region export
export const useAuth = () => {
  const store = useAuthStore()
  return {
    // reactive(storeそのまま)
    firebaseUser: store.firebaseUser,
    appUser: store.appUser,
    idToken: store.idToken,
    isAuthenticated: store.isAuthenticated,
 
    // actions(↑methodsを公開)
    register: (email: string, password: string) => register(email, password),
    login: (email: string, password: string) => login(email, password),
    signInWithProvider: (provider: 'google' | 'github') => signInWithProvider(provider),
    // region componentでは使ってない
    linkWithGithub: () => signInWithProvider('github'),
    linkWithGoogle: () => signInWithProvider('google'),
    syncWithBackend: (force = true) => syncWithBackend(force),
    resetPassword: (email: string) => resetPassword(email),
    // endregion componentでは使ってない
    logout: () => logout(),
    // 透過公開
    refreshIdToken: store.refreshIdToken
  }
}
// endregion export