1
0
Fork 0
mirror of https://codeberg.org/MarkusThielker/next-ory.git synced 2025-07-04 13:59:17 +00:00

NORY-7: add dashboard application with basic authentication routing

This commit is contained in:
Markus Thielker 2024-11-28 23:48:45 +01:00
parent 3b05405363
commit f1cc58a651
No known key found for this signature in database
48 changed files with 1713 additions and 0 deletions

View file

@ -0,0 +1,43 @@
'use client';
import { AxiosError } from 'axios';
import { DependencyList, useEffect, useState } from 'react';
import { kratos } from './sdk/kratos';
// Returns a function which will log the user out
export function LogoutLink(deps?: DependencyList) {
const [logoutToken, setLogoutToken] = useState<string>('');
useEffect(() => {
kratos
.createBrowserLogoutFlow()
.then(({ data }) => {
setLogoutToken(data.logout_token);
})
.catch((err: AxiosError) => {
switch (err.response?.status) {
case 401:
// do nothing, the user is not logged in
return;
}
// Something else happened!
return Promise.reject(err);
});
}, deps);
return () => {
if (logoutToken) {
const url = process.env.NEXT_PUBLIC_AUTHENTICATION_NODE_URL +
'/flow/login?return_to=' +
process.env.NEXT_PUBLIC_DASHBOARD_NODE_URL;
kratos
.updateLogoutFlow({ token: logoutToken })
.then(() => window.location.href = url);
}
};
}

View file

@ -0,0 +1,2 @@
export * from './hooks';
export * from './sdk/kratos';

View file

@ -0,0 +1,15 @@
'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,14 @@
'use client';
import { Configuration, FrontendApi } from '@ory/client';
const kratos = new FrontendApi(
new Configuration({
basePath: process.env.NEXT_PUBLIC_ORY_KRATOS_URL,
baseOptions: {
withCredentials: true,
},
}),
);
export { kratos };