From 854b6084c2501f78c7e15df13a76f7c4abcf3845 Mon Sep 17 00:00:00 2001
From: JoblersTune
Date: Fri, 1 Mar 2024 12:53:36 +0200
Subject: [PATCH] TMP: Kratos configured and basic registration and
verifications flows are set
---
.../frontend/app/lib/kratos_checks.server.ts | 32 +++++
packages/frontend/app/root.tsx | 6 +
.../app/routes/{auth.tsx => auth._index.tsx} | 46 +++++--
.../app/routes/{login.tsx => auth.login.tsx} | 2 +-
.../frontend/app/routes/auth.registration.tsx | 102 +++++++++++++++
.../frontend/app/routes/auth.verification.tsx | 121 ++++++++++++++++++
packages/frontend/app/routes/callback.tsx | 8 +-
packages/frontend/app/routes/consent.tsx | 10 +-
packages/frontend/app/routes/registration.tsx | 101 ---------------
packages/frontend/hydra/docker-compose.yml | 6 +-
packages/frontend/kratos/Dockerfile | 2 +-
packages/frontend/kratos/config/kratos.yml | 31 ++---
packages/frontend/kratos/docker-compose.yml | 10 +-
13 files changed, 334 insertions(+), 143 deletions(-)
create mode 100644 packages/frontend/app/lib/kratos_checks.server.ts
rename packages/frontend/app/routes/{auth.tsx => auth._index.tsx} (57%)
rename packages/frontend/app/routes/{login.tsx => auth.login.tsx} (98%)
create mode 100644 packages/frontend/app/routes/auth.registration.tsx
create mode 100644 packages/frontend/app/routes/auth.verification.tsx
delete mode 100644 packages/frontend/app/routes/registration.tsx
diff --git a/packages/frontend/app/lib/kratos_checks.server.ts b/packages/frontend/app/lib/kratos_checks.server.ts
new file mode 100644
index 0000000000..f37dfc6e2f
--- /dev/null
+++ b/packages/frontend/app/lib/kratos_checks.server.ts
@@ -0,0 +1,32 @@
+// todo remove session id logic since its actually a token and being handled differently
+import { redirect } from '@remix-run/node'
+import axios from 'axios'
+
+export async function requireSession(cookieHeader?: string | null) {
+ console.log('COOKIES: ', cookieHeader)
+
+ try {
+ const session = await axios.get(`http://kratos:4433/sessions/whoami`, {
+ headers: {
+ cookie: cookieHeader
+ },
+ withCredentials: true
+ })
+
+ console.log('SESSION DATA: ', session.data)
+ console.log(
+ 'VERIFIABLE ADDRESSES: ',
+ session.data.identity.verifiable_addresses
+ )
+
+ if (session.status !== 200 || !session.data?.active) {
+ // does active here mean it is a legit logged in session?
+ // Redirect to auth if there's no valid session
+ throw redirect('/auth')
+ }
+
+ return session
+ } catch {
+ throw redirect('/auth')
+ }
+}
diff --git a/packages/frontend/app/root.tsx b/packages/frontend/app/root.tsx
index 865c037b9b..62b8453cfb 100644
--- a/packages/frontend/app/root.tsx
+++ b/packages/frontend/app/root.tsx
@@ -22,6 +22,7 @@ import { messageStorage, type Message } from './lib/message.server'
import tailwind from './styles/tailwind.css'
import { getOpenPaymentsUrl } from './shared/utils'
import { PublicEnv, type PublicEnvironment } from './PublicEnv'
+import { requireSession } from './lib/kratos_checks.server'
export const meta: MetaFunction = () => [
{ title: 'Rafiki Admin' },
@@ -30,6 +31,11 @@ export const meta: MetaFunction = () => [
]
export const loader = async ({ request }: LoaderFunctionArgs) => {
+ console.log('URL: ', request.url)
+ const url = new URL(request.url)
+ if (!url.pathname.startsWith('/auth')) {
+ await requireSession(request.headers.get('cookie'))
+ }
const session = await messageStorage.getSession(request.headers.get('cookie'))
const message = session.get('message') as Message
diff --git a/packages/frontend/app/routes/auth.tsx b/packages/frontend/app/routes/auth._index.tsx
similarity index 57%
rename from packages/frontend/app/routes/auth.tsx
rename to packages/frontend/app/routes/auth._index.tsx
index c5b6391f57..01c3833a73 100644
--- a/packages/frontend/app/routes/auth.tsx
+++ b/packages/frontend/app/routes/auth._index.tsx
@@ -1,6 +1,7 @@
import { version } from '../../../../package.json'
import { Form } from '@remix-run/react'
import { redirect, type ActionFunctionArgs } from '@remix-run/node'
+import { Button } from '../components/ui'
export default function Auth() {
return (
@@ -19,10 +20,31 @@ export default function Auth() {
In this web application, you'll be able to manage peering
relationships, assets, and wallet addresses, among other settings.
+
+
+ https://rafiki.dev
+
+
-
@@ -36,15 +58,17 @@ export async function action({ request }: ActionFunctionArgs) {
const action = formData.get('action')
if (action === 'login') {
- // TODO: Make an API call to Ory Kratos for login
- return
+ return redirect('http://127.0.0.1:4433/self-service/login/browser', {
+ headers: {
+ Accept: 'text/html'
+ }
+ })
} else if (action === 'register') {
- return redirect('http://localhost:4433/self-service/registration/browser',
- {
- headers: {
- 'Accept': 'text/html'
- }
- })
+ return redirect('http://127.0.0.1:4433/self-service/registration/browser', {
+ headers: {
+ Accept: 'text/html'
+ }
+ })
}
throw new Error('Invalid auth action')
}
diff --git a/packages/frontend/app/routes/login.tsx b/packages/frontend/app/routes/auth.login.tsx
similarity index 98%
rename from packages/frontend/app/routes/login.tsx
rename to packages/frontend/app/routes/auth.login.tsx
index 1ff786234b..f35c794b5f 100644
--- a/packages/frontend/app/routes/login.tsx
+++ b/packages/frontend/app/routes/auth.login.tsx
@@ -20,7 +20,7 @@ export default function Login() {