|
|
|
@ -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,42 +27,40 @@ 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 { |
|
|
|
|
let redirectUrl = '/'; |
|
|
|
|
const gitHubRedirectAt = localStorage.getItem(GITHUB_REDIRECT_AT); |
|
|
|
|
const lastPageBeforeGithub = localStorage.getItem(GITHUB_LAST_PAGE); |
|
|
|
|
|
|
|
|
|
// If the social redirect is there and less than 30 seconds old
|
|
|
|
|
// redirect to the page that user was on before they clicked the github login button
|
|
|
|
|
if (gitHubRedirectAt && lastPageBeforeGithub) { |
|
|
|
|
const socialRedirectAtTime = parseInt(gitHubRedirectAt, 10); |
|
|
|
|
const now = Date.now(); |
|
|
|
|
const timeSinceRedirect = now - socialRedirectAtTime; |
|
|
|
|
|
|
|
|
|
if (timeSinceRedirect < 30 * 1000) { |
|
|
|
|
redirectUrl = lastPageBeforeGithub; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
localStorage.removeItem(GITHUB_REDIRECT_AT); |
|
|
|
|
localStorage.removeItem(GITHUB_LAST_PAGE); |
|
|
|
|
Cookies.set(TOKEN_COOKIE_NAME, data.token); |
|
|
|
|
window.location.href = redirectUrl; |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let redirectUrl = '/'; |
|
|
|
|
const gitHubRedirectAt = localStorage.getItem(GITHUB_REDIRECT_AT); |
|
|
|
|
const lastPageBeforeGithub = localStorage.getItem(GITHUB_LAST_PAGE); |
|
|
|
|
|
|
|
|
|
// If the social redirect is there and less than 30 seconds old
|
|
|
|
|
// redirect to the page that user was on before they clicked the github login button
|
|
|
|
|
if (gitHubRedirectAt && lastPageBeforeGithub) { |
|
|
|
|
const socialRedirectAtTime = parseInt(gitHubRedirectAt, 10); |
|
|
|
|
const now = Date.now(); |
|
|
|
|
const timeSinceRedirect = now - socialRedirectAtTime; |
|
|
|
|
|
|
|
|
|
if (timeSinceRedirect < 30 * 1000) { |
|
|
|
|
redirectUrl = lastPageBeforeGithub; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
localStorage.removeItem(GITHUB_REDIRECT_AT); |
|
|
|
|
localStorage.removeItem(GITHUB_LAST_PAGE); |
|
|
|
|
Cookies.set(TOKEN_COOKIE_NAME, response.token); |
|
|
|
|
window.location.href = redirectUrl; |
|
|
|
|
}) |
|
|
|
|
.catch((err) => { |
|
|
|
|
setError('Something went wrong. Please try again later.'); |
|
|
|
@ -69,33 +68,30 @@ 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) { |
|
|
|
|
// 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)) { |
|
|
|
|
localStorage.setItem(GITHUB_REDIRECT_AT, Date.now().toString()); |
|
|
|
|
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); |
|
|
|
|
}); |
|
|
|
|
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)) { |
|
|
|
|
localStorage.setItem(GITHUB_REDIRECT_AT, Date.now().toString()); |
|
|
|
|
localStorage.setItem(GITHUB_LAST_PAGE, window.location.pathname); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
window.location.href = response.loginUrl; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|