|
|
@ -3,6 +3,7 @@ import Cookies from 'js-cookie'; |
|
|
|
import GoogleIcon from '../../icons/google.svg'; |
|
|
|
import GoogleIcon from '../../icons/google.svg'; |
|
|
|
import SpinnerIcon from '../../icons/spinner.svg'; |
|
|
|
import SpinnerIcon from '../../icons/spinner.svg'; |
|
|
|
import { TOKEN_COOKIE_NAME } from '../../lib/jwt'; |
|
|
|
import { TOKEN_COOKIE_NAME } from '../../lib/jwt'; |
|
|
|
|
|
|
|
import { httpGet } from '../../lib/http'; |
|
|
|
|
|
|
|
|
|
|
|
type GoogleButtonProps = {}; |
|
|
|
type GoogleButtonProps = {}; |
|
|
|
|
|
|
|
|
|
|
@ -25,21 +26,19 @@ export function GoogleButton(props: GoogleButtonProps) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
setIsLoading(true); |
|
|
|
setIsLoading(true); |
|
|
|
fetch( |
|
|
|
httpGet<{ token: string }>( |
|
|
|
`${import.meta.env.PUBLIC_API_URL}/v1-google-callback${ |
|
|
|
`${import.meta.env.PUBLIC_API_URL}/v1-google-callback${ |
|
|
|
window.location.search |
|
|
|
window.location.search |
|
|
|
}`,
|
|
|
|
}` |
|
|
|
{ |
|
|
|
|
|
|
|
method: 'GET', |
|
|
|
|
|
|
|
credentials: 'include', |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
.then((res) => res.json()) |
|
|
|
.then(({ response, error }) => { |
|
|
|
.then((data: any) => { |
|
|
|
if (!response?.token) { |
|
|
|
if (!data.token) { |
|
|
|
setError(error?.message || 'Something went wrong.'); |
|
|
|
setError('Something went wrong. Please try again later.'); |
|
|
|
|
|
|
|
setIsLoading(false); |
|
|
|
setIsLoading(false); |
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let redirectUrl = '/'; |
|
|
|
let redirectUrl = '/'; |
|
|
|
const googleRedirectAt = localStorage.getItem(GOOGLE_REDIRECT_AT); |
|
|
|
const googleRedirectAt = localStorage.getItem(GOOGLE_REDIRECT_AT); |
|
|
|
const lastPageBeforeGoogle = localStorage.getItem(GOOGLE_LAST_PAGE); |
|
|
|
const lastPageBeforeGoogle = localStorage.getItem(GOOGLE_LAST_PAGE); |
|
|
@ -58,9 +57,8 @@ export function GoogleButton(props: GoogleButtonProps) { |
|
|
|
|
|
|
|
|
|
|
|
localStorage.removeItem(GOOGLE_REDIRECT_AT); |
|
|
|
localStorage.removeItem(GOOGLE_REDIRECT_AT); |
|
|
|
localStorage.removeItem(GOOGLE_LAST_PAGE); |
|
|
|
localStorage.removeItem(GOOGLE_LAST_PAGE); |
|
|
|
Cookies.set(TOKEN_COOKIE_NAME, data.token); |
|
|
|
Cookies.set(TOKEN_COOKIE_NAME, response.token); |
|
|
|
window.location.href = redirectUrl; |
|
|
|
window.location.href = redirectUrl; |
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
.catch((err) => { |
|
|
|
.catch((err) => { |
|
|
|
setError('Something went wrong. Please try again later.'); |
|
|
|
setError('Something went wrong. Please try again later.'); |
|
|
@ -70,14 +68,17 @@ export function GoogleButton(props: GoogleButtonProps) { |
|
|
|
|
|
|
|
|
|
|
|
const handleClick = () => { |
|
|
|
const handleClick = () => { |
|
|
|
setIsLoading(true); |
|
|
|
setIsLoading(true); |
|
|
|
fetch(`${import.meta.env.PUBLIC_API_URL}/v1-google-login`, { |
|
|
|
httpGet<{ loginUrl: string }>( |
|
|
|
credentials: 'include', |
|
|
|
`${import.meta.env.PUBLIC_API_URL}/v1-google-login` |
|
|
|
redirect: 'follow', |
|
|
|
) |
|
|
|
}) |
|
|
|
.then(({ response, error }) => { |
|
|
|
.then((res) => res.json()) |
|
|
|
if (!response?.loginUrl) { |
|
|
|
.then((data: any) => { |
|
|
|
setError(error?.message || 'Something went wrong.'); |
|
|
|
// @todo proper typing for API response
|
|
|
|
setIsLoading(false); |
|
|
|
if (data.loginUrl) { |
|
|
|
|
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// For non authentication pages, we want to redirect back to the page
|
|
|
|
// For non authentication pages, we want to redirect back to the page
|
|
|
|
// the user was on before they clicked the social login button
|
|
|
|
// the user was on before they clicked the social login button
|
|
|
|
if (!['/login', '/signup'].includes(window.location.pathname)) { |
|
|
|
if (!['/login', '/signup'].includes(window.location.pathname)) { |
|
|
@ -85,11 +86,7 @@ export function GoogleButton(props: GoogleButtonProps) { |
|
|
|
localStorage.setItem(GOOGLE_LAST_PAGE, window.location.pathname); |
|
|
|
localStorage.setItem(GOOGLE_LAST_PAGE, window.location.pathname); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
window.location.href = data.loginUrl; |
|
|
|
window.location.href = response.loginUrl; |
|
|
|
} else { |
|
|
|
|
|
|
|
setError('Something went wrong. Please try again later.'); |
|
|
|
|
|
|
|
setIsLoading(false); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
.catch((err) => { |
|
|
|
.catch((err) => { |
|
|
|
setError('Something went wrong. Please try again later.'); |
|
|
|
setError('Something went wrong. Please try again later.'); |
|
|
|