Skip to content

Commit

Permalink
feat(verity): add auth-next and Keycloak provider
Browse files Browse the repository at this point in the history
  • Loading branch information
IKatsuba committed May 31, 2024
1 parent 7dd8f06 commit 77418d1
Show file tree
Hide file tree
Showing 23 changed files with 891 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
AUTH_KEYCLOAK_ID=verity_client
AUTH_KEYCLOAK_SECRET=veritysecret
AUTH_KEYCLOAK_ISSUER=http://localhost:8080/realms/verity
AUTH_SECRET=authsecret

DATABASE_URL=postgresql://verity:verity@localhost:5432/verity
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,58 @@

Verity is a service that allows you to create new versions of various applications and connect them
to each other. Manage your applications easily and efficiently.

## Local Development

### Prerequisites

- [Docker](https://www.docker.com/)
- [Docker Compose](https://docs.docker.com/compose/)
- [Node.js LTS](https://nodejs.org/en/)

### Getting Started

1. Clone the repository:

```bash
git clone git@github.com:platacard/verity.git
```

or

```bash
git clone https://github.com/platacard/verity.git
```

2. Change to the project directory:

```bash
cd verity
```

3. Start the development environment:

```bash
docker-compose up -d
```

4. Install the dependencies:

```bash
npm install
```

5. Start the development server:

```bash
npm start
```

6. Open your browser and navigate to [http://localhost:3000](http://localhost:3000).

7. Login with the following credentials:

- **Username:** verity
- **Password:** verity

8. You're all set! 🚀
3 changes: 3 additions & 0 deletions apps/web/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { handlers } from '@verity/auth/server';

export const { GET, POST } = handlers;
9 changes: 8 additions & 1 deletion apps/web/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import Link from 'next/link';
import { redirect } from 'next/navigation';

import { MountainIcon } from 'lucide-react';

import { auth } from '@verity/auth/server';
import { Button } from '@verity/ui/button';
import { Input } from '@verity/ui/input';

export default function Component() {
export default async function Component() {
const session = await auth();
if (!session) {
redirect('/api/auth/signin');
}

return (
<div className="flex flex-col min-h-[100dvh]">
<header className="px-4 lg:px-6 h-14 flex items-center">
Expand Down
10 changes: 10 additions & 0 deletions apps/web/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import NextAuth from 'next-auth';
import Keycloak from 'next-auth/providers/keycloak';

export const middleware = NextAuth({
providers: [Keycloak],
}).auth;

export const config = {
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
};
6 changes: 5 additions & 1 deletion apps/web/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
"projectType": "application",
"tags": [],
"// targets": "to see all targets run: nx show project web --web",
"targets": {}
"targets": {
"dev": {
"dependsOn": ["verity:prisma:migrate:dev"]
}
}
}
63 changes: 63 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
version: '3.8'

services:
keycloakdb:
image: postgres:latest
container_name: postgres
environment:
POSTGRES_DB: keycloak
POSTGRES_USER: keycloak
POSTGRES_PASSWORD: keycloak
volumes:
- keycloakdb_data:/var/lib/postgresql/data
networks:
- keycloak-network

keycloak:
image: quay.io/keycloak/keycloak:latest
container_name: keycloak
command:
- start-dev
- -Dkeycloak.migration.action=import
- -Dkeycloak.migration.provider=singleFile
- -Dkeycloak.migration.file=/opt/keycloak/data/import/realm-export.json
- -Dkeycloak.migration.strategy=IGNORE_EXISTING
environment:
DB_VENDOR: POSTGRES
DB_ADDR: keycloakdb
DB_DATABASE: keycloak
DB_USER: keycloak
DB_PASSWORD: keycloak
KEYCLOAK_ADMIN: admin
KEYCLOAK_ADMIN_PASSWORD: admin
KEYCLOAK_IMPORT: /opt/keycloak/data/import/realm-export.json
volumes:
- ./realm-export.json:/opt/keycloak/data/import/realm-export.json
ports:
- 8080:8080
depends_on:
- keycloakdb
networks:
- keycloak-network

verity_db:
image: postgres:latest
container_name: verity_db
environment:
POSTGRES_DB: verity
POSTGRES_USER: verity
POSTGRES_PASSWORD: verity
volumes:
- verity_db_data:/var/lib/postgresql/data
networks:
- keycloak-network
ports:
- 5432:5432

networks:
keycloak-network:
driver: bridge

volumes:
keycloakdb_data:
verity_db_data:
18 changes: 18 additions & 0 deletions libs/auth/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["plugin:@nx/react", "../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
7 changes: 7 additions & 0 deletions libs/auth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# auth

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test auth` to execute the unit tests via [Jest](https://jestjs.io).
9 changes: 9 additions & 0 deletions libs/auth/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "auth",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/auth/src",
"projectType": "library",
"tags": [],
"// targets": "to see all targets run: nx show project auth --web",
"targets": {}
}
1 change: 1 addition & 0 deletions libs/auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { signIn, signOut } from 'next-auth/react';
13 changes: 13 additions & 0 deletions libs/auth/src/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import NextAuth from 'next-auth';
import Keycloak from 'next-auth/providers/keycloak';

import { PrismaAdapter } from '@auth/prisma-adapter';
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

export const { handlers, signIn, signOut, auth } = NextAuth({
adapter: PrismaAdapter(prisma),
providers: [Keycloak],
session: { strategy: 'jwt' },
});
3 changes: 3 additions & 0 deletions libs/auth/src/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'server-only';

export * from './lib/auth';
17 changes: 17 additions & 0 deletions libs/auth/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"jsx": "react-jsx",
"allowJs": false,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
}
],
"extends": "../../tsconfig.base.json"
}
25 changes: 25 additions & 0 deletions libs/auth/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"types": [
"node",
"@nx/react/typings/cssmodule.d.ts",
"@nx/react/typings/image.d.ts",
"next",
"@nx/next/typings/image.d.ts"
]
},
"exclude": [
"jest.config.ts",
"src/**/*.spec.ts",
"src/**/*.test.ts",
"src/**/*.spec.tsx",
"src/**/*.test.tsx",
"src/**/*.spec.js",
"src/**/*.test.js",
"src/**/*.spec.jsx",
"src/**/*.test.jsx"
],
"include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts", "src/**/*.tsx"]
}
Loading

0 comments on commit 77418d1

Please sign in to comment.