From 165998cf1cca3baef43b6e589cbd881de4a9254e Mon Sep 17 00:00:00 2001
From: Luka Radenovic <luka@init.hr>
Date: Mon, 6 Jun 2022 12:14:30 +0200
Subject: [PATCH] Dashboard logout should trigger OAuth logout endpoint

---
 src/common/util/domain.ts        |  3 +++
 src/common/util/index.ts         |  5 +++--
 src/components/Header/Header.tsx | 20 ++++++++++++++++++++
 3 files changed, 26 insertions(+), 2 deletions(-)
 create mode 100644 src/common/util/domain.ts

diff --git a/src/common/util/domain.ts b/src/common/util/domain.ts
new file mode 100644
index 00000000..7152af58
--- /dev/null
+++ b/src/common/util/domain.ts
@@ -0,0 +1,3 @@
+export function getDomainName(hostName: string) {
+  return hostName.substring(hostName.lastIndexOf('.', hostName.lastIndexOf('.') - 1) + 1);
+}
diff --git a/src/common/util/index.ts b/src/common/util/index.ts
index 40a831a5..da954030 100644
--- a/src/common/util/index.ts
+++ b/src/common/util/index.ts
@@ -1,2 +1,3 @@
-export { isTouched } from './is-touched';
-export { addParamsToLink } from './add-params-to-link';
+export * from './is-touched';
+export * from './add-params-to-link';
+export * from './domain';
diff --git a/src/components/Header/Header.tsx b/src/components/Header/Header.tsx
index a58e2d82..facd338e 100644
--- a/src/components/Header/Header.tsx
+++ b/src/components/Header/Header.tsx
@@ -2,6 +2,7 @@ import React, { Fragment, useState } from 'react';
 import { Disclosure, Menu, Transition } from '@headlessui/react';
 import { MenuIcon, XIcon } from '@heroicons/react/outline';
 import { useAuth } from 'src/services/auth';
+import { getDomainName } from 'src/common/util';
 import Gravatar from 'react-gravatar';
 import { Link, useLocation } from 'react-router-dom';
 import clsx from 'clsx';
@@ -31,6 +32,24 @@ const Header: React.FC<HeaderProps> = () => {
   const [currentUserModal, setCurrentUserModal] = useState(false);
   const { logOut, currentUser, isAdmin } = useAuth();
 
+  const singOutUrl = () => {
+    const { hostname } = window.location;
+    const domain = getDomainName(window.location.hostname);
+    let url = `https://sso.${domain}/oauth2/sessions/logout`;
+
+    // This is a fix so it can work with dashboard.init.stackspin.net
+    if (hostname.includes('init')) {
+      url = `https://sso.init.${domain}/oauth2/sessions/logout`;
+    }
+
+    // This is a fix so it can work locally
+    if (hostname.includes('localhost')) {
+      url = 'https://sso.init.stackspin.net/oauth2/sessions/logout';
+    }
+
+    return url;
+  };
+
   const { pathname } = useLocation();
 
   const currentUserModalOpen = () => {
@@ -120,6 +139,7 @@ const Header: React.FC<HeaderProps> = () => {
                           {({ active }) => (
                             <a
                               onClick={() => logOut()}
+                              href={singOutUrl()}
                               className={classNames(
                                 active ? 'bg-gray-100 cursor-pointer' : '',
                                 'block px-4 py-2 text-sm text-gray-700 cursor-pointer',
-- 
GitLab