diff --git a/packages/dashboard/package.json b/packages/dashboard/package.json index 61cde58d..26bbc059 100644 --- a/packages/dashboard/package.json +++ b/packages/dashboard/package.json @@ -12,6 +12,7 @@ "@ory/kratos-client": "^0.5.4-alpha.1", "@tailwindcss/forms": "^0.2.1", "autoprefixer": "^10.2.4", + "axios": "^0.21.1", "classnames": "^2.2.6", "dayjs": "^1.10.4", "express-jwt": "^6.0.0", diff --git a/packages/dashboard/src/components/Layout.js b/packages/dashboard/src/components/Layout.js index 31c1feae..76c1f800 100644 --- a/packages/dashboard/src/components/Layout.js +++ b/packages/dashboard/src/components/Layout.js @@ -1,15 +1,25 @@ import Link from "next/link"; import { useRouter } from "next/router"; import Head from "next/head"; +import axios from "axios"; import { useState } from "react"; import config from "../../src/config"; -const signOutUrl = `${config.kratos.browser}/self-service/browser/flows/logout`; - export default function Layout({ title, children }) { const [menuOpen, openMenu] = useState(false); const [avatarDropdownOpen, openAvatarDropdown] = useState(false); const router = useRouter(); + const handleSignOut = async (e) => { + e.preventDefault(); + + try { + await axios.post("/logout"); + + window.location = `${config.kratos.browser}/self-service/browser/flows/logout`; + } catch (error) { + console.log(error); // todo: handle errors + } + }; return (
@@ -132,9 +142,9 @@ export default function Layout({ title, children }) { Sign out @@ -239,7 +249,7 @@ export default function Layout({ title, children }) { Sign out diff --git a/packages/dashboard/src/pages/api/accounts/authenticate.js b/packages/dashboard/src/pages/api/accounts/login.js similarity index 85% rename from packages/dashboard/src/pages/api/accounts/authenticate.js rename to packages/dashboard/src/pages/api/accounts/login.js index 55c8266b..4890d52b 100644 --- a/packages/dashboard/src/pages/api/accounts/authenticate.js +++ b/packages/dashboard/src/pages/api/accounts/login.js @@ -3,11 +3,11 @@ import superagent from "superagent"; export default async (req, res) => { if (req.cookies.ory_kratos_session) { try { - const auth = await superagent + const { header } = await superagent .post("http://oathkeeper:4455/login") .set("cookie", `ory_kratos_session=${req.cookies.ory_kratos_session}`); - res.setHeader("Set-Cookie", auth.header["set-cookie"]); + res.setHeader("Set-Cookie", header["set-cookie"]); res.redirect(req.query.return_to ?? "/"); } catch (error) { // credentials were correct but accounts service failed diff --git a/packages/dashboard/src/pages/auth/login.js b/packages/dashboard/src/pages/auth/login.js index bb307f97..d553ab40 100644 --- a/packages/dashboard/src/pages/auth/login.js +++ b/packages/dashboard/src/pages/auth/login.js @@ -8,7 +8,7 @@ const kratos = new PublicApi(new Configuration({ basePath: config.kratos.public export async function getServerSideProps(context) { const flow = context.query.flow; - const redirect = encodeURIComponent(`/api/accounts/authenticate?return_to=${context.query.return_to ?? "/"}`); + const redirect = encodeURIComponent(`/api/accounts/login?return_to=${context.query.return_to ?? "/"}`); if (process.env.NODE_ENV === "development") { return { props: { flow: require("../../../stubs/login.json") } };