Skip to content
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zenstack-monorepo",
"version": "0.4.0",
"version": "0.4.1",
"description": "",
"scripts": {
"build": "pnpm -r build",
Expand Down
14 changes: 14 additions & 0 deletions packages/next-auth/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
]
}
5 changes: 5 additions & 0 deletions packages/next-auth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# ZenStack Runtime Library

This package is for integrating [next-auth](https://next-auth.js.org/) with ZenStack.

Visit [Homepage](https://zenstack.dev) for more details.
46 changes: 46 additions & 0 deletions packages/next-auth/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@zenstackhq/next-auth",
"displayName": "ZenStack next-auth integration library",
"version": "0.4.1",
"description": "ZenStack adapter for integrating with next-auth",
"repository": {
"type": "git",
"url": "https://github.com/zenstackhq/zenstack"
},
"main": "index.js",
"scripts": {
"clean": "rimraf dist",
"build": "pnpm lint && pnpm clean && tsc && cp ./package.json ./README.md dist/",
"watch": "tsc --watch",
"lint": "eslint src --ext ts",
"prepublishOnly": "pnpm build",
"publish-dev": "pnpm publish --tag dev"
},
"publishConfig": {
"directory": "dist",
"linkDirectory": true
},
"author": {
"name": "ZenStack Team"
},
"homepage": "https://zenstack.dev",
"license": "MIT",
"keywords": [
"zenstack",
"next-auth"
],
"dependencies": {
"@next-auth/prisma-adapter": "^1.0.5",
"@zenstackhq/runtime": "workspace:*",
"bcryptjs": "^2.4.3"
},
"devDependencies": {
"@types/bcryptjs": "^2.4.2",
"next-auth": "^4.0.0",
"rimraf": "^3.0.2",
"typescript": "^4.9.3"
},
"peerDependencies": {
"next-auth": "^4.0.0"
}
}
11 changes: 11 additions & 0 deletions packages/next-auth/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Adapter } from 'next-auth/adapters';
import type { Service } from '@zenstackhq/runtime/server';
import { PrismaAdapter } from '@next-auth/prisma-adapter';

/**
* Next-auth adapter for reading and persisting auth entities
* @param service ZenStack service
*/
export function Adapter(service: Service): Adapter {
return PrismaAdapter(service.db);
}
59 changes: 59 additions & 0 deletions packages/next-auth/src/authorize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Service } from '@zenstackhq/runtime/server';
import { compare } from 'bcryptjs';

async function verifyPassword(password: string, hashedPassword: string) {
return compare(password, hashedPassword);
}

export class AuthorizeError extends Error {
constructor(message: string) {
super(message);
}
}

export function authorize(service: Service) {
return async (
credentials: Record<'email' | 'password', string> | undefined
) => {
if (!credentials) {
throw new AuthorizeError('Missing credentials');
}

if (!credentials.email) {
throw new AuthorizeError('"email" is required in credentials');
}

if (!credentials.password) {
throw new AuthorizeError('"password" is required in credentials');
}

const maybeUser = await service.db.user.findFirst({
where: {
email: credentials.email,
},
select: {
id: true,
email: true,
password: true,
},
});

if (!maybeUser || !maybeUser.password) {
return null;
}

const isValid = await verifyPassword(
credentials.password,
maybeUser.password
);

if (!isValid) {
return null;
}

return {
id: maybeUser.id,
email: maybeUser.email,
};
};
}
2 changes: 2 additions & 0 deletions packages/next-auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './adapter';
export * from './authorize';
22 changes: 22 additions & 0 deletions packages/next-auth/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"target": "ES6",
"module": "CommonJS",
"lib": ["ESNext"],
"sourceMap": true,
"outDir": "dist",
"strict": true,
"noUnusedLocals": true,
"noImplicitReturns": true,
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"declaration": true,
"resolveJsonModule": true,
"strictPropertyInitialization": false,
"paths": {}
},
"include": ["src/**/*.ts"],
"exclude": ["dist", "node_modules"]
}
2 changes: 1 addition & 1 deletion packages/runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

This package is the runtime library supporting web apps built using ZenStack.

Visit [Homepage](https://github.com/zenstackhq/zenstack#readme) for more details.
Visit [Homepage](https://zenstack.dev) for more details.
5 changes: 3 additions & 2 deletions packages/runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "@zenstackhq/runtime",
"displayName": "ZenStack Runtime Library",
"version": "0.4.0",
"version": "0.4.1",
"description": "Runtime of ZenStack for both client-side and server-side environments.",
"repository": {
"type": "git",
"url": "https://github.com/zenstackhq/zenstack"
},
"scripts": {
"clean": "rimraf dist",
"build": "npm run clean && tsc && cp -r pre/* dist/ && cp ./package.json dist/",
"build": "pnpm lint && pnpm clean && tsc && cp -r pre/* dist/ && cp ./package.json ./README.md dist/",
"watch": "tsc --watch",
"lint": "eslint src --ext ts",
"prepublishOnly": "pnpm build",
Expand Down Expand Up @@ -39,6 +39,7 @@
"author": {
"name": "ZenStack Team"
},
"homepage": "https://zenstack.dev",
"license": "MIT",
"devDependencies": {
"@types/bcryptjs": "^2.4.2",
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime/pre/server/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export type {
PolicyOperationKind,
RuntimeAttribute,
QueryContext,
Service,
DbClientContract,
} from '../lib/types';

export {
Expand Down
5 changes: 3 additions & 2 deletions packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"name": "zenstack",
"publisher": "zenstack",
"displayName": "ZenStack Language Tools",
"description": "A toolkit for modeling data and access policies in full-stack development with Next.js and Typescript",
"version": "0.4.0",
"description": "A toolkit for building secure CRUD apps with Next.js + Typescript",
"version": "0.4.1",
"author": {
"name": "ZenStack Team"
},
"homepage": "https://zenstack.dev",
"license": "MIT",
"keywords": [
"fullstack",
Expand Down
2 changes: 0 additions & 2 deletions packages/schema/src/generator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import colors from 'colors';
import PrismaGenerator from './prisma';
import ServiceGenerator from './service';
import ReactHooksGenerator from './react-hooks';
import NextAuthGenerator from './next-auth';
import { TypescriptCompilation } from './tsc';
import FieldConstraintGenerator from './field-constraint';
import telemetry from '../telemetry';
Expand Down Expand Up @@ -46,7 +45,6 @@ export class ZenStackGenerator {
new PrismaGenerator(),
new ServiceGenerator(),
new ReactHooksGenerator(),
new NextAuthGenerator(),
new FieldConstraintGenerator(),
new TypescriptCompilation(),
];
Expand Down
Loading