Skip to content

Commit b8defc3

Browse files
dan-fRubenVerborgh
authored andcommitted
Add login button to homepage
1 parent 6637b72 commit b8defc3

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

common/js/solid-auth-client.bundle.js

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/js/solid-auth-client.bundle.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

default-templates/new-account/index.html

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1">
66
<title>{{#if name}}{{name}} &ndash; {{/if}}Solid Home</title>
77
<link rel="stylesheet" href="/common/css/bootstrap.min.css">
8+
<script id="solid-auth-client" src="/common/js/solid-auth-client.bundle.js" defer></script>
9+
<style>
10+
.session-action {
11+
margin-right: 15px;
12+
}
13+
</style>
814
</head>
915
<body>
1016
<div class="container">
@@ -13,6 +19,9 @@
1319
<div class="navbar-header">
1420
<a href="/" class="navbar-brand">{{#if name}}{{name}} &ndash; {{/if}}Solid Home</a>
1521
</div>
22+
<div class="navbar-right">
23+
<button id="session-action" type="button" class="session-action btn btn-default navbar-btn">Loading...</button>
24+
</div>
1625
</div>
1726
</nav>
1827
</div>
@@ -67,4 +76,63 @@ <h1>Apps</h1>
6776
</section>
6877
</div>
6978
</body>
79+
<script type="text/javascript">
80+
const popupUri = window.location.origin + '/common/popup.html'
81+
const stateData = {
82+
session: null
83+
}
84+
const state = new Proxy(stateData, {
85+
set: (obj, prop, value) => {
86+
obj[prop] = value
87+
render()
88+
}
89+
})
90+
91+
function main() {
92+
// Render the session state
93+
SolidAuthClient
94+
.currentSession()
95+
.then(saveSession)
96+
.then(render)
97+
98+
// Add event listeners
99+
const actionButton = sessionActionButton()
100+
actionButton.addEventListener('click', takeSessionAction)
101+
}
102+
103+
function saveSession(session) {
104+
state.session = session
105+
return session
106+
}
107+
108+
function takeSessionAction() {
109+
const { session } = state
110+
const loggedIn = !!session
111+
if (loggedIn) {
112+
SolidAuthClient
113+
.logout()
114+
.then(saveSession)
115+
} else {
116+
SolidAuthClient
117+
.popupLogin({ popupUri })
118+
.then(saveSession)
119+
}
120+
}
121+
122+
function render() {
123+
const { session } = state
124+
const actionButton = sessionActionButton()
125+
if (!session) {
126+
actionButton.innerText = 'Log in'
127+
} else {
128+
actionButton.innerText = 'Log out'
129+
}
130+
}
131+
132+
function sessionActionButton() {
133+
return document.getElementById('session-action')
134+
}
135+
136+
main()
137+
</script>
70138
</html>

0 commit comments

Comments
 (0)