Skip to content

Commit

Permalink
#26 - run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianscatularo committed Mar 21, 2024
1 parent 95f8d72 commit 302d9a0
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 50 deletions.
21 changes: 8 additions & 13 deletions frontend/components/DiscordButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ type DiscordButtonProps = {
}

export function DiscordButton({ disableOnAuth }: DiscordButtonProps) {
const { authenticate, clear, isConnecting, isAuthenticated, profile } = useDiscordAuth();
const { authenticate, clear, isConnecting, isAuthenticated, profile } =
useDiscordAuth()

const { logo, text } = useMemo(() => {
if (isAuthenticated)
return {
logo: profile?.image ? (
<Image
src={profile?.image}
alt="user image"
width={20}
height={20}
/>
<Image src={profile?.image} alt="user image" width={20} height={20} />
) : (
<Discord />
),
Expand All @@ -36,13 +32,12 @@ export function DiscordButton({ disableOnAuth }: DiscordButtonProps) {
<button
className={'wbtn wbtn-secondary min-w-[117px] sm:min-w-[207px]'}
onClick={() => {
if(isAuthenticated) {
clear()
} else {
authenticate()
}
if (isAuthenticated) {
clear()
} else {
authenticate()
}
}
}}
disabled={disableOnAuth}
>
<span className="relative inline-flex items-center gap-1 whitespace-nowrap sm:gap-2.5">
Expand Down
12 changes: 10 additions & 2 deletions frontend/hooks/useDiscordAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export type DiscordProfile = {
image: string
}

function watchStorageForChange(setToken: (token: string) => void, setIsConnecting: (isConnecting: boolean) => void) {
function watchStorageForChange(
setToken: (token: string) => void,
setIsConnecting: (isConnecting: boolean) => void
) {
function onStorageChange(event: StorageEvent) {
if (event.key === DISCORD_TOKEN_KEY && event.newValue) {
setToken(event.newValue)
Expand Down Expand Up @@ -125,7 +128,12 @@ export default function useDiscordAuth() {
const getDiscordUserInfo = useCallback(() => {
const controller = new AbortController()
if (token && isAuthenticated) {
getDiscordUserInfoAndSetProfile(controller.signal, token, setProfile, setIsConnecting)
getDiscordUserInfoAndSetProfile(
controller.signal,
token,
setProfile,
setIsConnecting
)
}
return () => {
controller.abort()
Expand Down
2 changes: 1 addition & 1 deletion frontend/hooks/useGetEcosystemIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function useGetEcosystemIdentity() {
// TODO update logic to get discord data from lambda function execution
// const { data } = useSession()
const data = {} as any
const { profile } = useDiscordAuth();
const { profile } = useDiscordAuth()

return useCallback(
(ecosystem: Ecosystem) => {
Expand Down
70 changes: 36 additions & 34 deletions frontend/pages/auth/discord.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect } from "react";
import { useRouter } from "next/navigation";
import { useEffect } from 'react'
import { useRouter } from 'next/navigation'

export const DISCORD_OAUTH = {
url: '/auth/discord',
Expand All @@ -10,45 +10,47 @@ export const DISCORD_OAUTH = {
// http://localhost:3000/discord#token_type=Bearer&access_token=<access-token>&expires_in=604800&scope=identify
// https://discord.com/oauth2/authorize?client_id=<client-id>&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fdiscord&scope=identify
type DiscordResponse = {
token: string;
error: string;
expires: string;
token: string
error: string
expires: string
}

const DISCORD_CALLBACK_URL = process.env.NEXT_PUBLIC_DISCORD_CALLBACK_URL;
const DISCORD_CALLBACK_URL = process.env.NEXT_PUBLIC_DISCORD_CALLBACK_URL

function getCodeFromUrl(): DiscordResponse {
const href = window.location.href;
const urlString = href.replace(`${DISCORD_CALLBACK_URL}#`, `${DISCORD_CALLBACK_URL}?`);
const url = new URL(urlString);
const searchParams = url.searchParams;
return {
token: searchParams.get('access_token') || '',
error: searchParams.get('error_description') || 'N/A',
expires: searchParams.get('expires_in') || '0'
}
const href = window.location.href
const urlString = href.replace(
`${DISCORD_CALLBACK_URL}#`,
`${DISCORD_CALLBACK_URL}?`
)
const url = new URL(urlString)
const searchParams = url.searchParams
return {
token: searchParams.get('access_token') || '',
error: searchParams.get('error_description') || 'N/A',
expires: searchParams.get('expires_in') || '0',
}
}

function expiresAsDate(expires: string): string {
try {
const expiresInSeconds = parseInt(expires);
const now = new Date();
now.setSeconds(now.getSeconds() + expiresInSeconds);
return now.toISOString();
} catch (error) {
return new Date().toISOString();
}
try {
const expiresInSeconds = parseInt(expires)
const now = new Date()
now.setSeconds(now.getSeconds() + expiresInSeconds)
return now.toISOString()
} catch (error) {
return new Date().toISOString()
}
}

export default function Discord() {
const router = useRouter();
useEffect(() => {
const { token = '', error = 'N/A', expires = '0' } = getCodeFromUrl();
localStorage.setItem('discord.token', token);
localStorage.setItem('discord.error', error);
localStorage.setItem('discord.expires', expiresAsDate(expires));
window.close();
}, [router]);
return (<button onClick={() => window.close()}>Close Window</button>);
};

const router = useRouter()
useEffect(() => {
const { token = '', error = 'N/A', expires = '0' } = getCodeFromUrl()
localStorage.setItem('discord.token', token)
localStorage.setItem('discord.error', error)
localStorage.setItem('discord.expires', expiresAsDate(expires))
window.close()
}, [router])
return <button onClick={() => window.close()}>Close Window</button>
}

0 comments on commit 302d9a0

Please sign in to comment.