11import { browser } from '$app/environment' ;
22
3- import { toastStore } from '@codingcatdev/blackcatui' ;
43import { initializeApp , getApps , FirebaseError } from 'firebase/app' ;
5- import { getAuth , setPersistence , browserSessionPersistence , signInWithEmailAndPassword , signInWithPopup , type AuthProvider , type Auth , createUserWithEmailAndPassword } from 'firebase/auth' ;
4+ import {
5+ getAuth ,
6+ setPersistence ,
7+ browserSessionPersistence ,
8+ signInWithEmailAndPassword ,
9+ signInWithPopup ,
10+ type AuthProvider ,
11+ type Auth ,
12+ createUserWithEmailAndPassword
13+ } from 'firebase/auth' ;
614import { getFirestore , collection , doc , addDoc , onSnapshot , Firestore } from 'firebase/firestore' ;
715import { httpsCallable , getFunctions , type Functions } from 'firebase/functions' ;
8- import { getAnalytics , type Analytics , logEvent , type AnalyticsCallOptions } from "firebase/analytics" ;
16+ import {
17+ getAnalytics ,
18+ type Analytics ,
19+ logEvent ,
20+ type AnalyticsCallOptions
21+ } from 'firebase/analytics' ;
922
1023import { env } from '$env/dynamic/public' ;
1124
@@ -25,16 +38,17 @@ let db: Firestore;
2538let functions : Functions ;
2639let analytics : Analytics ;
2740
28- if ( ! app &&
41+ if (
42+ ! app &&
2943 browser &&
3044 firebaseConfig . apiKey &&
3145 firebaseConfig . authDomain &&
3246 firebaseConfig . projectId &&
3347 firebaseConfig . storageBucket &&
3448 firebaseConfig . messagingSenderId &&
3549 firebaseConfig . appId &&
36- firebaseConfig . measurementId ) {
37-
50+ firebaseConfig . measurementId
51+ ) {
3852 app = initializeApp ( firebaseConfig ) ;
3953
4054 auth = getAuth ( app ) ;
@@ -44,100 +58,82 @@ if (!app &&
4458 functions = getFunctions ( app ) ;
4559 analytics = getAnalytics ( app ) ;
4660} else {
47- console . debug ( 'Skipping Firebase Initialization, check firebaseconfig.' )
61+ console . debug ( 'Skipping Firebase Initialization, check firebaseconfig.' ) ;
4862}
4963
5064/* AUTH */
5165
5266const setCookie = ( idToken : string ) => {
5367 document . cookie = '__ccdlogin=' + idToken + ';max-age=3600' ;
54- }
68+ } ;
5569
56- export const ccdSignInWithEmailAndPassword = async ( { email, password } : { email : string , password : string } ) => {
70+ export const ccdSignInWithEmailAndPassword = async ( {
71+ email,
72+ password
73+ } : {
74+ email : string ;
75+ password : string ;
76+ } ) => {
5777 const userResponse = await signInWithEmailAndPassword ( auth , email , password ) ;
5878 const idToken = await userResponse . user . getIdToken ( ) ;
5979 setCookie ( idToken ) ;
60- }
80+ } ;
6181
62- export const ccdSignUpWithEmailAndPassword = async ( { email, password } : { email : string , password : string } ) => {
82+ export const ccdSignUpWithEmailAndPassword = async ( {
83+ email,
84+ password
85+ } : {
86+ email : string ;
87+ password : string ;
88+ } ) => {
6389 const userCredential = await createUserWithEmailAndPassword ( auth , email , password ) ;
6490 const idToken = await userCredential . user . getIdToken ( ) ;
6591 setCookie ( idToken ) ;
66- }
92+ } ;
6793
6894export const ccdSignInWithPopUp = async ( provider : AuthProvider ) => {
69- try {
70- const result = await signInWithPopup ( auth , provider )
71- const idToken = await result . user . getIdToken ( )
72-
73- if ( ! idToken )
74- throw 'Missing id Token'
75- setCookie ( idToken ) ;
76- } catch ( err ) {
77- if ( err instanceof FirebaseError ) {
78- if ( err . code === 'auth/account-exists-with-different-credential' ) {
79- toastStore . trigger ( {
80- message : 'Account Exists with Different Login Method. Please first login and then link within your Account page.' ,
81- background : 'variant-filled-warning' ,
82- } )
83- } else {
84- toastStore . trigger ( {
85- message : err . message ,
86- background : 'variant-filled-error'
87- } )
88- }
89- } else {
90- console . error ( err ) ;
91- }
92- }
93- }
95+ const result = await signInWithPopup ( auth , provider ) ;
96+ const idToken = await result . user . getIdToken ( ) ;
97+
98+ if ( ! idToken ) throw 'Missing id Token' ;
99+ setCookie ( idToken ) ;
100+ } ;
94101
95102/* DB */
96103
97104/* STRIPE */
98105export const addSubscription = async ( price : string , uid : string ) => {
99- const userDoc = doc ( collection ( db , 'stripe-customers' ) , uid )
100- const docRef = await addDoc ( collection ( userDoc , 'checkout_sessions' ) , {
106+ const userDoc = doc ( collection ( db , 'stripe-customers' ) , uid ) ;
107+ return await addDoc ( collection ( userDoc , 'checkout_sessions' ) , {
101108 price,
102109 success_url : window . location . href ,
103- cancel_url : window . location . href ,
104- } )
105- onSnapshot ( docRef , ( snap ) => {
106- const { error, url } = snap . data ( ) as { error : Error , url : string } ;
107- if ( error ) {
108- toastStore . trigger ( {
109- message : error . message ,
110- background : 'variant-filled-error'
111- } )
112- }
113- if ( url ) {
114- // We have a Stripe Checkout URL, let's redirect.
115- window . location . assign ( url ) ;
116- }
110+ cancel_url : window . location . href
117111 } ) ;
118112} ;
119113
120114/* FUNCTIONS */
121115export const openStripePortal = async ( ) => {
122116 const functionRef = httpsCallable ( functions , 'ext-firestore-stripe-payments-createPortalLink' ) ;
123- const { data } = await functionRef ( {
124- returnUrl : window . location . href ,
125- } ) as { data : { url : string } } ;
117+ const { data } = ( await functionRef ( {
118+ returnUrl : window . location . href
119+ } ) ) as { data : { url : string } } ;
126120 window . location . assign ( data . url ) ;
127- }
121+ } ;
128122
129123/* Analytics */
130- export const analyticsLogPageView = async ( eventParams ?: {
131- page_title ?: string ;
132- page_location ?: string ;
133- page_path ?: string ;
134- // eslint-disable-next-line @typescript-eslint/no-explicit-any
135- [ key : string ] : any ;
136- } , options ?: AnalyticsCallOptions ) => {
137-
124+ export const analyticsLogPageView = async (
125+ eventParams ?: {
126+ page_title ?: string ;
127+ page_location ?: string ;
128+ page_path ?: string ;
129+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
130+ [ key : string ] : any ;
131+ } ,
132+ options ?: AnalyticsCallOptions
133+ ) => {
138134 if ( firebaseConfig . apiKey ) {
139- logEvent ( analytics , " page_view" , eventParams , options )
135+ logEvent ( analytics , ' page_view' , eventParams , options ) ;
140136 } else {
141- console . debug ( 'Skipping Firebase Analytics, no key specified.' )
137+ console . debug ( 'Skipping Firebase Analytics, no key specified.' ) ;
142138 }
143- }
139+ } ;
0 commit comments