NORY-16: check if session exists in middleware (dashboard)
This commit is contained in:
parent
24f7d0936f
commit
531f0453e7
3 changed files with 54 additions and 42 deletions
|
@ -1,52 +1,15 @@
|
|||
'use client';
|
||||
'use server';
|
||||
|
||||
import React, { useCallback, useEffect, useState } from 'react';
|
||||
import { kratos, LogoutLink } from '@/ory';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import React from 'react';
|
||||
import { ThemeToggle } from '@/components/themeToggle';
|
||||
import { Session } from '@ory/client';
|
||||
import { LogOut } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
|
||||
export default function Page() {
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
const [session, setSession] = useState<Session>();
|
||||
const loadSession = useCallback(async () => {
|
||||
console.log(kratos.toSession());
|
||||
return kratos.toSession();
|
||||
}, [router]);
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
if (session) {
|
||||
return;
|
||||
}
|
||||
|
||||
loadSession()
|
||||
.then((response) => {
|
||||
console.log(response.data);
|
||||
response.data && setSession(response.data);
|
||||
})
|
||||
.catch(() => {
|
||||
const authentication_url = process.env.NEXT_PUBLIC_AUTHENTICATION_NODE_URL;
|
||||
const dashboard_url = process.env.NEXT_PUBLIC_AUTHENTICATION_NODE_URL;
|
||||
authentication_url && dashboard_url &&
|
||||
router.push(authentication_url + '/flow/login?return_to=' + dashboard_url);
|
||||
});
|
||||
|
||||
}, [router, session]);
|
||||
|
||||
const onLogout = LogoutLink();
|
||||
import { LogoutButton } from '@/components/auth/logout';
|
||||
|
||||
export default async function Page() {
|
||||
return (
|
||||
<div className="flex flex-col min-h-screen items-center text-3xl relative space-y-4">
|
||||
<div className="absolute flex flex-row w-fit items-center space-x-4 top-4 right-4">
|
||||
<ThemeToggle/>
|
||||
<Button variant="outline" size="icon" onClick={onLogout}>
|
||||
<LogOut className="h-[1.2rem] w-[1.2rem]"/>
|
||||
</Button>
|
||||
<LogoutButton/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
|
16
dashboard/src/components/auth/logout.tsx
Normal file
16
dashboard/src/components/auth/logout.tsx
Normal file
|
@ -0,0 +1,16 @@
|
|||
'use client';
|
||||
|
||||
import { LogOut } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { LogoutLink } from '@/ory';
|
||||
|
||||
export function LogoutButton() {
|
||||
|
||||
const onLogout = LogoutLink();
|
||||
|
||||
return (
|
||||
<Button variant="outline" size="icon" onClick={onLogout}>
|
||||
<LogOut className="h-[1.2rem] w-[1.2rem]"/>
|
||||
</Button>
|
||||
);
|
||||
}
|
33
dashboard/src/middleware.ts
Normal file
33
dashboard/src/middleware.ts
Normal file
|
@ -0,0 +1,33 @@
|
|||
import { NextResponse } from 'next/server';
|
||||
import { getFrontendApi } from '@/ory/sdk/hydra';
|
||||
import { cookies } from 'next/headers';
|
||||
|
||||
export async function middleware() {
|
||||
|
||||
const api = await getFrontendApi();
|
||||
const cookie = await cookies();
|
||||
|
||||
const session = await api
|
||||
.toSession({ cookie: 'ory_kratos_session=' + cookie.get('ory_kratos_session')?.value })
|
||||
.then((response) => response.data)
|
||||
.catch(() => null);
|
||||
|
||||
if (!session) {
|
||||
|
||||
console.log('NO SESSION');
|
||||
|
||||
const url = process.env.NEXT_PUBLIC_AUTHENTICATION_NODE_URL +
|
||||
'/flow/login?return_to=' +
|
||||
process.env.NEXT_PUBLIC_DASHBOARD_NODE_URL;
|
||||
|
||||
console.log('REDIRECT TO', url);
|
||||
|
||||
return NextResponse.redirect(url);
|
||||
}
|
||||
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: '/((?!api|_next/static|_next/image|favicon.png|sitemap.xml|robots.txt).*)',
|
||||
};
|
Loading…
Add table
Reference in a new issue