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:
parent
3b05405363
commit
f1cc58a651
48 changed files with 1713 additions and 0 deletions
43
dashboard/src/ory/hooks.tsx
Normal file
43
dashboard/src/ory/hooks.tsx
Normal 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);
|
||||
}
|
||||
};
|
||||
}
|
2
dashboard/src/ory/index.ts
Normal file
2
dashboard/src/ory/index.ts
Normal file
|
@ -0,0 +1,2 @@
|
|||
export * from './hooks';
|
||||
export * from './sdk/kratos';
|
15
dashboard/src/ory/sdk/hydra/index.ts
Normal file
15
dashboard/src/ory/sdk/hydra/index.ts
Normal 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,
|
||||
},
|
||||
}),
|
||||
));
|
||||
}
|
14
dashboard/src/ory/sdk/kratos/index.ts
Normal file
14
dashboard/src/ory/sdk/kratos/index.ts
Normal 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 };
|
Loading…
Add table
Add a link
Reference in a new issue