-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from aswanthabam/authentication-issues
Fix : Authentication Issues
- Loading branch information
Showing
6 changed files
with
133 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React, { useEffect, useRef } from "react"; | ||
import { GoogleIdentity } from "../../../utils/utils"; | ||
|
||
interface GoogleButtonProps {} | ||
|
||
const GoogleButton: React.FC<GoogleButtonProps> = ({}) => { | ||
const googleButtonRef = useRef<HTMLDivElement>(null); | ||
|
||
useEffect(() => { | ||
GoogleIdentity.renderGoogleSignInButton(googleButtonRef.current!); | ||
}, []); | ||
|
||
return <div ref={googleButtonRef} />; | ||
}; | ||
|
||
export default GoogleButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
export class GoogleIdentity { | ||
static initializer: Promise<boolean> | null; | ||
static is_initialized: boolean = false; | ||
static callbackFuncion: ((response: any) => void) | null = null; | ||
|
||
constructor() { | ||
GoogleIdentity.initializer = GoogleIdentity.initializeGoogleIdentity().then( | ||
(val) => { | ||
GoogleIdentity.is_initialized = val; | ||
return val; | ||
} | ||
); | ||
} | ||
|
||
/* | ||
Initialize google identity service | ||
*/ | ||
|
||
static initializeGoogleIdentity(): Promise<boolean> { | ||
return new Promise<boolean>((resolve) => { | ||
var script = document.createElement("script"); | ||
script.src = "https://accounts.google.com/gsi/client"; | ||
script.defer = true; | ||
script.async = true; | ||
script.onload = () => { | ||
(window as any).google.accounts.id.initialize({ | ||
client_id: | ||
"1025507377861-ksv14u42p6c0bes203hkbki7n56u6v80.apps.googleusercontent.com", | ||
callback: GoogleIdentity.callbackFuncion, | ||
}); | ||
console.log("Google Identity initialized"); | ||
resolve(true); | ||
}; | ||
document.body.appendChild(script); | ||
}); | ||
} | ||
|
||
/* | ||
Call back function that handles google sign in action | ||
*/ | ||
|
||
static setCallBack(callbackFuncion: (response: any) => void) { | ||
GoogleIdentity.callbackFuncion = callbackFuncion; | ||
} | ||
|
||
/* | ||
Show google one tap popup for sign in | ||
*/ | ||
|
||
static showGoogleOneTapPopup() { | ||
if (!GoogleIdentity.initializer) new GoogleIdentity(); | ||
GoogleIdentity.initializer!.then((val) => { | ||
if (val) { | ||
(window as any).google.accounts.id.prompt(); | ||
} | ||
}); | ||
} | ||
|
||
/* | ||
Render google sign in button | ||
*/ | ||
|
||
static renderGoogleSignInButton(parentElement: HTMLElement) { | ||
if (!GoogleIdentity.initializer) new GoogleIdentity(); | ||
GoogleIdentity.initializer!.then((val) => { | ||
if (val) { | ||
(window as any).google.accounts.id.renderButton(parentElement, { | ||
theme: "dark", | ||
size: "large", | ||
type: "standard", | ||
text: "signin", | ||
shape: "pill", | ||
longtitle: false, | ||
onsuccess: GoogleIdentity.callbackFuncion, | ||
}); | ||
} else { | ||
console.log("Google Identity not initialized"); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export const isLoggedIn = () => { | ||
return localStorage.getItem("token") ? true : false; | ||
}; |