mirror of
https://codeberg.org/MarkusThielker/next-ory.git
synced 2025-07-03 05:19:18 +00:00
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>
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue