mirror of
https://codeberg.org/MarkusThielker/next-ory.git
synced 2025-04-19 00:51:18 +00:00
NORY-14: refactor Kratos and Hydra APIs (authentication)
This commit is contained in:
parent
215d47e888
commit
9ee7da3a6c
12 changed files with 51 additions and 27 deletions
|
@ -2,11 +2,11 @@
|
||||||
|
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { Card } from '@/components/ui/card';
|
import { Card } from '@/components/ui/card';
|
||||||
import getHydra from '@/ory/sdk/hydra';
|
|
||||||
import { OAuth2ConsentRequest, OAuth2RedirectTo } from '@ory/client';
|
import { OAuth2ConsentRequest, OAuth2RedirectTo } from '@ory/client';
|
||||||
import ConsentForm from '@/components/consentForm';
|
import ConsentForm from '@/components/consentForm';
|
||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
import { toast } from 'sonner';
|
import { toast } from 'sonner';
|
||||||
|
import { getOAuth2Api } from '@/ory/sdk/server';
|
||||||
|
|
||||||
export default async function Consent(props: { searchParams: Promise<{ consent_challenge: string }> }) {
|
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) => {
|
const onAccept = async (challenge: string, scopes: string[], remember: boolean) => {
|
||||||
'use server';
|
'use server';
|
||||||
|
|
||||||
const hydra = await getHydra();
|
const hydra = await getOAuth2Api();
|
||||||
const response = await hydra
|
const response = await hydra
|
||||||
.acceptOAuth2ConsentRequest({
|
.acceptOAuth2ConsentRequest({
|
||||||
consentChallenge: challenge,
|
consentChallenge: challenge,
|
||||||
|
@ -43,7 +43,7 @@ export default async function Consent(props: { searchParams: Promise<{ consent_c
|
||||||
const onReject = async (challenge: string) => {
|
const onReject = async (challenge: string) => {
|
||||||
'use server';
|
'use server';
|
||||||
|
|
||||||
const hydra = await getHydra();
|
const hydra = await getOAuth2Api();
|
||||||
const response: OAuth2RedirectTo | void = await hydra
|
const response: OAuth2RedirectTo | void = await hydra
|
||||||
.rejectOAuth2ConsentRequest({
|
.rejectOAuth2ConsentRequest({
|
||||||
consentChallenge: challenge,
|
consentChallenge: challenge,
|
||||||
|
@ -64,7 +64,7 @@ export default async function Consent(props: { searchParams: Promise<{ consent_c
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const hydra = await getHydra();
|
const hydra = await getOAuth2Api();
|
||||||
await hydra
|
await hydra
|
||||||
.getOAuth2ConsentRequest({ consentChallenge })
|
.getOAuth2ConsentRequest({ consentChallenge })
|
||||||
.then(({ data }) => {
|
.then(({ data }) => {
|
||||||
|
|
|
@ -4,7 +4,7 @@ import React, { useEffect, useState } from 'react';
|
||||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||||
import { FlowError } from '@ory/client';
|
import { FlowError } from '@ory/client';
|
||||||
import { AxiosError } from 'axios';
|
import { AxiosError } from 'axios';
|
||||||
import { kratos } from '@/ory/sdk/kratos';
|
import { kratos } from '../../../ory/sdk/client';
|
||||||
import { useRouter, useSearchParams } from 'next/navigation';
|
import { useRouter, useSearchParams } from 'next/navigation';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/com
|
||||||
import { Flow, HandleError, LogoutLink } from '@/ory';
|
import { Flow, HandleError, LogoutLink } from '@/ory';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { LoginFlow, UpdateLoginFlowBody } from '@ory/client';
|
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 { Button } from '@/components/ui/button';
|
||||||
import { useRouter, useSearchParams } from 'next/navigation';
|
import { useRouter, useSearchParams } from 'next/navigation';
|
||||||
import Image from 'next/image';
|
import Image from 'next/image';
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/com
|
||||||
import { Flow, HandleError } from '@/ory';
|
import { Flow, HandleError } from '@/ory';
|
||||||
import { RecoveryFlow, UpdateRecoveryFlowBody } from '@ory/client';
|
import { RecoveryFlow, UpdateRecoveryFlowBody } from '@ory/client';
|
||||||
import { AxiosError } from 'axios';
|
import { AxiosError } from 'axios';
|
||||||
import { kratos } from '@/ory/sdk/kratos';
|
import { kratos } from '../../../ory/sdk/client';
|
||||||
import { useRouter, useSearchParams } from 'next/navigation';
|
import { useRouter, useSearchParams } from 'next/navigation';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
|
|
|
@ -6,7 +6,7 @@ import { Flow, HandleError } from '@/ory';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { RegistrationFlow, UpdateRegistrationFlowBody } from '@ory/client';
|
import { RegistrationFlow, UpdateRegistrationFlowBody } from '@ory/client';
|
||||||
import { AxiosError } from 'axios';
|
import { AxiosError } from 'axios';
|
||||||
import { kratos } from '@/ory/sdk/kratos';
|
import { kratos } from '../../../ory/sdk/client';
|
||||||
import { useRouter, useSearchParams } from 'next/navigation';
|
import { useRouter, useSearchParams } from 'next/navigation';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { Skeleton } from '@/components/ui/skeleton';
|
import { Skeleton } from '@/components/ui/skeleton';
|
||||||
|
|
|
@ -5,7 +5,7 @@ import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/com
|
||||||
import { Flow, HandleError } from '@/ory';
|
import { Flow, HandleError } from '@/ory';
|
||||||
import { UpdateVerificationFlowBody, VerificationFlow } from '@ory/client';
|
import { UpdateVerificationFlowBody, VerificationFlow } from '@ory/client';
|
||||||
import { AxiosError } from 'axios';
|
import { AxiosError } from 'axios';
|
||||||
import { kratos } from '@/ory/sdk/kratos';
|
import { kratos } from '../../../ory/sdk/client';
|
||||||
import { useRouter, useSearchParams } from 'next/navigation';
|
import { useRouter, useSearchParams } from 'next/navigation';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { NextRequest, NextResponse } from 'next/server';
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
import { getFrontendApi } from '@/ory/sdk/hydra';
|
import { getFrontendApi } from 'ory/sdk/server';
|
||||||
import { cookies } from 'next/headers';
|
import { cookies } from 'next/headers';
|
||||||
|
|
||||||
export async function middleware(request: NextRequest) {
|
export async function middleware(request: NextRequest) {
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
import { AxiosError } from 'axios';
|
import { AxiosError } from 'axios';
|
||||||
import React, { DependencyList, useEffect, useState } from 'react';
|
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';
|
import { AppRouterInstance } from 'next/dist/shared/lib/app-router-context.shared-runtime';
|
||||||
|
|
||||||
export const HandleError = (
|
export const HandleError = (
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
export * from './hooks';
|
export * from './hooks';
|
||||||
export * from './ui';
|
export * from './ui';
|
||||||
export * from './sdk/kratos';
|
export * from './sdk/client';
|
||||||
|
|
|
@ -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,
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
));
|
|
||||||
}
|
|
39
authentication/src/ory/sdk/server/index.ts
Normal file
39
authentication/src/ory/sdk/server/index.ts
Normal 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;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue