NORY-14: refactor Kratos and Hydra APIs (authentication)

This commit is contained in:
Markus Thielker 2024-12-01 15:39:34 +01:00
parent 215d47e888
commit 9ee7da3a6c
No known key found for this signature in database
12 changed files with 51 additions and 27 deletions

View file

@ -2,11 +2,11 @@
import React from 'react';
import { Card } from '@/components/ui/card';
import getHydra from '@/ory/sdk/hydra';
import { OAuth2ConsentRequest, OAuth2RedirectTo } from '@ory/client';
import ConsentForm from '@/components/consentForm';
import { redirect } from 'next/navigation';
import { toast } from 'sonner';
import { getOAuth2Api } from '@/ory/sdk/server';
export default async function Consent(props: { searchParams: Promise<{ consent_challenge: string }> }) {
@ -18,7 +18,7 @@ export default async function Consent(props: { searchParams: Promise<{ consent_c
const onAccept = async (challenge: string, scopes: string[], remember: boolean) => {
'use server';
const hydra = await getHydra();
const hydra = await getOAuth2Api();
const response = await hydra
.acceptOAuth2ConsentRequest({
consentChallenge: challenge,
@ -43,7 +43,7 @@ export default async function Consent(props: { searchParams: Promise<{ consent_c
const onReject = async (challenge: string) => {
'use server';
const hydra = await getHydra();
const hydra = await getOAuth2Api();
const response: OAuth2RedirectTo | void = await hydra
.rejectOAuth2ConsentRequest({
consentChallenge: challenge,
@ -64,7 +64,7 @@ export default async function Consent(props: { searchParams: Promise<{ consent_c
return;
}
const hydra = await getHydra();
const hydra = await getOAuth2Api();
await hydra
.getOAuth2ConsentRequest({ consentChallenge })
.then(({ data }) => {

View file

@ -4,7 +4,7 @@ import React, { useEffect, useState } from 'react';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { FlowError } from '@ory/client';
import { AxiosError } from 'axios';
import { kratos } from '@/ory/sdk/kratos';
import { kratos } from '../../../ory/sdk/client';
import { useRouter, useSearchParams } from 'next/navigation';
import Link from 'next/link';
import { Button } from '@/components/ui/button';

View file

@ -5,7 +5,7 @@ import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/com
import { Flow, HandleError, LogoutLink } from '@/ory';
import Link from 'next/link';
import { LoginFlow, UpdateLoginFlowBody } from '@ory/client';
import { kratos } from '@/ory/sdk/kratos';
import { kratos } from '../../../ory/sdk/client';
import { Button } from '@/components/ui/button';
import { useRouter, useSearchParams } from 'next/navigation';
import Image from 'next/image';

View file

@ -5,7 +5,7 @@ import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/com
import { Flow, HandleError } from '@/ory';
import { RecoveryFlow, UpdateRecoveryFlowBody } from '@ory/client';
import { AxiosError } from 'axios';
import { kratos } from '@/ory/sdk/kratos';
import { kratos } from '../../../ory/sdk/client';
import { useRouter, useSearchParams } from 'next/navigation';
import Link from 'next/link';
import { Button } from '@/components/ui/button';

View file

@ -6,7 +6,7 @@ import { Flow, HandleError } from '@/ory';
import Link from 'next/link';
import { RegistrationFlow, UpdateRegistrationFlowBody } from '@ory/client';
import { AxiosError } from 'axios';
import { kratos } from '@/ory/sdk/kratos';
import { kratos } from '../../../ory/sdk/client';
import { useRouter, useSearchParams } from 'next/navigation';
import { Button } from '@/components/ui/button';
import { Skeleton } from '@/components/ui/skeleton';

View file

@ -5,7 +5,7 @@ import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/com
import { Flow, HandleError } from '@/ory';
import { UpdateVerificationFlowBody, VerificationFlow } from '@ory/client';
import { AxiosError } from 'axios';
import { kratos } from '@/ory/sdk/kratos';
import { kratos } from '../../../ory/sdk/client';
import { useRouter, useSearchParams } from 'next/navigation';
import Link from 'next/link';
import { Button } from '@/components/ui/button';

View file

@ -1,5 +1,5 @@
import { NextRequest, NextResponse } from 'next/server';
import { getFrontendApi } from '@/ory/sdk/hydra';
import { getFrontendApi } from 'ory/sdk/server';
import { cookies } from 'next/headers';
export async function middleware(request: NextRequest) {

View file

@ -3,7 +3,7 @@
import { AxiosError } from 'axios';
import React, { DependencyList, useEffect, useState } from 'react';
import { kratos } from './sdk/kratos';
import { kratos } from './sdk/client';
import { AppRouterInstance } from 'next/dist/shared/lib/app-router-context.shared-runtime';
export const HandleError = (

View file

@ -1,3 +1,3 @@
export * from './hooks';
export * from './ui';
export * from './sdk/kratos';
export * from './sdk/client';

View file

@ -1,15 +0,0 @@
'use server';
import { Configuration, OAuth2Api } from '@ory/client';
// implemented as a function because of 'use server'
export default async function getHydra() {
return new OAuth2Api(new Configuration(
new Configuration({
basePath: process.env.ORY_HYDRA_ADMIN_URL,
baseOptions: {
withCredentials: true,
},
}),
));
}

View file

@ -0,0 +1,39 @@
'use server';
import { Configuration, FrontendApi, OAuth2Api } from '@ory/client';
// ####################################################################################
// OAuth2 API
// ####################################################################################
const oAuth2Api = new OAuth2Api(new Configuration(
{
basePath: process.env.ORY_HYDRA_ADMIN_URL,
baseOptions: {
withCredentials: true,
},
},
));
export async function getOAuth2Api() {
return oAuth2Api;
}
// ####################################################################################
// Frontend API
// ####################################################################################
const frontendApi = new FrontendApi(
new Configuration({
basePath: process.env.NEXT_PUBLIC_ORY_KRATOS_URL,
baseOptions: {
withCredentials: true,
},
}),
);
export async function getFrontendApi() {
return frontendApi;
}