2
0

(auth) Add custom OAuth provider support

Closes #42
This commit is contained in:
Baptiste Arnaud
2023-01-02 08:29:46 +01:00
parent 60ed0b2d4a
commit b9d38935a6
4 changed files with 67 additions and 1 deletions

View File

@ -105,6 +105,15 @@ export const SocialLoginButtons = ({ providers }: Props) => {
Continue with {providers['azure-ad'].name}
</Button>
)}
{providers?.['custom-oauth'] && (
<Button
onClick={handleAzureAdClick}
isLoading={['loading', 'authenticated'].includes(status)}
variant="outline"
>
Continue with {providers['custom-oauth'].name}
</Button>
)}
</Stack>
)
}

View File

@ -10,7 +10,7 @@ import { Provider } from 'next-auth/providers'
import { NextApiRequest, NextApiResponse } from 'next'
import { CustomAdapter } from './adapter'
import { User } from 'db'
import { env, isNotEmpty } from 'utils'
import { env, getAtPath, isNotEmpty } from 'utils'
import { mockedUser } from '@/features/auth'
const providers: Provider[] = []
@ -97,6 +97,34 @@ if (
)
}
if (isNotEmpty(process.env.CUSTOM_OAUTH_AUTHORIZATION_URL)) {
providers.push({
id: 'custom-oauth',
name: process.env.CUSTOM_OAUTH_NAME ?? 'Custom OAuth',
type: 'oauth',
authorization: process.env.CUSTOM_OAUTH_AUTHORIZATION_URL,
token: process.env.CUSTOM_OAUTH_TOKEN_URL,
userinfo: process.env.CUSTOM_OAUTH_USERINFO_URL,
profile(profile) {
return {
id: getAtPath(profile, process.env.CUSTOM_OAUTH_USER_ID_PATH ?? 'id'),
name: getAtPath(
profile,
process.env.CUSTOM_OAUTH_USER_NAME_PATH ?? 'name'
),
email: getAtPath(
profile,
process.env.CUSTOM_OAUTH_USER_EMAIL_PATH ?? 'email'
),
image: getAtPath(
profile,
process.env.CUSTOM_OAUTH_USER_IMAGE_PATH ?? 'image'
),
} as User
},
})
}
const handler = (req: NextApiRequest, res: NextApiResponse) => {
if (
req.method === 'GET' &&