This commit is contained in:
Karol Wypchlo 2021-02-16 12:19:25 +01:00
parent dde2233d16
commit c6b180c44a
4 changed files with 18 additions and 7 deletions

View File

@ -12,6 +12,7 @@
"@ory/kratos-client": "^0.5.4-alpha.1", "@ory/kratos-client": "^0.5.4-alpha.1",
"@tailwindcss/forms": "^0.2.1", "@tailwindcss/forms": "^0.2.1",
"autoprefixer": "^10.2.4", "autoprefixer": "^10.2.4",
"axios": "^0.21.1",
"classnames": "^2.2.6", "classnames": "^2.2.6",
"dayjs": "^1.10.4", "dayjs": "^1.10.4",
"express-jwt": "^6.0.0", "express-jwt": "^6.0.0",

View File

@ -1,15 +1,25 @@
import Link from "next/link"; import Link from "next/link";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import Head from "next/head"; import Head from "next/head";
import axios from "axios";
import { useState } from "react"; import { useState } from "react";
import config from "../../src/config"; import config from "../../src/config";
const signOutUrl = `${config.kratos.browser}/self-service/browser/flows/logout`;
export default function Layout({ title, children }) { export default function Layout({ title, children }) {
const [menuOpen, openMenu] = useState(false); const [menuOpen, openMenu] = useState(false);
const [avatarDropdownOpen, openAvatarDropdown] = useState(false); const [avatarDropdownOpen, openAvatarDropdown] = useState(false);
const router = useRouter(); 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 ( return (
<div> <div>
@ -132,9 +142,9 @@ export default function Layout({ title, children }) {
</a> </a>
</Link> </Link>
<a <a
href={signOutUrl}
className="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100" className="block px-4 py-2 text-sm text-gray-700 hover:bg-gray-100"
role="menuitem" role="menuitem"
onClick={handleSignOut}
> >
Sign out Sign out
</a> </a>
@ -239,7 +249,7 @@ export default function Layout({ title, children }) {
</a> </a>
</Link> </Link>
<a <a
href={signOutUrl} onClick={handleSignOut}
className="block px-3 py-2 rounded-md text-base font-medium text-gray-400 hover:text-white hover:bg-gray-700" className="block px-3 py-2 rounded-md text-base font-medium text-gray-400 hover:text-white hover:bg-gray-700"
> >
Sign out Sign out

View File

@ -3,11 +3,11 @@ import superagent from "superagent";
export default async (req, res) => { export default async (req, res) => {
if (req.cookies.ory_kratos_session) { if (req.cookies.ory_kratos_session) {
try { try {
const auth = await superagent const { header } = await superagent
.post("http://oathkeeper:4455/login") .post("http://oathkeeper:4455/login")
.set("cookie", `ory_kratos_session=${req.cookies.ory_kratos_session}`); .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 ?? "/"); res.redirect(req.query.return_to ?? "/");
} catch (error) { } catch (error) {
// credentials were correct but accounts service failed // credentials were correct but accounts service failed

View File

@ -8,7 +8,7 @@ const kratos = new PublicApi(new Configuration({ basePath: config.kratos.public
export async function getServerSideProps(context) { export async function getServerSideProps(context) {
const flow = context.query.flow; 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") { if (process.env.NODE_ENV === "development") {
return { props: { flow: require("../../../stubs/login.json") } }; return { props: { flow: require("../../../stubs/login.json") } };