Skip to content

Commit

Permalink
Use AWS Cognito for user management (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
junlarsen authored Sep 10, 2023
1 parent c56814b commit c8f7ace
Show file tree
Hide file tree
Showing 82 changed files with 597 additions and 1,131 deletions.
4 changes: 0 additions & 4 deletions .env.local
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# Database
DATABASE_URL=postgres://ow:owpassword123@localhost:6543/ow

# Hydra
HYDRA_ADMIN_URL=http://127.0.0.1:4445
HYDRA_PUBLIC_URL=http://127.0.0.1:4444

# NextAuth
NEXTAUTH_URL=http://localhost:3000
NEXTAUTH_CLIENT_ID=
Expand Down
19 changes: 3 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,9 @@ https://owdocs.vercel.app

1. `pnpm i` to install packages
2. `pnpm build` to build packages
3. `docker-compose up` to start Hydra and databases
4. If you want authentication (optional), run:

```
docker exec hydra hydra create --endpoint http://127.0.0.1:4445 oauth2-client \
--name onlineweb \
--client-uri onlineweb \
--token-endpoint-auth-method none \
--grant-type authorization_code,refresh_token \
--response-type code \
--scope profile,email,openid \
--redirect-uri http://localhost:3000/api/auth/callback/onlineweb
```

5. The command will output a client id, set this in apps/web/.env. Copy the example from .env.example
6. `pnpm dev` to run the project
3. `docker-compose up` to start databases
4. The command will output a client id, set this in apps/web/.env. Copy the example from .env.example
5. `pnpm dev` to run the project
- You can run `pnpm dev --filter=<project>` to run a certain project from root, such as `pnpm dev --filter=web`

### [Payment] How to configure stripe (locally)
Expand Down
1 change: 1 addition & 0 deletions apps/dashboard/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXTAUTH_URL=http://localhost:3002
11 changes: 7 additions & 4 deletions apps/dashboard/next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ const config = {
experimental: {
appDir: true,
},
transpilePackages: ["@dotkomonline/ui", "@dotkomonline/types","@dotkomonline/auth", "@dotkomonline/api"],
eslint: {
ignoreDuringBuilds: !!process.env.CI,
},
transpilePackages: [
"@dotkomonline/auth",
"@dotkomonline/env",
"@dotkomonline/gateway-trpc",
"@dotkomonline/types",
"@dotkomonline/ui"
],
}

export default config
1 change: 1 addition & 0 deletions apps/dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"@dotkomonline/auth": "workspace:*",
"@dotkomonline/env": "workspace:*",
"@dotkomonline/gateway-trpc": "workspace:*",
"@dotkomonline/types": "workspace:*",
"@dotkomonline/ui": "workspace:*",
Expand Down
6 changes: 0 additions & 6 deletions apps/dashboard/src/app/ApplicationShell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ const SIDEBAR_LINKS = [
label: "Komiteer",
targetSegment: "committee",
},
{
icon: "tabler:user-circle",
href: "/user",
label: "Brukere",
targetSegment: "user",
},
]

