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

[core] Add more OAuth providers to SignInPage #3933

Merged
merged 25 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
784bded
feat: More OAuth providers
bharatkashyap Aug 14, 2024
36b962d
fix: CI
bharatkashyap Aug 14, 2024
0434458
fix: Ignore icons for api docs
bharatkashyap Aug 16, 2024
4bcde3d
fix: CI, ignore icons for codecov
bharatkashyap Aug 16, 2024
85a0cf9
Merge branch 'master' into oauth-more-providers
bharatkashyap Aug 17, 2024
7fd0443
Merge branch 'master' into oauth-more-providers
bharatkashyap Aug 18, 2024
543443f
Merge branch 'master' into oauth-more-providers
bharatkashyap Aug 18, 2024
3baf5f9
Merge branch 'master' into oauth-more-providers
bharatkashyap Aug 19, 2024
dfce430
Merge branch 'master' of github.com:mui/mui-toolpad into oauth-more-p…
bharatkashyap Aug 22, 2024
6541d92
fix: Better Cognito icon
bharatkashyap Aug 22, 2024
f0634cc
Merge branch 'master' into oauth-more-providers
bharatkashyap Aug 22, 2024
a09e4a4
Merge branch 'master' of github.com:mui/mui-toolpad into oauth-more-p…
bharatkashyap Aug 27, 2024
7e604c5
fix: Add more providers to CLI
bharatkashyap Aug 27, 2024
0a10ee6
fix: Show list in docs, better variable name
bharatkashyap Aug 28, 2024
7fcdc91
Merge branch 'master' of github.com:mui/mui-toolpad into oauth-more-p…
bharatkashyap Aug 30, 2024
2e5e729
Merge branch 'master' of github.com:mui/mui-toolpad into oauth-more-p…
bharatkashyap Sep 2, 2024
8abe45a
Merge branch 'master' into oauth-more-providers
bharatkashyap Sep 3, 2024
a5e4f43
fix: `create-toolpad-app` depends on `@toolpad/core` types
bharatkashyap Sep 3, 2024
58f731b
fix: Clean templates, fix a bunch of issues in generated apps
bharatkashyap Sep 4, 2024
9c4b999
fix: Warn/error on missing config, misc cleanup to examples/playground
bharatkashyap Sep 4, 2024
84f53cf
Merge branch 'fix/create-toolpad-app' into oauth-more-providers
bharatkashyap Sep 5, 2024
5bf58e1
Merge branch 'master' into oauth-more-providers
bharatkashyap Sep 5, 2024
d9135e5
Merge branch 'master' of github.com:mui/mui-toolpad into oauth-more-p…
bharatkashyap Sep 12, 2024
1bcf42d
fix: Agnostic
bharatkashyap Sep 12, 2024
7e61cae
Merge branch 'master' into oauth-more-providers
bharatkashyap Sep 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
coverage:
ignore:
- '*/icons/*'
status:
project:
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const providers = [
{ id: 'github', name: 'GitHub' },
{ id: 'google', name: 'Google' },
{ id: 'facebook', name: 'Facebook' },
{ id: 'twitter', name: 'Twitter' },
{ id: 'linkedin', name: 'LinkedIn' },
];

// preview-end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const providers = [
{ id: 'github', name: 'GitHub' },
{ id: 'google', name: 'Google' },
{ id: 'facebook', name: 'Facebook' },
{ id: 'twitter', name: 'Twitter' },
{ id: 'linkedin', name: 'LinkedIn' },
];
// preview-end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const providers = [
{ id: 'github', name: 'GitHub' },
{ id: 'google', name: 'Google' },
{ id: 'facebook', name: 'Facebook' },
{ id: 'twitter', name: 'Twitter' },
{ id: 'linkedin', name: 'LinkedIn' },
];

// ...
Expand Down
31 changes: 30 additions & 1 deletion docs/data/toolpad/core/components/sign-in-page/sign-in-page.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,36 @@ The `SignInPage` component is a quick way to generate a ready-to-use authenticat

The `SignInPage` component can be set up with an OAuth provider by passing in a list of providers in the `providers` prop, along with a `signIn` function that accepts the `provider` as a parameter.

{{"demo": "OAuthSignInPage.js", "iframe": true}}
{{"demo": "OAuthSignInPage.js", "iframe": true, "height": 500}}

:::info

The following providers are supported and maintained by default:

- Google
- GitHub
- Facebook
- Microsoft (Entra ID)
- Apple
- Auth0
- AWS Cognito
- GitLab
- Instagram
- LINE
- Okta
- FusionAuth
- Twitter
- TikTok
- LinkedIn
- Slack
- Spotify
- Twitch
- Discord
- Keycloak
- Credentials (username/password)

