Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GitHub provider #578

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
4 changes: 4 additions & 0 deletions explorer/next-auth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ declare module 'next-auth' {
DIDs: string[]
subspace?: SubspaceToken
discord?: DiscordToken
github?: GitHubToken
}

export interface Session {
Expand All @@ -31,6 +32,7 @@ declare module 'next-auth/client' {
DIDs: string[]
subspace?: SubspaceToken
discord?: DiscordToken
github?: GitHubToken
}

export interface Session {
Expand All @@ -46,12 +48,14 @@ declare module 'next-auth/jwt' {
DIDs: string[]
subspace?: SubspaceToken
discord?: DiscordToken
github?: GitHubToken
}

interface JWT {
id: string
DIDs: string[]
subspace?: SubspaceToken
discord?: DiscordToken
github?: GitHubToken
}
}
31 changes: 31 additions & 0 deletions explorer/src/components/WalletSideKick/Actions/ConnectGitHub.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { StyledListItem } from 'components/common/List'
import { StyledButton } from 'components/common/StyledButton'
import { CheckMarkIcon } from 'components/icons/CheckMarkIcon'
import { signIn, useSession } from 'next-auth/react'
import { FC, useCallback } from 'react'

export const ConnectGitHub: FC = () => {
const { data: session } = useSession()

const handleConnectGitHub = useCallback(
async () => await signIn('github', { redirect: false }),
[],
)

if (!session || !session.user) return null

return (
<StyledListItem title='Connect your GitHub account!'>
{session?.user?.github?.vcs ? (
<>
<CheckMarkIcon />
<StyledButton className='ml-2' onClick={handleConnectGitHub}>
Refresh
</StyledButton>
</>
) : (
<StyledButton onClick={handleConnectGitHub}>Connect</StyledButton>
)}
</StyledListItem>
)
}
3 changes: 3 additions & 0 deletions explorer/src/components/WalletSideKick/GetDiscordRoles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Link from 'next/link'
import { FC, useMemo, useState } from 'react'
// import { ClaimStakingToken } from './Actions/ClaimStakingToken'
import { ConnectDiscord } from './Actions/ConnectDiscord'
import { ConnectGitHub } from './Actions/ConnectGitHub'
import { JoinDiscord } from './Actions/JoinDiscord'
import { VerifyWalletOwnership } from './Actions/VerifyWalletOwnership'

Expand Down Expand Up @@ -85,6 +86,7 @@ export const GetDiscordRoles: FC = () => {
)}
<VerifyWalletOwnership />
<ConnectDiscord />
<ConnectGitHub />
</List>
{/* <ClaimStakingToken /> */}
</Accordion>
Expand All @@ -99,6 +101,7 @@ export const GetDiscordRoles: FC = () => {
<VerifyWalletOwnership />
<JoinDiscord />
<ConnectDiscord />
<ConnectGitHub />
</List>
</Accordion>
<ExplainerLinkAndModal />
Expand Down
7 changes: 6 additions & 1 deletion explorer/src/constants/session.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DiscordToken, SubspaceToken } from 'types/jwt'
import type { DiscordToken, GitHubToken, SubspaceToken } from 'types/jwt'

export const TOKEN_EXPIRATION = 60 * 60 * 24 // 1 day

Expand Down Expand Up @@ -34,3 +34,8 @@ export const DEFAULT_DISCORD_TOKEN: DiscordToken = {
},
},
}

export const DEFAULT_GITHUB_TOKEN: GitHubToken = {
id: '',
username: '',
}
5 changes: 5 additions & 0 deletions explorer/src/types/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ export type DiscordToken = {
}
}
}

export type GitHubToken = {
id: string
username: string
}
1 change: 1 addition & 0 deletions explorer/src/utils/auth/authOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const authOptions: AuthOptions = {
token.DIDs = user.DIDs
token.subspace = user.subspace
token.discord = user.discord
token.github = user.github
}
return token
},
Expand Down
3 changes: 2 additions & 1 deletion explorer/src/utils/auth/providers/discord.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AuthProvider } from 'constants/session'
import { AuthProvider, DEFAULT_GITHUB_TOKEN } from 'constants/session'
import type { TokenSet } from 'next-auth'
import { User } from 'next-auth'
import type { DiscordProfile } from 'next-auth/providers/discord'
Expand Down Expand Up @@ -82,6 +82,7 @@ export const Discord = () => {
},
},
},
github: session.github || DEFAULT_GITHUB_TOKEN,
}

if (!savedUser || savedUser.length === 0) {
Expand Down
46 changes: 46 additions & 0 deletions explorer/src/utils/auth/providers/github.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import * as jsonwebtoken from 'jsonwebtoken'
import type { TokenSet } from 'next-auth'
import { JWT } from 'next-auth/jwt'
import GitHubProvider, { GithubProfile } from 'next-auth/providers/github'
import { cookies } from 'next/headers'

const { GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET } = process.env

export const GitHub = () => {
return GitHubProvider({
// client credentials
clientId: GITHUB_CLIENT_ID || '',
clientSecret: GITHUB_CLIENT_SECRET || '',

// fetch discord profile
profile: async (profile: GithubProfile, token: TokenSet) => {
try {
if (!token.access_token) throw new Error('No access token')

if (!process.env.NEXTAUTH_SECRET) throw new Error('No secret')
const { NEXTAUTH_SECRET } = process.env

const { get } = cookies()
const sessionToken = get('next-auth.session-token')?.value || ''
const session = jsonwebtoken.verify(sessionToken, NEXTAUTH_SECRET, {
algorithms: ['HS256'],
}) as JWT
const did = 'did:openid:github:' + profile.id

return {
id: session.id || did,
DIDs: [...session.DIDs, did],
subspace: session.subspace,
discord: session.discord,
github: {
id: profile.id,
username: profile.login,
},
}
} catch (error) {
console.error('Error fetching Discord profile:', error)
throw new Error('Failed to fetch Discord profile')
}
},
})
}
3 changes: 2 additions & 1 deletion explorer/src/utils/auth/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Provider } from 'next-auth/providers'
import { Discord } from './discord'
import { GitHub } from './github'
import { Nova } from './nova'
import { Subspace } from './subspace'

export const providers: Provider[] = [Discord(), Subspace(), Nova()]
export const providers: Provider[] = [Discord(), GitHub(), Subspace(), Nova()]
5 changes: 3 additions & 2 deletions explorer/src/utils/auth/providers/subspace.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cryptoWaitReady, signatureVerify } from '@autonomys/auto-utils'
import { AuthProvider, DEFAULT_DISCORD_TOKEN } from 'constants/session'
import { cryptoWaitReady, signatureVerify } from '@polkadot/util-crypto'
import { AuthProvider, DEFAULT_DISCORD_TOKEN, DEFAULT_GITHUB_TOKEN } from 'constants/session'
import { User } from 'next-auth'
import type { Provider } from 'next-auth/providers'
import CredentialsProvider from 'next-auth/providers/credentials'
Expand Down Expand Up @@ -55,6 +55,7 @@ export const Subspace = () => {
},
},
discord: DEFAULT_DISCORD_TOKEN,
github: DEFAULT_GITHUB_TOKEN,
}

if (!savedUser || savedUser.length === 0) {
Expand Down
Loading