Skip to content

Commit

Permalink
feat: complete the logout URL
Browse files Browse the repository at this point in the history
  • Loading branch information
meysam81 committed May 17, 2024
1 parent ab4ddeb commit ee367e4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<li><a href="/verify">Verify</a></li>
<li><a href="/recovery">Recovery</a></li>
<li><a href="/settings">Settings</a></li>
<li><a href="/logout">Logout</a></li>
</ul>
</nav>
</header>
Expand Down
19 changes: 19 additions & 0 deletions frontend/src/logout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createFlowForm, getFlowInfo, initFlow, whoami } from "./utils.js";

async function createForm() {
var flowInfo;

var extraHeaders = {
Accept: "application/json",
};

flowInfo = await initFlow("logout", extraHeaders);

var flowJson = await flowInfo.json();

await fetch(flowJson.logout_url, { credentials: "include" });

window.location.href = "/login";
}

export default createForm;
5 changes: 5 additions & 0 deletions frontend/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import RegisterForm from "./register.js";
import VerifyForm from "./verify.js";
import RecoveryForm from "./recovery.js";
import SettingsForm from "./settings.js";
import LogOutForm from "./logout.js";

const Router = {
init: async function init_() {
Expand Down Expand Up @@ -55,6 +56,10 @@ const Router = {
document.title = "Settings - Developer Friendly";
pageElement = await SettingsForm();
break;
case route.startsWith("/logout"):
document.title = "Log Out - Developer Friendly";
pageElement = await LogOutForm();
break;
default:
document.title = "Developer Friendly";
pageElement = document.createElement("h1");
Expand Down
19 changes: 15 additions & 4 deletions frontend/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@ export async function whoami() {
return await fetch(`${kratosHost}/sessions/whoami`, fetchOptions);
}

export async function getFlowInfo(flow, flowId) {
export async function getFlowInfo(flow, flowId, extraHeaders = {}) {
switch (flow) {
case "login":
case "registration":
case "verification":
case "recovery":
case "settings":
case "logout":
return await fetch(
`${kratosHost}/self-service/${flow}/flows?id=${flowId}`,
fetchOptions
{
...fetchOptions,
headers: {
...extraHeaders,
},
}
);
default:
console.error("Unknown flow type", flow);
Expand Down Expand Up @@ -101,10 +107,15 @@ export function createFlowForm(flowInfo, submitLabel = "Submit") {
return form;
}

export async function initFlow(flow) {
export async function initFlow(flow, extraHeaders = {}) {
return await fetch(
`${kratosHost}/self-service/${flow}/browser`,
fetchOptions
{
...fetchOptions,
headers: {
...extraHeaders,
},
}
);
}

Expand Down
3 changes: 3 additions & 0 deletions kratos/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ serve:
cors:
allowed_origins:
- http://localhost:8080
allowed_headers:
- content-type
- Accept
options_passthrough: true
max_age: 0
debug: true
Expand Down

0 comments on commit ee367e4

Please sign in to comment.