Skip to content

Commit b97d01e

Browse files
committed
Replace github login with google login:
- Reason: FIREBASE ISSUE: firebase/firebase-js-sdk#4256 - Add google icon - Handle navigator exception
1 parent 4b5d564 commit b97d01e

File tree

5 files changed

+96
-13
lines changed

5 files changed

+96
-13
lines changed

components/SigninButton.tsx

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { useAuth } from "@/context/AuthContext";
22
import React from "react";
33
import Button from "./form/Button";
4-
import GithubIcon from "./icons/GithubIcon";
4+
import GoogleIcon from "./icons/GoogleIcon";
55

66
const SigninButton = () => {
7-
const { loginWithGithub } = useAuth();
7+
const { loginWithGoogle } = useAuth();
88

99
return (
10-
<Button data-testid="signin-btn" onClick={loginWithGithub}>
11-
<GithubIcon className="inline-flex mr-1" />
12-
Signin
13-
</Button>
10+
<>
11+
<Button data-testid="signin-btn" onClick={loginWithGoogle}>
12+
<GoogleIcon className="inline-flex mr-2" />
13+
Signin
14+
</Button>
15+
</>
1416
);
1517
};
1618

components/editor/SnippngControlHeader.tsx

+6-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,12 @@ const SnippngControlHeader = () => {
136136
</ColorPicker>
137137
<Button
138138
onClick={() => {
139-
if (!navigator) return;
140-
navigator.clipboard.writeText(code).then(() => {
139+
if (!navigator?.clipboard)
140+
return addToast({
141+
message: "navigator unavailable",
142+
type: "error",
143+
});
144+
navigator.clipboard?.writeText(code).then(() => {
141145
addToast({
142146
message: "Code snippet copied!",
143147
});

components/icons/GoogleIcon.tsx

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React from "react";
2+
3+
const GoogleIcon: React.FC<React.SVGAttributes<SVGSVGElement>> = ({
4+
...props
5+
}) => {
6+
return (
7+
<svg
8+
xmlns="http://www.w3.org/2000/svg"
9+
width={18}
10+
height={18}
11+
preserveAspectRatio="xMidYMid"
12+
viewBox="0 0 256 262"
13+
{...props}
14+
>
15+
<path
16+
fill="#4285F4"
17+
d="M255.878 133.451c0-10.734-.871-18.567-2.756-26.69H130.55v48.448h71.947c-1.45 12.04-9.283 30.172-26.69 42.356l-.204 1.622 38.755 30.023 2.685.268c24.659-22.774 38.875-56.282 38.875-96.027"
18+
/>
19+
<path
20+
fill="#34A853"
21+
d="M130.55 261.1c35.248 0 64.839-11.605 86.453-31.622l-41.196-31.913c-11.024 7.688-25.82 13.055-45.257 13.055-34.523 0-63.824-22.773-74.269-54.25l-1.531.13-40.298 31.187-.527 1.465C35.393 231.798 79.49 261.1 130.55 261.1"
22+
/>
23+
<path
24+
fill="#FBBC05"
25+
d="M56.281 156.37c-2.756-8.123-4.351-16.827-4.351-25.82 0-8.994 1.595-17.697 4.206-25.82l-.073-1.73L15.26 71.312l-1.335.635C5.077 89.644 0 109.517 0 130.55s5.077 40.905 13.925 58.602l42.356-32.782"
26+
/>
27+
<path
28+
fill="#EB4335"
29+
d="M130.55 50.479c24.514 0 41.05 10.589 50.479 19.438l36.844-35.974C195.245 12.91 165.798 0 130.55 0 79.49 0 35.393 29.301 13.925 71.947l42.211 32.783c10.59-31.477 39.891-54.251 74.414-54.251"
30+
/>
31+
</svg>
32+
);
33+
};
34+
35+
export default GoogleIcon;

context/AuthContext.tsx

+40-3
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,34 @@ import { onAuthStateChanged } from "firebase/auth";
33
import {
44
User,
55
GithubAuthProvider,
6+
GoogleAuthProvider,
67
signInWithPopup,
78
signOut,
89
} from "firebase/auth";
910
import { auth } from "@/config/firebase";
1011
import { useToast } from "./ToastContext";
1112

12-
const provider = new GithubAuthProvider();
13+
const GithubProvider = new GithubAuthProvider();
14+
const GoogleProvider = new GoogleAuthProvider();
1315

1416
const AuthContext = createContext<{
1517
user: User | null;
18+
/**
19+
* Using mobile browsers in NON-incognito mode with **github authentication** results in the `missing initial state` error.
20+
*
21+
* This issue is still unresolved from the firebase side.
22+
*
23+
* FIREBASE ISSUE: https://github.com/firebase/firebase-js-sdk/issues/4256
24+
*
25+
* @deprecated
26+
*/
1627
loginWithGithub: () => Promise<void>;
28+
loginWithGoogle: () => Promise<void>;
1729
logout: () => Promise<void>;
1830
}>({
1931
user: null,
2032
loginWithGithub: async () => {},
33+
loginWithGoogle: async () => {},
2134
logout: async () => {},
2235
});
2336

@@ -29,10 +42,32 @@ const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
2942
const { addToast } = useToast();
3043
const [user, setUser] = useState<User | null>(null);
3144

45+
/**
46+
* Using mobile browsers in NON-incognito mode with **github authentication** results in the `missing initial state` error.
47+
*
48+
* This issue is still unresolved from the firebase side.
49+
*
50+
* FIREBASE ISSUE: https://github.com/firebase/firebase-js-sdk/issues/4256
51+
*
52+
* @deprecated
53+
*/
3254
const loginWithGithub = async () => {
3355
if (!auth) return console.log(Error("Firebase is not configured")); // This is to handle error when there is no `.env` file. So, that app doesn't crash while developing without `.env` file.
3456
try {
35-
await signInWithPopup(auth, provider);
57+
await signInWithPopup(auth, GithubProvider);
58+
addToast({
59+
message: "Signed in successfully",
60+
description: "Hey there!",
61+
});
62+
} catch (error) {
63+
console.log("Error while logging in", error);
64+
}
65+
};
66+
67+
const loginWithGoogle = async () => {
68+
if (!auth) return console.log(Error("Firebase is not configured")); // This is to handle error when there is no `.env` file. So, that app doesn't crash while developing without `.env` file.
69+
try {
70+
await signInWithPopup(auth, GoogleProvider);
3671
addToast({
3772
message: "Signed in successfully",
3873
description: "Hey there!",
@@ -62,7 +97,9 @@ const AuthProvider: React.FC<{ children: React.ReactNode }> = ({
6297
}, []);
6398

6499
return (
65-
<AuthContext.Provider value={{ user, loginWithGithub, logout }}>
100+
<AuthContext.Provider
101+
value={{ user, loginWithGithub, loginWithGoogle, logout }}
102+
>
66103
{children}
67104
</AuthContext.Provider>
68105
);

pages/profile.tsx

+7-2
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,14 @@ const UserProfile = () => {
177177
className="md:w-[unset] w-full"
178178
StartIcon={LinkIcon}
179179
onClick={() => {
180-
if (!navigator) return;
180+
if (!navigator?.clipboard)
181+
return addToast({
182+
message:
183+
"navigator unavailable",
184+
type: "error",
185+
});
181186
navigator.clipboard
182-
.writeText(
187+
?.writeText(
183188
`${window.location.host}/snippet/${snippet.uid}`
184189
)
185190
.then(() => {

0 commit comments

Comments
 (0)