Skip to content

Commit 31e0f9e

Browse files
Construct the Provider Select window via document.write()
1 parent 5a2c431 commit 31e0f9e

3 files changed

Lines changed: 84 additions & 5 deletions

File tree

demo/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ <h5>App body</h5>
8585
function loginTo (providerUri) {
8686
SolidClient.login(providerUri)
8787
.then(function (webId) {
88-
loginSuccess(webId)
88+
if (webId) { loginSuccess(webId) }
8989
})
9090
.catch(function (error) {
9191
console.error('Error logging in:', error)

src/index.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727
'use strict'
2828
const RelyingParty = require('oidc-rp')
29+
const providerSelectPopupSource = require('./provider-select-popup')
2930

3031
// URI parameter types
3132
const HASH = 'hash'
@@ -172,9 +173,11 @@ class ClientAuthOIDC {
172173
* on page load (in case the user is already authenticated), as well as
173174
* triggered when the user initiates login explicitly (such as by pressing
174175
* a Login button, etc).
176+
*
175177
* @param [providerUri] {string} Provider URI, result of a Provider Selection
176178
* operation (that the app developer has provided). If `null`, the
177179
* `selectProvider()` step will kick off its own UI for Provider Selection.
180+
*
178181
* @return {Promise<string>} Resolves to the logged in user's WebID URI
179182
*/
180183
login (providerUri) {
@@ -259,12 +262,18 @@ class ClientAuthOIDC {
259262
console.log('Getting provider from default popup UI')
260263
this.initEventListeners(this.window)
261264

262-
if (!this.selectProviderWindow) {
263-
this.selectProviderWindow = this.window.open('login_iframe_inline.html',
264-
'selectProviderWindow', 'menubar=no,resizable=yes,width=300,height=300')
265-
}
266265
if (this.selectProviderWindow) {
266+
// Popup has already been opened
267267
this.selectProviderWindow.focus()
268+
} else {
269+
// Open a new Provider Select popup window
270+
this.selectProviderWindow = this.window.open('',
271+
'selectProviderWindow',
272+
'menubar=no,resizable=yes,width=300,height=300'
273+
)
274+
275+
this.selectProviderWindow.document.write(providerSelectPopupSource)
276+
this.selectProviderWindow.document.close()
268277
}
269278
}
270279

src/provider-select-popup.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'use strict'
2+
3+
module.exports = `<!doctype html>
4+
<html lang="en">
5+
<head>
6+
<meta charset="utf-8">
7+
<meta http-equiv="Content-type" content="text/html;charset=utf-8">
8+
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
9+
10+
</head>
11+
<body>
12+
<div class="container">
13+
<div class="row">
14+
<div class="col-xs-12 text-center" style="margin-top: 3em;">
15+
Login with:
16+
</div>
17+
</div>
18+
<div class="row">
19+
<div class="col-xs-12 text-center" style="padding-top: 2em;">
20+
<button type="button" class="btn btn-md btn-primary" id="dbProvider">
21+
databox2.com
22+
</button>
23+
</div>
24+
</div>
25+
<div class="row">
26+
<div class="col-xs-12 text-center" style="padding-top: 2em;">
27+
or custom:<br />
28+
<input type="text" id="customProviderUri" value="https://" placeholder="databox.me" />
29+
<button type="button" class="btn btn-md" id="customProvider">Go</button>
30+
</div>
31+
</div>
32+
</div>
33+
<script type="text/javascript">
34+
window.addEventListener('load', function () { init() });
35+
36+
function init () {
37+
initEvents()
38+
}
39+
40+
function initButton(id, action) {
41+
document.getElementById(id).addEventListener('click', action)
42+
}
43+
44+
function initEvents () {
45+
initButton('dbProvider',
46+
function () { selectProvider('https://databox2.com') })
47+
initButton('customProvider',
48+
function () {
49+
var defaultValue = 'https://'
50+
var customUri = document.getElementById('customProviderUri')
51+
if (customUri && customUri.value !== defaultValue) {
52+
selectProvider(customUri.value)
53+
}
54+
})
55+
}
56+
57+
function selectProvider (providerUri) {
58+
console.log('Provider selected: ', providerUri)
59+
var message = {
60+
event_type: 'providerSelected',
61+
value: providerUri
62+
}
63+
console.log('opener.window.location: ', opener.window.location.href)
64+
65+
opener.postMessage(message, opener.window.location.origin)
66+
}
67+
</script>
68+
</body>
69+
</html>
70+
`

0 commit comments

Comments
 (0)