index.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Keycloak OIDC Demo</title>
  6. <link
  7. href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
  8. rel="stylesheet"
  9. integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk"
  10. crossorigin="anonymous"
  11. />
  12. <script src="http://localhost:9000/auth/js/keycloak.js"></script>
  13. <style>
  14. #logout.hidden {
  15. display: none;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div class="container">
  21. <h1>Keycloak Auth Demo</h1>
  22. <h2 class="mt-4">Welcome to << corporate intranet >></h2>
  23. <p class="lead">
  24. You are <span id="auth-status"></span>
  25. <button class="btn btn-sm btn-secondary hidden" id="logout">Log out of intranet</button>
  26. </p>
  27. <div class="text-center mt-4">
  28. <button id="login" class="btn btn-primary">
  29. Log In To Vendure
  30. </button>
  31. </div>
  32. </div>
  33. <script>
  34. const loginButton = document.querySelector('#login');
  35. const logoutButton = document.querySelector('#logout');
  36. const authStatus = document.querySelector('#auth-status');
  37. const keycloak = new Keycloak({
  38. realm: 'myrealm',
  39. 'auth-server-url': 'http://localhost:9000/auth/',
  40. 'ssl-required': 'external',
  41. resource: 'vendure',
  42. 'public-client': true,
  43. 'confidential-port': 0,
  44. clientId: 'vendure',
  45. flow: 'standard',
  46. });
  47. keycloak
  48. .init({
  49. onLoad: 'check-sso',
  50. })
  51. .then(function (authenticated) {
  52. console.log('authenticated', authenticated);
  53. authStatus.innerHTML = authenticated ? `logged in` : `not logged in`;
  54. if (authenticated) {
  55. if (-1 < window.location.search.indexOf('loginToVendure')) {
  56. loginToAdminUi();
  57. }
  58. logoutButton.classList.remove('hidden');
  59. } else {
  60. logoutButton.classList.add('hidden');
  61. }
  62. logoutButton.addEventListener('click', () => {
  63. keycloak.logout();
  64. });
  65. loginButton.addEventListener('click', () => {
  66. if (authenticated) {
  67. loginToAdminUi();
  68. } else {
  69. keycloak.login({
  70. redirectUri: window.location.href + '?loginToVendure=true',
  71. });
  72. }
  73. });
  74. })
  75. .catch(function () {
  76. alert('failed to initialize');
  77. });
  78. function loginToAdminUi() {
  79. return graphQlQuery(
  80. `
  81. mutation Authenticate($token: String!) {
  82. authenticate(input: {
  83. keycloak: {
  84. token: $token
  85. }
  86. }) {
  87. user { id }
  88. }
  89. }
  90. `,
  91. { token: keycloak.token },
  92. )
  93. .then((result) => {
  94. console.log(result);
  95. if (result.data?.authenticate.user) {
  96. // successfully authenticated
  97. window.location.replace('http://localhost:3000/admin');
  98. }
  99. })
  100. .catch((err) => {
  101. console.log('error', err);
  102. });
  103. }
  104. function graphQlQuery(query, variables) {
  105. return fetch('http://localhost:3000/admin-api', {
  106. method: 'POST',
  107. headers: {
  108. 'Content-Type': 'application/json',
  109. Accept: 'application/json',
  110. },
  111. body: JSON.stringify({ query, variables }),
  112. }).then((r) => {
  113. return r.json();
  114. });
  115. }
  116. </script>
  117. </body>
  118. </html>