index.html 4.7 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/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">Log In To Vendure</button>
  29. </div>
  30. </div>
  31. <script>
  32. const loginButton = document.querySelector('#login');
  33. const logoutButton = document.querySelector('#logout');
  34. const authStatus = document.querySelector('#auth-status');
  35. const keycloak = new Keycloak({
  36. realm: 'myrealm',
  37. 'auth-server-url': 'http://localhost:9000/auth/',
  38. 'ssl-required': 'external',
  39. resource: 'vendure',
  40. 'public-client': true,
  41. 'confidential-port': 0,
  42. clientId: 'vendure',
  43. flow: 'standard',
  44. });
  45. keycloak
  46. .init({
  47. onLoad: 'check-sso',
  48. })
  49. .then(function (authenticated) {
  50. console.log('authenticated', authenticated);
  51. authStatus.innerHTML = authenticated ? `logged in` : `not logged in`;
  52. if (authenticated) {
  53. if (-1 < window.location.search.indexOf('loginToVendure')) {
  54. loginToAdminUi();
  55. }
  56. logoutButton.classList.remove('hidden');
  57. } else {
  58. logoutButton.classList.add('hidden');
  59. }
  60. logoutButton.addEventListener('click', () => {
  61. keycloak.logout();
  62. });
  63. loginButton.addEventListener('click', () => {
  64. if (authenticated) {
  65. loginToAdminUi();
  66. } else {
  67. keycloak.login({
  68. redirectUri: window.location.href + '?loginToVendure=true',
  69. });
  70. }
  71. });
  72. })
  73. .catch(function () {
  74. alert('failed to initialize');
  75. });
  76. function loginToAdminUi() {
  77. return graphQlQuery(
  78. /* GraphQL */ `
  79. mutation Authenticate($token: String!) {
  80. authenticate(input: { keycloak: { token: $token } }) {
  81. ... on CurrentUser {
  82. id
  83. }
  84. ... on ErrorResult {
  85. errorCode
  86. message
  87. }
  88. }
  89. }
  90. `,
  91. { token: keycloak.token },
  92. )
  93. .then(result => {
  94. console.log(result);
  95. if (result.data?.authenticate.id) {
  96. // successfully authenticated
  97. window.location.replace('http://localhost:4200/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>