This repository has been archived by the owner on May 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
did-component.js
62 lines (59 loc) · 1.69 KB
/
did-component.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*!
* Copyright (c) 2016-2017 Digital Bazaar, Inc. All rights reserved.
*/
/* global navigator */
export default {
bindings: {
sysIdentifier: '@brIdentity',
onLogin: '&brOnLogin',
onNotRegistered: '&?brOnNotRegistered'
},
controller: Ctrl,
templateUrl: 'bedrock-angular-authn-did/did-component.html'
};
/* @ngInject */
function Ctrl($q, $scope, brAlertService, brDidService, config) {
var self = this;
self.loading = false;
$scope.$on('brAuthnDid.login', function() {
self.login();
});
self.login = function() {
self.loading = true;
const credentialQuery = {
web: {
VerifiableProfile: {
'@context': 'https://w3id.org/identity/v1',
id: self.sysIdentifier || '',
publicKey: ''
},
mediatorUrl: config.data['authorization-io'].baseUri
}
};
if(self.onNotRegistered) {
// TODO: reconsider design for this feature
credentialQuery.web.enableRegistration = true;
}
$q.resolve(navigator.credentials.get(credentialQuery))
.then(function(credential) {
if(credential === null) {
return $q.reject(new Error('Login canceled.'));
}
return brDidService.login(credential.data);
}).catch(function(err) {
if(self.onNotRegistered && err.message === 'NotRegisteredError') {
return self.onNotRegistered();
}
return $q.reject(err);
}).then(function(identity) {
if(!identity) {
return;
}
return self.onLogin({identity: identity});
}).catch(function(err) {
brAlertService.add('error', err, {scope: $scope});
}).then(function() {
self.loading = false;
});
};
}