Find details on how to set up each provider in the [Auth.js documentation](https://authjs.dev/getting-started/authentication/oauth/).
:::

## Credentials

Expand Down
5 changes: 4 additions & 1 deletion docs/pages/toolpad/core/api/sign-in-page.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"props": {
"providers": {
"type": { "name": "arrayOf", "description": "Array<{ id: string, name: string }>" },
"type": {
"name": "arrayOf",
"description": "Array&lt;{ id: 'apple'<br>&#124;&nbsp;'auth0'<br>&#124;&nbsp;'cognito'<br>&#124;&nbsp;'credentials'<br>&#124;&nbsp;'discord'<br>&#124;&nbsp;'facebook'<br>&#124;&nbsp;'fusionauth'<br>&#124;&nbsp;'github'<br>&#124;&nbsp;'gitlab'<br>&#124;&nbsp;'google'<br>&#124;&nbsp;'instagram'<br>&#124;&nbsp;'keycloak'<br>&#124;&nbsp;'line'<br>&#124;&nbsp;'linkedin'<br>&#124;&nbsp;'microsoft-entra-id'<br>&#124;&nbsp;'okta'<br>&#124;&nbsp;'slack'<br>&#124;&nbsp;'spotify'<br>&#124;&nbsp;'tiktok'<br>&#124;&nbsp;'twitch'<br>&#124;&nbsp;'twitter', name: string }&gt;"
},
"default": "[]"
},
"signIn": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const NAVIGATION: Navigation = [
];

const BRANDING = {
title: 'My Toolpad Core App',
title: 'My Toolpad Core Next.js Pages next-auth 4 App',
};

const AUTHENTICATION = {
Expand Down
2 changes: 1 addition & 1 deletion examples/core-auth-nextjs-pages/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const NAVIGATION: Navigation = [
];

const BRANDING = {
title: 'My Toolpad Core App',
title: 'My Toolpad Core Next.js Pages App',
};

const AUTHENTICATION = {
Expand Down
2 changes: 1 addition & 1 deletion examples/core-auth-nextjs/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const NAVIGATION: Navigation = [
];

const BRANDING = {
title: 'My Toolpad Core App',
title: 'My Toolpad Core Next.js App',
};

const AUTHENTICATION = {
Expand Down
1 change: 1 addition & 0 deletions packages/create-toolpad-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"dependencies": {
"@inquirer/prompts": "^5.5.0",
"@toolpad/utils": "workspace:*",
"@toolpad/core": "workspace:*",
"chalk": "5.3.0",
"execa": "9.3.1",
"invariant": "2.2.4",
Expand Down
26 changes: 20 additions & 6 deletions packages/create-toolpad-app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@ import { satisfies } from 'semver';
import { readJsonFile } from '@toolpad/utils/fs';
import invariant from 'invariant';
import { bashResolvePath } from '@toolpad/utils/cli';
import type { SupportedAuthProvider } from '@toolpad/core';
import generateProject from './generateProject';
import generateStudioProject from './generateStudioProject';
import writeFiles from './writeFiles';
import { downloadAndExtractExample } from './examples';
import type { PackageJson } from './templates/packageType';
import type {
SupportedRouter,
SupportedAuthProvider,
PackageManager,
GenerateProjectOptions,
} from './types';
import type { SupportedRouter, PackageManager, GenerateProjectOptions } from './types';

/**
* Find package.json of the create-toolpad-app package
Expand Down Expand Up @@ -308,6 +304,24 @@ const run = async () => {
choices: [
{ name: 'Google', value: 'google' },
{ name: 'GitHub', value: 'github' },
{ name: 'GitLab', value: 'gitlab' },
{ name: 'Twitter', value: 'twitter' },
{ name: 'Facebook', value: 'facebook' },
{ name: 'Cognito', value: 'cognito' },
{ name: 'Microsoft Entra ID', value: 'microsoft-entra-id' },
{ name: 'Apple', value: 'apple' },
{ name: 'Instagram', value: 'instagram' },
{ name: 'TikTok', value: 'tiktok' },
{ name: 'LinkedIn', value: 'linkedin' },
{ name: 'Slack', value: 'slack' },
{ name: 'Spotify', value: 'spotify' },
{ name: 'Twitch', value: 'twitch' },
{ name: 'Discord', value: 'discord' },
{ name: 'Line', value: 'line' },
{ name: 'Auth0', value: 'auth0' },
{ name: 'Keycloak', value: 'keycloak' },
{ name: 'Okta', value: 'okta' },
{ name: 'FusionAuth', value: 'fusionauth' },
],
});
}
Expand Down
20 changes: 11 additions & 9 deletions packages/create-toolpad-app/src/templates/auth/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { kebabToConstant, capitalize } from '@toolpad/utils/strings';
import { Template, SupportedAuthProvider } from '../../types';
import type { SupportedAuthProvider } from '@toolpad/core';
import { kebabToConstant, kebabToPascal } from '@toolpad/utils/strings';
import { requiresIssuer, requiresTenantId } from './utils';
import { Template } from '../../types';

const CredentialsProviderTemplate = `Credentials({
credentials: {
Expand All @@ -18,6 +20,12 @@ const CredentialsProviderTemplate = `Credentials({
},
}),`;

const oAuthProviderTemplate = (provider: SupportedAuthProvider) => `${kebabToPascal(provider)}({
clientId: process.env.${kebabToConstant(provider)}_CLIENT_ID,
clientSecret: process.env.${kebabToConstant(provider)}_CLIENT_SECRET,
${requiresIssuer(provider) ? `issuer: process.env.${kebabToConstant(provider)}_ISSUER,\n` : ''}${requiresTenantId(provider) ? `tenantId: process.env.${kebabToConstant(provider)}_TENANT_ID,` : ''}
})`;

const checkEnvironmentVariables = (providers: SupportedAuthProvider[] | undefined) => `

const missingVars: string[] = [];
Expand Down Expand Up @@ -47,20 +55,14 @@ if (missingVars.length > 0) {
}
}`;

const oAuthProviderTemplate = (provider: SupportedAuthProvider) => `
${capitalize(provider)}({
clientId: process.env.${kebabToConstant(provider)}_CLIENT_ID,
clientSecret: process.env.${kebabToConstant(provider)}_CLIENT_SECRET,
}),`;

const auth: Template = (options) => {
const providers = options.authProviders;

return `import NextAuth from 'next-auth';
${providers
?.map(
(provider) =>
`import ${capitalize(provider)} from 'next-auth/providers/${provider.toLowerCase()}';`,
`import ${kebabToPascal(provider)} from 'next-auth/providers/${provider.toLowerCase()}';`,
)
.join('\n')}
import type { Provider } from 'next-auth/providers';
Expand Down
12 changes: 7 additions & 5 deletions packages/create-toolpad-app/src/templates/auth/envLocal.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { kebabToConstant } from '@toolpad/utils/strings';
import { requiresIssuer, requiresTenantId } from './utils';
import { Template } from '../../types';

const env: Template = (options) => {
const { authProviders: providers } = options;
const oAuthProviders = providers?.filter((provider) => provider !== 'credentials');
const nonCredentialProviders = providers?.filter((provider) => provider !== 'credentials');
return `
# Generate a secret with \`npx auth secret\`
# and replace the value below with it
AUTH_SECRET=secret

# Add the CLIENT_ID and CLIENT_SECRET from your OAuth provider
# to the .env.local file
${oAuthProviders
# Add secrets for your auth providers to the .env.local file

${nonCredentialProviders
?.map(
(provider) => `
${kebabToConstant(provider)}_CLIENT_ID=
${kebabToConstant(provider)}_CLIENT_SECRET=`,
${kebabToConstant(provider)}_CLIENT_SECRET=
${requiresIssuer(provider) ? `${kebabToConstant(provider)}_ISSUER=\n` : ''}${requiresTenantId(provider) ? `${kebabToConstant(provider)}_TENANT_ID=\n` : ''}`,
)
.join('\n')}
`;
Expand Down
14 changes: 14 additions & 0 deletions packages/create-toolpad-app/src/templates/auth/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { SupportedAuthProvider } from '@toolpad/core';

export function requiresIssuer(provider: SupportedAuthProvider) {
return (
provider === 'cognito' ||
provider === 'fusionauth' ||
provider === 'keycloak' ||
provider === 'okta'
);
}

export function requiresTenantId(provider: SupportedAuthProvider) {
return provider === 'microsoft-entra-id' || provider === 'fusionauth';
}
8 changes: 2 additions & 6 deletions packages/create-toolpad-app/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { PackageJson } from './templates/packageType';

// https://authjs.dev/reference/core/providers#oauthconfigprofile
type SupportedOAuthProvider = 'facebook' | 'github' | 'google';
import type { SupportedAuthProvider } from '@toolpad/core';

// https://authjs.dev/reference/core/providers#providertype
export type SupportedAuthProvider = SupportedOAuthProvider | 'credentials';
import { PackageJson } from './templates/packageType';

export type SupportedRouter = 'nextjs-app' | 'nextjs-pages';
export type PackageManager = 'npm' | 'pnpm' | 'yarn';
Expand Down
2 changes: 1 addition & 1 deletion packages/toolpad-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"build:types": "tsc -b tsconfig.build.json",
"predev": "pnpm clean",
"dev": "concurrently \"pnpm build:stable --watch\" \"pnpm build:types --watch --preserveWatchOutput\"",
"check-types": "tsc --noEmit",
"check-types": "pnpm build:types && tsc --noEmit",
"test": "vitest run --coverage",
"test:dev": "vitest",
"test:browser": "vitest run --browser.enabled",
Expand Down
Loading
Loading