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

✨ [Feat] Extend Authentication System ( username/password, more oAuth options, ability to extend? ) #67

Merged
merged 14 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 15 additions & 11 deletions packages/studioCMS/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,21 @@
"@astrojs/rss": "^4.0.5",
"@cloudinary/url-gen": "^1.19.0",
"@iconify-json/mdi": "^1.1.66",
"@iconify/utils": "^2.1.23",
"@inox-tools/sitemap-ext": "^0.2.11",
"@lucia-auth/adapter-drizzle": "^1.0.7",
"@markdoc/markdoc": "^0.4.0",
"@shikijs/transformers": "^1.3.0",
"@unocss/astro": "0.59.4",
"@unocss/reset": "0.59.4",
"@unocss/preset-uno": "0.59.4",
"@unocss/preset-wind": "0.59.4",
"@unocss/preset-typography": "0.59.4",
"@unocss/transformer-directives": "0.59.4",
"@shikijs/transformers": "^1.4.0",
"@unocss/astro": "^0.59.4",
"@unocss/reset": "^0.59.4",
"@unocss/preset-uno": "^0.59.4",
"@unocss/preset-wind": "^0.59.4",
"@unocss/preset-typography": "^0.59.4",
"@unocss/preset-web-fonts": "^0.59.4",
"@unocss/transformer-directives": "^0.59.4",
"@unpic/astro": "^0.0.46",
"@yangyang20240403/unocss-preset-daisyui": "^1.0.0",
"arctic": "^1.8.0",
"arctic": "^1.8.1",
"astro-integration-kit": "^0.13.2",
"daisyui": "^4.4.23",
"lucia": "^3.2.0",
Expand All @@ -64,13 +66,15 @@
"marked-footnote": "^1.2.2",
"marked-shiki": "^1.1.0",
"marked-smartypants": "^1.1.6",
"shiki": "^1.3.0",
"oslo": "^1.2.0",
"shiki": "^1.4.0",
"typescript": "^5.4.5",
"unpic": "^3.18.0",
"unocss": "^0.59.4",
"vite": "^5.2.10",
"unocss-preset-scrollbar": "^0.3.1",
"vite": "^5.2.11",
"pathe": "^1.1.2",
"recast": "^0.23.5"
"recast": "^0.23.6"
},
"peerDependencies": {
"@astrojs/db": ">=0.11.0",
Expand Down
6 changes: 5 additions & 1 deletion packages/studioCMS/src/db/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ export const User = defineTable({
name: column.text(),
email: column.text({ unique: true, optional: true }),
avatar: column.text({ optional: true }),
githubId: column.number({ unique: true }),
githubId: column.number({ unique: true, optional: true, nullable: true }),
githubURL: column.text({ optional: true }),
discordId: column.text({ unique: true, optional: true, nullable: true }),
googleId: column.text({ unique: true, optional: true, nullable: true }),
auth0Id: column.text({ unique: true, optional: true, nullable: true }),
username: column.text(),
password: column.text({ optional: true }),
updatedAt: column.date({ default: NOW, nullable: true }),
createdAt: column.date({ default: NOW, nullable: true }),
},
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
205 changes: 185 additions & 20 deletions packages/studioCMS/src/integrations/studioCMSDashboard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { CheckENV, integrationLogger } from "../../utils";
import { loadEnv } from "vite";
import { fileFactory } from "../../utils/fileFactory";
import { DashboardStrings } from "../../strings";
import { presetTypography, presetWind, presetUno, transformerDirectives, presetIcons } from "unocss";
import { presetTypography, presetWind, presetUno, transformerDirectives, presetIcons, presetWebFonts } from "unocss";
import UnoCSSAstroIntegration from "@unocss/astro";
import { presetDaisy } from "@yangyang20240403/unocss-preset-daisyui";
import { FileSystemIconLoader } from '@iconify/utils/lib/loader/node-loaders'
import { presetScrollbar } from 'unocss-preset-scrollbar'

// Environment Variables
const env = loadEnv('all', process.cwd(), 'CMS');
Expand All @@ -25,7 +27,77 @@ const AUTHKEYS = {
env.CMS_GITHUB_CLIENT_SECRET ||
import.meta.env.CMS_GITHUB_CLIENT_SECRET ||
process.env.CMS_GITHUB_CLIENT_SECRET,
}
},
DISCORDCLIENTID: {
N: 'CMS_DISCORD_CLIENT_ID',
KEY:
env.CMS_DISCORD_CLIENT_ID ||
import.meta.env.CMS_DISCORD_CLIENT_ID ||
process.env.CMS_DISCORD_CLIENT_ID,
},
DISCORDCLIENTSECRET: {
N: 'CMS_DISCORD_CLIENT_SECRET',
KEY:
env.CMS_DISCORD_CLIENT_SECRET ||
import.meta.env.CMS_DISCORD_CLIENT_SECRET ||
process.env.CMS_DISCORD_CLIENT_SECRET,
},
DISCORDREDIRECTURI: {
N: 'CMS_DISCORD_REDIRECT_URL',
KEY:
env.CMS_DISCORD_REDIRECT_URI ||
import.meta.env.CMS_DISCORD_REDIRECT_URI ||
process.env.CMS_DISCORD_REDIRECT_URI,
},
GOOGLECLIENTID: {
N: 'CMS_GOOGLE_CLIENT_ID',
KEY:
env.CMS_GOOGLE_CLIENT_ID ||
import.meta.env.CMS_GOOGLE_CLIENT_ID ||
process.env.CMS_GOOGLE_CLIENT_ID,
},
GOOGLECLIENTSECRET: {
N: 'CMS_GOOGLE_CLIENT_SECRET',
KEY:
env.CMS_GOOGLE_CLIENT_SECRET ||
import.meta.env.CMS_GOOGLE_CLIENT_SECRET ||
process.env.CMS_GOOGLE_CLIENT_SECRET,
},
GOOGLEREDIRECTURI: {
N: 'CMS_GOOGLE_REDIRECT_URL',
KEY:
env.CMS_GOOGLE_REDIRECT_URI ||
import.meta.env.CMS_GOOGLE_REDIRECT_URI ||
process.env.CMS_GOOGLE_REDIRECT_URI,
},
AUTH0CLIENTID: {
N: 'CMS_AUTH0_CLIENT_ID',
KEY:
env.CMS_AUTH0_CLIENT_ID ||
import.meta.env.CMS_AUTH0_CLIENT_ID ||
process.env.CMS_AUTH0_CLIENT_ID,
},
AUTH0CLIENTSECRET: {
N: 'CMS_AUTH0_CLIENT_SECRET',
KEY:
env.CMS_AUTH0_CLIENT_SECRET ||
import.meta.env.CMS_AUTH0_CLIENT_SECRET ||
process.env.CMS_AUTH0_CLIENT_SECRET,
},
AUTH0DOMAIN: {
N: 'CMS_AUTH0_DOMAIN',
KEY:
env.CMS_AUTH0_DOMAIN ||
import.meta.env.CMS_AUTH0_DOMAIN ||
process.env.CMS_AUTH0_DOMAIN,
},
AUTH0REDIRECTURI: {
N: 'CMS_AUTH0_REDIRECT_URL',
KEY:
env.CMS_AUTH0_REDIRECT_URI ||
import.meta.env.CMS_AUTH0_REDIRECT_URI ||
process.env.CMS_AUTH0_REDIRECT_URI,
},
};

