Skip to content

Commit

Permalink
Add low-level auth lib + nextjs integration (#750)
Browse files Browse the repository at this point in the history
* Add low-level auth lib

* Add nextjs app router integration + example (wip)

* Update route handlers to take params object + add missing apis + fix some bugs

* Refactor and add initial next pages integration

* Add basic readme + mark version as alpha

* Drop nextjs example

* Fix lint

* Split into two packages

* Update readme's

* Fix readme code example

* Address review feedback

* Build workspaces in the right order

* Update `getProvidersInfo` query

* Replace emailpassword provider magic string with generated const
  • Loading branch information
jaclarke authored Nov 8, 2023
1 parent dc76cb6 commit 32877ff
Show file tree
Hide file tree
Showing 22 changed files with 1,599 additions and 4 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ jobs:
- name: Build
run: |
yarn workspaces run build
yarn workspace edgedb build
yarn workspace @edgedb/auth-core build
yarn workspace @edgedb/auth-nextjs build
yarn workspace @edgedb/generate build
# - name: Compile for Deno
# run: |
Expand Down
47 changes: 47 additions & 0 deletions packages/auth-core/genConsts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const fs = require("node:fs/promises");
const { createClient } = require("edgedb");

const client = createClient("_localdev");

(async function () {
const providerNames = await client.queryRequiredSingle(`
with
oauth_providers := (
select schema::ObjectType
filter .bases.name = 'ext::auth::OAuthProviderConfig'
),
emailpassword_provider := (
select schema::ObjectType
filter .name = 'ext::auth::EmailPasswordProviderConfig'
)
select {
oauth := (
select oauth_providers.properties filter .name = 'name'
).default,
emailpassword := assert_single((
select emailpassword_provider.properties filter .name = 'name'
).default)
}`);

await fs.writeFile(
"./src/consts.ts",
`// AUTOGENERATED - Run \`yarn gen-consts\` to re-generate.
export const builtinOAuthProviderNames = [
${providerNames.oauth
.sort()
.map((provider) => ` ${provider.replace(/^'|'$/g, '"')},`)
.join("\n")}
] as const;
export type BuiltinOAuthProviderNames =
(typeof builtinOAuthProviderNames)[number];
export const emailPasswordProviderName = ${providerNames.emailpassword.replace(
/^'|'$/g,
'"'
)};
`
);

console.log('Generated into "src/consts.ts"');
})();
7 changes: 7 additions & 0 deletions packages/auth-core/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
testPathIgnorePatterns: ["./dist"],
globalSetup: "./test/globalSetup.ts",
globalTeardown: "./test/globalTeardown.ts",
};
37 changes: 37 additions & 0 deletions packages/auth-core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@edgedb/auth-core",
"description": "Core helper library for the EdgeDB Auth extension",
"version": "0.1.0-alpha.1",
"author": "EdgeDB <[email protected]>",
"repository": {
"type": "git",
"url": "https://github.com/edgedb/edgedb-js.git",
"directory": "packages/auth-core"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"license": "Apache-2.0",
"sideEffects": false,
"files": [
"/dist"
],
"scripts": {
"test": "jest --detectOpenHandles",
"build": "tsc --project tsconfig.json",
"gen-consts": "node genConsts.js"
},
"devDependencies": {
"@types/node": "^20.8.4",
"edgedb": "^1.3.6",
"typescript": "5.0.4",
"@types/jest": "^29.5.2",
"jest": "29.5.0",
"ts-jest": "29.1.0"
},
"peerDependencies": {
"edgedb": "^1.3.6"
},
"dependencies": {
"jwt-decode": "^3.1.2"
}
}
10 changes: 10 additions & 0 deletions packages/auth-core/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# @edgedb/auth-core: Core helper library for the EdgeDB Auth extension

> Warning: This library is still in an alpha state, and so, bugs are likely and the api's should be considered unstable and may change in future releases.
This library contains some low-level utilities to help working with the EdgeDB auth extension; namely resolving the api endpoint urls from an edgedb `Client` object, providing wrappers around those api's to help handle the various auth flows (including handling PKCE), and adding typesafety.

It is recommended to instead use the separate helper libraries created to make integration with popular frameworks as easy as possible. Currently supported frameworks:

- Next.js (@edgedb/auth-nextjs)
- _...more coming soon_
12 changes: 12 additions & 0 deletions packages/auth-core/src/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// AUTOGENERATED - Run `yarn gen-consts` to re-generate.

export const builtinOAuthProviderNames = [
"builtin::oauth_apple",
"builtin::oauth_azure",
"builtin::oauth_github",
"builtin::oauth_google",
] as const;
export type BuiltinOAuthProviderNames =
(typeof builtinOAuthProviderNames)[number];

export const emailPasswordProviderName = "builtin::local_emailpassword";
Loading

0 comments on commit 32877ff

Please sign in to comment.