|
|
|
@ -4,6 +4,7 @@ import GitHubIcon from '../../icons/github.svg'; |
|
|
|
|
import SpinnerIcon from '../../icons/spinner.svg'; |
|
|
|
|
import Cookies from 'js-cookie'; |
|
|
|
|
import { TOKEN_COOKIE_NAME } from '../../lib/jwt'; |
|
|
|
|
import { httpGet } from '../../lib/http'; |
|
|
|
|
|
|
|
|
|
type GitHubButtonProps = {}; |
|
|
|
|
|
|
|
|
@ -26,21 +27,20 @@ export function GitHubButton(props: GitHubButtonProps) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
setIsLoading(true); |
|
|
|
|
fetch( |
|
|
|
|
httpGet<{ token: string }>( |
|
|
|
|
`${import.meta.env.PUBLIC_API_URL}/v1-github-callback${ |
|
|
|
|
window.location.search |
|
|
|
|
}`,
|
|
|
|
|
{ |
|
|
|
|
method: 'GET', |
|
|
|
|
credentials: 'include', |
|
|
|
|
} |
|
|
|
|
}` |
|
|
|
|
) |
|
|
|
|
.then((res) => res.json()) |
|
|
|
|
.then((data: any) => { |
|
|
|
|
if (!data.token) { |
|
|
|
|
setError('Something went wrong. Please try again later.'); |
|
|
|
|
.then(({ response, error }) => { |
|
|
|
|
if (!response?.token) { |
|
|
|
|
const errMessage = error?.message || 'Something went wrong.'; |
|
|
|
|
setError(errMessage); |
|
|
|
|
setIsLoading(false); |
|
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let redirectUrl = '/'; |
|
|
|
|
const gitHubRedirectAt = localStorage.getItem(GITHUB_REDIRECT_AT); |
|
|
|
|
const lastPageBeforeGithub = localStorage.getItem(GITHUB_LAST_PAGE); |
|
|
|
@ -59,9 +59,8 @@ export function GitHubButton(props: GitHubButtonProps) { |
|
|
|
|
|
|
|
|
|
localStorage.removeItem(GITHUB_REDIRECT_AT); |
|
|
|
|
localStorage.removeItem(GITHUB_LAST_PAGE); |
|
|
|
|
Cookies.set(TOKEN_COOKIE_NAME, data.token); |
|
|
|
|
Cookies.set(TOKEN_COOKIE_NAME, response.token); |
|
|
|
|
window.location.href = redirectUrl; |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
.catch((err) => { |
|
|
|
|
setError('Something went wrong. Please try again later.'); |
|
|
|
@ -69,16 +68,22 @@ export function GitHubButton(props: GitHubButtonProps) { |
|
|
|
|
}); |
|
|
|
|
}, []); |
|
|
|
|
|
|
|
|
|
const handleClick = () => { |
|
|
|
|
const handleClick = async () => { |
|
|
|
|
setIsLoading(true); |
|
|
|
|
fetch(`${import.meta.env.PUBLIC_API_URL}/v1-github-login`, { |
|
|
|
|
credentials: 'include', |
|
|
|
|
redirect: 'follow', |
|
|
|
|
}) |
|
|
|
|
.then((res) => res.json()) |
|
|
|
|
.then((data: any) => { |
|
|
|
|
// @todo proper typing for API response
|
|
|
|
|
if (data.loginUrl) { |
|
|
|
|
|
|
|
|
|
const { response, error } = await httpGet<{ loginUrl: string }>( |
|
|
|
|
`${import.meta.env.PUBLIC_API_URL}/v1-github-login` |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
if (error || !response?.loginUrl) { |
|
|
|
|
setError( |
|
|
|
|
error?.message || 'Something went wrong. Please try again later.' |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
setIsLoading(false); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// For non authentication pages, we want to redirect back to the page
|
|
|
|
|
// the user was on before they clicked the social login button
|
|
|
|
|
if (!['/login', '/signup'].includes(window.location.pathname)) { |
|
|
|
@ -86,16 +91,7 @@ export function GitHubButton(props: GitHubButtonProps) { |
|
|
|
|
localStorage.setItem(GITHUB_LAST_PAGE, window.location.pathname); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
window.location.href = data.loginUrl; |
|
|
|
|
} else { |
|
|
|
|
setError('Something went wrong. Please try again later.'); |
|
|
|
|
setIsLoading(false); |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
.catch((err) => { |
|
|
|
|
setError('Something went wrong. Please try again later.'); |
|
|
|
|
setIsLoading(false); |
|
|
|
|
}); |
|
|
|
|
window.location.href = response.loginUrl; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|