const ApplicationSidebar: FC = () => {
Expand Down
16 changes: 0 additions & 16 deletions apps/dashboard/src/app/user/UserDetailsModal.tsx

This file was deleted.

81 changes: 0 additions & 81 deletions apps/dashboard/src/app/user/page.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion apps/dashboard/src/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { authOptions } from "@dotkomonline/auth"
import NextAuth from "next-auth"
import { dashboard as authOptions } from "@dotkomonline/auth"

export default NextAuth(authOptions)
10 changes: 5 additions & 5 deletions apps/dashboard/src/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { CreateTRPCClientOptions, httpBatchLink, loggerLink } from "@trpc/client
import { inferRouterInputs, inferRouterOutputs } from "@trpc/server"
import type { AppRouter } from "@dotkomonline/gateway-trpc"
import superjson from "superjson"
import { env } from "@dotkomonline/env"

const getBaseUrl = () => {
if (process.env.NODE_ENV === "production") return "https://new.online.ntnu.no/"
if (process.env.VERCEL_URL) return `https://${process.env.VERCEL_URL}` // SSR should use vercel url
return `http://localhost:${process.env.PORT ?? 3000}` // dev SSR should use localhost
if (env.NODE_ENV === "production") return "https://new.online.ntnu.no/"
if (env.VERCEL_URL) return `https://${env.VERCEL_URL}`
return `http://localhost:3002`
}

export const trpcConfig: CreateTRPCClientOptions<AppRouter> = {
transformer: superjson,
links: [
loggerLink({
enabled: (opts) =>
process.env.NODE_ENV === "development" || (opts.direction === "down" && opts.result instanceof Error),
enabled: (opts) => env.NODE_ENV === "development" || (opts.direction === "down" && opts.result instanceof Error),
}),
httpBatchLink({
url: `${getBaseUrl()}/api/trpc`,
Expand Down
3 changes: 2 additions & 1 deletion apps/db-migrator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "db-migrator",
"type": "module",
"dependencies": {
"@dotkomonline/env": "workspace:*",
"@dotkomonline/db": "workspace:*",
"@dotkomonline/types": "workspace:*",
"chalk": "^5.3.0",
Expand All @@ -11,7 +12,7 @@
"zod": "^3.21.4"
},
"devDependencies": {
"@dotkomonline/logger": "*",
"@dotkomonline/logger": "workspace:*",
"@faker-js/faker": "^7.6.0",
"@types/eslint": "^8.44.1",
"@types/pg": "^8.10.2",
Expand Down
2 changes: 1 addition & 1 deletion apps/db-migrator/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { CockroachDialect, Database } from "@dotkomonline/db"
import { Kysely, CamelCasePlugin } from "kysely"
import pg from "pg"

import { env } from "./env"
import { env } from "@dotkomonline/env"

export const db = new Kysely<Database>({
dialect: new CockroachDialect({
Expand Down
16 changes: 0 additions & 16 deletions apps/db-migrator/src/env.ts

This file was deleted.

1 change: 1 addition & 0 deletions apps/web/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXTAUTH_URL=http://localhost:3000
17 changes: 10 additions & 7 deletions apps/web/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* eslint-disable */
!process.env.SKIP_ENV_VALIDATION && (await import("./src/env/server.mjs"))

/**
* @type {import('next').NextConfig}
*/
Expand All @@ -14,10 +11,16 @@ const config = {
appDir: true,
swcPlugins: [["next-superjson-plugin", {}]]
},
transpilePackages: ["@dotkomonline/ui", "@dotkomonline/types", "@dotkomonline/api"],
eslint: {
ignoreDuringBuilds: !!process.env.CI,
},
transpilePackages: [
"@dotkomonline/auth",
"@dotkomonline/db",
"@dotkomonline/env",
"@dotkomonline/emails",
"@dotkomonline/gateway-edge-nextjs",
"@dotkomonline/gateway-trpc",
"@dotkomonline/types",
"@dotkomonline/ui",
],
}

export default config
11 changes: 5 additions & 6 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
},
"dependencies": {
"@aws-sdk/client-ses": "^3.379.1",
"@clerk/nextjs": "^4.23.1",
"@clerk/themes": "^1.7.5",
"@dotkomonline/auth": "workspace:*",
"@dotkomonline/db": "workspace:*",
"@dotkomonline/env": "workspace:*",
"@dotkomonline/emails": "workspace:*",
"@dotkomonline/gateway-edge-nextjs": "workspace:*",
"@dotkomonline/gateway-trpc": "workspace:*",
"@dotkomonline/types": "workspace:*",
"@dotkomonline/ui": "workspace:*",
"@next/env": "^13.4.12",
"@next/font": "^13.4.12",
"@ory/client": "^1.1.41",
"@portabletext/react": "^2.0.3",
"@radix-ui/colors": "^0.1.9",
"@radix-ui/react-avatar": "^1.0.3",
Expand Down Expand Up @@ -58,9 +58,8 @@
"zod": "^3.21.4"
},
"devDependencies": {
"@dotkomonline/config": "^1.0.0",
"@dotkomonline/db": "^1.0.0",
"@dotkomonline/logger": "*",
"@dotkomonline/config": "workspace:*",
"@dotkomonline/logger": "workspace:*",
"@types/cors": "^2.8.13",
"@types/eslint": "^8.44.1",
"@types/jsdom": "^21.1.1",
Expand Down
Empty file removed apps/web/src/assets/.gitignore
Empty file.
5 changes: 0 additions & 5 deletions apps/web/src/components/layout/AuthLayout.tsx

This file was deleted.

16 changes: 6 additions & 10 deletions apps/web/src/components/organisms/Navbar/ProfileMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,32 @@ import {
DropdownMenuTrigger,
Icon,
} from "@dotkomonline/ui"
import { useRouter } from "next/router"
import { FC, PropsWithChildren } from "react"
import { useTheme } from "next-themes"
import { navigationMenuTriggerStyle } from "./NavigationMenu"
import { useAuth } from "@clerk/nextjs"
import { signIn, signOut, useSession } from "next-auth/react"

export const ProfileMenu = () => {
const router = useRouter()
const { isSignedIn, isLoaded } = useAuth()
const { status } = useSession()

if (!isLoaded) {
if (status === "loading") {
return <Icon icon="tabler:loader-2" className="animate-spin" />
}

if (!isSignedIn) {
if (status === "unauthenticated") {
return (
<>
<Button
variant="subtle"
className={cn(navigationMenuTriggerStyle(), "hover:translate-y-0 active:translate-y-0")}
onClick={() => router.push("/sign-in")}
onClick={() => signIn("cognito")}
>
Log in
</Button>
<Button
variant="gradient"
className={cn(navigationMenuTriggerStyle(), "ml-3 hover:translate-y-0 active:translate-y-0")}
onClick={() => router.push("/sign-up")}
onClick={() => signIn("cognito")}
>
Sign up
</Button>
Expand Down Expand Up @@ -120,9 +118,7 @@ const linkGroups: Link[][] = [
],
]

// TODO: link
const AvatarDropdown: FC<PropsWithChildren> = ({ children }) => {
const { signOut } = useAuth()
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>{children}</DropdownMenuTrigger>
Expand Down
Loading

1 comment on commit c8f7ace

@vercel
Copy link

@vercel vercel bot commented on c8f7ace Sep 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.