Skip to content

Commit 03dbf2b

Browse files
in the spirit of 'https://firebase.google.com/docs/auth/web/custom-dependencies' and 'firebase/firebase-js-sdk#4946' I am now deferring the loading of 'signInWithPopup' iFrame ... which pagespeed insights complains about. This is done by swapping getAuth(app) for initializeAuth(app,..) and then contextually loading the 'browserPopupRedirectResolver' method and passing as a third parameter to 'signInWithPopup' in both google and twitter login functions
1 parent 678fbc5 commit 03dbf2b

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

.svelte-kit/ambient.d.ts

-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ declare module '$env/static/private' {
123123
export const npm_node_execpath: string;
124124
export const npm_config_prefix: string;
125125
export const COLORTERM: string;
126-
export const NODE_ENV: string;
127126
}
128127

129128
/**
@@ -257,7 +256,6 @@ declare module '$env/dynamic/private' {
257256
npm_node_execpath: string;
258257
npm_config_prefix: string;
259258
COLORTERM: string;
260-
NODE_ENV: string;
261259
[key: `PUBLIC_${string}`]: undefined;
262260
[key: string]: string | undefined;
263261
}

src/lib/Login/loginFunctions.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ export async function magicLinkToEmail(EMAIL) {
4242
// TODO: nov29.2022 : google login didnt work with 'signInWithRedirect' logic, so assuming the same for twitter login .. getting rid of the if-else logic and keeping only the 'signInWithPopup' logic
4343
export async function TwitterLogin() {
4444
// const auth = await import("$lib/firebase");
45-
const { TwitterAuthProvider } = await import("firebase/auth");
45+
const { TwitterAuthProvider, browserPopupRedirectResolver } = await import(
46+
"firebase/auth"
47+
);
4648
const provider = new TwitterAuthProvider();
4749

4850
// if (get(lessThan768)) {
@@ -73,7 +75,8 @@ export async function TwitterLogin() {
7375
// else {
7476
const { signInWithPopup } = await import("firebase/auth");
7577
// console.log("twitter provider?", provider.providerId);
76-
signInWithPopup(auth, provider)
78+
// signInWithPopup(auth, provider)
79+
signInWithPopup(auth, provider, browserPopupRedirectResolver)
7780
.then((result) => {
7881
// This gives you a the Twitter OAuth 1.0 Access Token and Secret.
7982
// You can use these server side with your app's credentials to access the Twitter API.
@@ -101,11 +104,13 @@ export async function TwitterLogin() {
101104
// TODO: nov29.2022 noticed that 'signinWithRedirect' logic did not sign me in ... furthermore 'signInWithPopup' worked perfectly on both the PWA and on the mobile version of the website ... It appears firebase has consolidated the two??
102105
export async function GoogleLogin() {
103106
// const auth = await import("$lib/firebase");
107+
104108
const {
105109
GoogleAuthProvider,
106110
// setPersistence,
107111
// browserSessionPersistence,
108112
// inMemoryPersistence,
113+
browserPopupRedirectResolver,
109114
} = await import("firebase/auth");
110115

111116
// if (get(lessThan768)) {
@@ -134,7 +139,8 @@ export async function GoogleLogin() {
134139

135140
// setPersistence(auth, browserSessionPersistence).then(() => {
136141
const provider = new GoogleAuthProvider();
137-
signInWithPopup(auth, provider)
142+
// signInWithPopup(auth, provider)
143+
signInWithPopup(auth, provider, browserPopupRedirectResolver)
138144
.then((result) => {
139145
// This gives you a Google Access Token. You can use it to access the Google API.
140146
const credential = GoogleAuthProvider.credentialFromResult(result);

src/lib/firebase.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import * as NV from "$env/static/public";
22
import { initializeApp } from "firebase/app";
3-
import { getAuth } from "firebase/auth";
3+
// import { getAuth } from "firebase/auth";
4+
5+
import {
6+
initializeAuth,
7+
indexedDBLocalPersistence,
8+
browserLocalPersistence,
9+
browserSessionPersistence,
10+
} from "firebase/auth";
411

512
// import { getFirestore } from "firebase/firestore/lite";
613

@@ -14,7 +21,16 @@ const firebaseConfig = {
1421
};
1522

1623
export const app = initializeApp(firebaseConfig);
17-
export const auth = getAuth(app);
24+
// export const auth = getAuth(app);
25+
26+
export const auth = initializeAuth(app, {
27+
persistence: [
28+
indexedDBLocalPersistence,
29+
browserLocalPersistence,
30+
browserSessionPersistence,
31+
],
32+
});
33+
1834
// this is used when website is mounted to persist login state ... maybe import when login clicked instead??
1935

2036
// idea: import getAuth on page load IF visitor as logged in before ... e.g. can set a localstorage variable ("previouslyLoggedIn") inside the logged in logic (not here)

vite.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { sveltekit } from "@sveltejs/kit/vite";
44
const config = {
55
plugins: [sveltekit()],
66
build: {
7-
minify: true,
7+
minify: true, // added dec 13,2022; not sure of perf gains
88
rollupOptions: {
99
external: [
1010
"/Users/brightowl/Documents/GitHub/sveltekit/node_modules/dropzone/dist/dropzone.css",

0 commit comments

Comments
 (0)