|
13 | 13 | <div class="pull-right"> |
14 | 14 | <a href="/register" class="btn btn-primary">Register</a> |
15 | 15 |
|
16 | | - <a onclick="login()" class="btn btn-success">Login</a> |
| 16 | + <button id="session-action" type="button" class="session-action btn btn-default">Loading...</button> |
17 | 17 | </div> |
18 | 18 |
|
19 | 19 | <h1>Welcome to the Solid Prototype</h1> |
@@ -51,33 +51,35 @@ <h2>Server info</h2> |
51 | 51 | </div> |
52 | 52 | <script src="/common/js/solid-auth-client.bundle.js"></script> |
53 | 53 | <script> |
54 | | - const elements = {}; |
55 | | - ['loggedIn', 'profileLink'].forEach(id => { |
56 | | - elements[id] = document.getElementById(id) |
57 | | - }) |
| 54 | + (function () { |
| 55 | + const elements = {}; |
| 56 | + ['loggedIn', 'profileLink'].forEach(id => { |
| 57 | + elements[id] = document.getElementById(id) |
| 58 | + }) |
| 59 | + var button = document.getElementById('session-action') |
| 60 | + var loggedIn = false |
58 | 61 |
|
59 | | - async function login () { |
60 | | - const session = await solid.auth.popupLogin() |
61 | | - if (session) { |
62 | | - // Make authenticated request to the server to establish a session cookie |
63 | | - const {status} = await solid.auth.fetch(location) |
64 | | - if (status === 401) { |
65 | | - alert(`Invalid login.`) |
66 | | - await solid.auth.logout() |
| 62 | + solid.auth.trackSession(async session => { |
| 63 | + loggedIn = !!session; |
| 64 | + button.innerText = loggedIn ? 'Log out' : 'Log in'; |
| 65 | + button.classList.remove('btn-default'); |
| 66 | + button.classList.remove('btn-danger'); |
| 67 | + button.classList.remove('btn-success'); |
| 68 | + button.classList.add(loggedIn ? 'btn-danger' : 'btn-success'); |
| 69 | + if (!session) { |
| 70 | + elements.loggedIn.classList.add('hidden') |
67 | 71 | } |
68 | | - } |
69 | | - } |
| 72 | + else { |
| 73 | + elements.loggedIn.classList.remove('hidden') |
| 74 | + elements.profileLink.innerText = session.webId |
| 75 | + elements.profileLink.href = session.webId |
| 76 | + } |
| 77 | + }) |
70 | 78 |
|
71 | | - solid.auth.trackSession(async session => { |
72 | | - if (!session) { |
73 | | - elements.loggedIn.classList.add('hidden') |
74 | | - } |
75 | | - else { |
76 | | - elements.loggedIn.classList.remove('hidden') |
77 | | - elements.profileLink.innerText = session.webId |
78 | | - elements.profileLink.href = session.webId |
79 | | - } |
80 | | - }) |
| 79 | + button.addEventListener('click', function () { |
| 80 | + loggedIn ? solid.auth.logout() : solid.auth.popupLogin() |
| 81 | + }) |
| 82 | + })(); |
81 | 83 | </script> |
82 | 84 | </body> |
83 | 85 | </html> |
0 commit comments