Skip to content

Commit 884aacd

Browse files
Refactor API to use login().then().catch()
1 parent 36a885a commit 884aacd

9 files changed

Lines changed: 686 additions & 144 deletions

File tree

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,25 @@ Connect.
99
This is an Authentication helper library that wraps an OpenID Connect (OIDC)
1010
Relying Party library, [`oidc-rp`](https://github.com/anvilresearch/oidc-rp).
1111
It is meant to be used in browser-side applications, as part of `solid-client`.
12+
13+
### Usage
14+
15+
```js
16+
// Using a standard "document loaded" event listener
17+
// (equivalent to jQuery's $(document).ready())
18+
// Trigger a login() check on page load, in case user is logged in already
19+
document.addEventListener('DOMContentLoaded', function () {
20+
SolidClient.auth.login()
21+
.then(function (webId) {
22+
// User is logged in, you can display their webId, load their profile, etc
23+
// Solid.auth.webId is set to the current user's webId URI
24+
// Also, SolidClient.auth.accessToken is set to the current user's access token
25+
})
26+
.catch(function (error) {
27+
// An error has occurred while logging in, display it to user
28+
})
29+
})
30+
```
31+
32+
Called by itself, `login()` will perform Provider Discovery and kick off the
33+
OAuth2/OpenID Connect `/authorize` process.

demo/.acl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
2+
3+
<#Public>
4+
a <http://www.w3.org/ns/auth/acl#Authorization> ;
5+
<http://www.w3.org/ns/auth/acl#accessTo> <.> ;
6+
<http://www.w3.org/ns/auth/acl#agentClass> <http://xmlns.com/foaf/0.1/Agent> ;
7+
<http://www.w3.org/ns/auth/acl#defaultForNew> <.> ;
8+
<http://www.w3.org/ns/auth/acl#mode> <http://www.w3.org/ns/auth/acl#Read> .

demo/index.html

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,16 @@ <h5>App body</h5>
5959
<script>
6060

6161
// Using a standard "document loaded" event listener
62-
// (equivalent to jQuery's $(document).ready()
62+
// (equivalent to jQuery's $(document).ready())
63+
// Trigger a login() check on page load, in case user is logged in already
6364
document.addEventListener('DOMContentLoaded', function () {
64-
// Init the solid auth helper, inject whatever behavior you want to happen
65-
// when authentication succeeds.
66-
SolidClient.auth.init({
67-
onLoginSuccess: function (webId, accessToken) {
65+
SolidClient.auth.login()
66+
.then(function (webId, accessToken) {
6867
loginSuccess(webId, accessToken)
69-
},
70-
onProviderSelected: function (providerUri) {
71-
providerSelected(providerUri)
72-
}
73-
})
68+
})
69+
.catch(function (error) {
70+
console.error('Error logging in:', error)
71+
})
7472
init()
7573
})
7674

@@ -81,9 +79,8 @@ <h5>App body</h5>
8179
function initButton(id, action) {
8280
document.getElementById(id).addEventListener('click', action)
8381
}
84-
function loginSuccess (webId, accessToken) {
85-
console.log('onLoginSuccess() callback! webId: ', webId,
86-
'access_token: ', accessToken)
82+
function loginSuccess (webId) {
83+
console.log('onLoginSuccess() callback! webId: ', webId)
8784
hide('noWebId')
8885
show('logoutDiv')
8986
setField('webId', webId)

demo/solid-client.min.js

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

karma.conf.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const webpackConfig = require('./webpack.config.js')
2+
webpackConfig.entry = {}
3+
4+
module.exports = function(config) {
5+
config.set({
6+
basePath: '',
7+
8+
frameworks: ['mocha', 'sinon-chai'],
9+
10+
client: {
11+
chai: {
12+
includeStack: true
13+
}
14+
},
15+
16+
files: [
17+
'dist/solid-auth-oidc.min.js',
18+
'test/unit/*-test.js'
19+
],
20+
21+
preprocessors: {
22+
// 'dist/solid-auth-oidc.min.js': ['webpack'],
23+
'./tests/**/*-test.js': ['babel']
24+
},
25+
26+
babelPreprocessor: {
27+
options: {
28+
presets: [ 'es2015' ],
29+
sourceMap: 'inline'
30+
}
31+
},
32+
33+
port: 9876,
34+
35+
// test results reporter to use
36+
// possible values: 'dots', 'progress'
37+
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
38+
reporters: ['progress'],
39+
40+
// start these browsers
41+
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
42+
browsers: ['Chrome'],
43+
44+
colors: true,
45+
46+
webpack: webpackConfig,
47+
48+
singleRun: true
49+
})
50+
}

package.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
"build-dist": "webpack --progress --colors --optimize-occurrence-order --optimize-dedupe",
1616
"build-lib": "babel src -d lib",
1717
"dist": "npm run build && npm run build-dist",
18+
"karma": "./node_modules/.bin/karma start",
1819
"postversion": "git push --follow-tags",
1920
"prepublish": "npm run build && npm run test",
2021
"preversion": "npm test",
2122
"standard": "standard src/*",
2223
"tape": "tape test/unit/*.js",
23-
"test": "npm run standard && npm run tape"
24+
"test": "npm run standard && npm run dist && npm run karma"
2425
},
2526
"repository": {
2627
"type": "git",
@@ -49,15 +50,23 @@
4950
},
5051
"homepage": "https://github.com/solid/solid-auth-oidc",
5152
"dependencies": {
52-
"oidc-rp": "^0.1.0"
53+
"oidc-rp": "git://github.com/anvilresearch/oidc-rp.git#lib"
5354
},
5455
"devDependencies": {
5556
"babel-cli": "^6.18.0",
5657
"babel-core": "^6.21.0",
5758
"babel-loader": "^6.2.5",
5859
"babel-preset-es2015": "^6.18.0",
60+
"karma": "^1.3.0",
61+
"karma-babel-preprocessor": "^6.0.1",
62+
"karma-chrome-launcher": "^2.0.0",
63+
"karma-mocha": "^1.3.0",
64+
"karma-sinon-chai": "^1.2.4",
65+
"karma-webpack": "^1.8.1",
66+
"mocha": "^3.2.0",
67+
"sinon": "^1.17.7",
68+
"sinon-chai": "^2.8.0",
5969
"standard": "^5.4.1",
60-
"tape": "^4.6.3",
6170
"webpack": "^1.14.0"
6271
},
6372
"standard": {

0 commit comments

Comments
 (0)