export default defineIntegration({
Expand All @@ -50,9 +122,27 @@ export default defineIntegration({
dashboardEnabled,
AuthConfig,
dashboardRouteOverride,
UnoCSSConfigOverride,
UnoCSSConfigOverride: {
injectEntry,
injectReset,
presetsConfig: {
presetDaisyUI: {
themes,
darkTheme
}
},
},
AuthConfig: {
providers,
providers: {
usernameAndPasswordConfig: {
allowUserRegistration
},
github,
discord,
google,
auth0,
usernameAndPassword
}
},
developerConfig: {
testingAndDemoMode
Expand All @@ -69,26 +159,39 @@ export default defineIntegration({
const { resolve } = createResolver(import.meta.url);

// Virtual Resolver
const virtResolver = { Auth: resolve('./lib/auth.ts'), };
const virtualResolver = {
Auth: resolve('./lib/auth.ts'),
AuthENVChecker: resolve("./utils/authEnvCheck.ts"),
DashboardLayout: resolve('./routes/dashboard/layouts/Layout.astro'),
};

// Virtual Components
const virtualComponentMap = `
export * from '${virtResolver.Auth}';`;
export * from '${virtualResolver.Auth}';
export * from '${virtualResolver.AuthENVChecker}';`;
const VirtualAstroComponents = `
export {default as Layout} from '${virtualResolver.DashboardLayout}';`;

// Add Virtual Imports
integrationLogger(logger, verbose, 'info', 'Adding Virtual Imports...');
addVirtualImports(params, {
name,
imports: {
'studiocms-dashboard:auth': virtualComponentMap,
'studiocms-dashboard:components': VirtualAstroComponents,
},
});

// Create Virtual DTS File
const studioCMSDTS = fileFactory();

studioCMSDTS.addLines(`declare module 'studiocms-dashboard:auth' {
export const lucia: typeof import('${virtResolver.Auth}').lucia;
export const lucia: typeof import('${virtualResolver.Auth}').lucia;
export const authEnvCheck: typeof import('${virtualResolver.AuthENVChecker}').authEnvCheck;
}`);

studioCMSDTS.addLines(`declare module 'studiocms-dashboard:components' {
export const Layout: typeof import('${virtualResolver.DashboardLayout}').default;
}`);

// Add Virtual DTS File
Expand Down Expand Up @@ -129,17 +232,6 @@ export default defineIntegration({
// Add Dashboard Integrations
integrationLogger(logger, verbose, 'info', 'Adding Dashboard Integrations');

const {
injectEntry,
injectReset,
presetsConfig: {
presetDaisyUI: {
themes,
darkTheme
}
}
} = UnoCSSConfigOverride;

// CSS Management
addIntegration(params, {
integration: UnoCSSAstroIntegration({
Expand All @@ -157,8 +249,22 @@ export default defineIntegration({
presetIcons({
collections: {
mdi: () => import('@iconify-json/mdi/icons.json').then(i => i.default),
google: FileSystemIconLoader(resolve('./icons/google')),
discord: FileSystemIconLoader(resolve('./icons/discord')),
github: FileSystemIconLoader(resolve('./icons/github')),
auth0: FileSystemIconLoader(resolve('./icons/auth0')),
}
}),
presetScrollbar({
}),
presetWebFonts({
provider: 'google',
fonts: {
// Required Fonts for Google Icons
sans: 'Roboto',
mono: ['Fira Code', 'Fira Mono:400,700'],
},
}),
],
transformers: [
transformerDirectives()
Expand Down Expand Up @@ -246,7 +352,7 @@ export default defineIntegration({
})

// GitHub Auth Provider
if ( providers.github ) {
if ( github ) {
// Log that the GitHub Auth Provider is enabled
integrationLogger(logger, verbose, 'info', 'GitHub Auth Provider is Enabled');
injectRoute({
Expand All @@ -262,8 +368,67 @@ export default defineIntegration({
integrationLogger(logger, verbose, 'info', 'GitHub Auth Provider is Disabled');
}

// Discord Auth Provider
if (discord){
// Log that the Discord Auth Provider is enabled
integrationLogger(logger, verbose, 'info', 'Discord Auth Provider is Enabled');
injectRoute({
pattern: makeRoute('login/discord'),
entrypoint: resolve('./routes/authroutes/login/discord/index.ts'),
});
injectRoute({
pattern: makeRoute('login/discord/callback'),
entrypoint: resolve('./routes/authroutes/login/discord/callback.ts'),
});
}

// Google Auth Provider
if (google){
// Log that the Google Auth Provider is enabled
integrationLogger(logger, verbose, 'info', 'Google Auth Provider is Enabled');
injectRoute({
pattern: makeRoute('login/google'),
entrypoint: resolve('./routes/authroutes/login/google/index.ts'),
});
injectRoute({
pattern: makeRoute('login/google/callback'),
entrypoint: resolve('./routes/authroutes/login/google/callback.ts'),
});
}

// Auth0 Auth Provider
if (auth0){
// Log that the Auth0 Auth Provider is enabled
integrationLogger(logger, verbose, 'info', 'Auth0 Auth Provider is Enabled');
injectRoute({
pattern: makeRoute('login/auth0'),
entrypoint: resolve('./routes/authroutes/login/auth0/index.ts'),
});
injectRoute({
pattern: makeRoute('login/auth0/callback'),
entrypoint: resolve('./routes/authroutes/login/auth0/callback.ts'),
});
}

// Username and Password Auth Provider
// **NOT YET IMPLEMENTED**
if ( usernameAndPassword) {
// Log that the Username and Password Auth Provider is enabled
integrationLogger(logger, verbose, 'info', 'Username and Password Auth Provider is Enabled');
injectRoute({
pattern: makeRoute('login/api/login'),
entrypoint: resolve('./routes/authroutes/login/api/login.ts'),
})
if ( allowUserRegistration ) {
injectRoute({
pattern: makeRoute('signup/'),
entrypoint: resolve('./routes/authroutes/login/signup.astro'),
})
injectRoute({
pattern: makeRoute('login/api/register'),
entrypoint: resolve('./routes/authroutes/login/api/register.ts'),
})
}
}

} else if ( !AuthConfig.enabled ) {
// Log that the Auth is disabled
Expand Down
Loading