1+ let connection = null ;
2+
3+ const drLib = {
4+ showLoginButton
5+
6+ }
7+
8+ function showLoginButton ( loginSpanId , stateChangeCallBack ) {
9+
10+ const authSettings = {
11+ spanButtonID : loginSpanId , // div id the DOM that will be replaced by the Service specific button
12+ onStateChange : pryvAuthStateChange , // event Listener for Authentication steps
13+ authRequest : { // See: https://api.pryv.com/reference/#auth-request
14+ requestingAppId : 'demo-dr-form-dr' , // to customize for your own app
15+ requestedPermissions : [
16+ {
17+ streamId : '*' ,
18+ level : 'manage'
19+ }
20+ ] ,
21+ clientData : {
22+ 'app-web-auth:description' : {
23+ 'type' : 'note/txt' ,
24+ 'content' : 'This app allows to send invitation links to patients and visualize and export answers.'
25+ }
26+ } ,
27+ }
28+ } ;
29+
30+ // following the APP GUIDELINES: https://api.pryv.com/guides/app-guidelines/
31+ const serviceInfoUrl = Pryv . Browser . serviceInfoFromUrl ( ) || 'https://demo.datasafe.dev/reg/service/info' ;
32+ Pryv . Browser . setupAuth ( authSettings , serviceInfoUrl ) ;
33+
34+ async function pryvAuthStateChange ( state ) { // called each time the authentication state changes
35+ console . log ( '##pryvAuthStateChange' , state ) ;
36+ if ( state . id === Pryv . Browser . AuthStates . AUTHORIZED ) {
37+ connection = new Pryv . Connection ( state . apiEndpoint ) ;
38+ await initPatientAccount ( connection ) ;
39+ stateChangeCallBack ( 'loggedIN' ) ;
40+ }
41+ if ( state . id === Pryv . Browser . AuthStates . INITIALIZED ) {
42+ connection = null ;
43+ stateChangeCallBack ( 'loggedOUT' ) ;
44+ }
45+ }
46+ }
47+
48+ async function initPatientAccount ( connection ) {
49+ // create stream structure (even if already exists)
50+ const apiCalls = [
51+ {
52+ method : 'streams.create' ,
53+ params : {
54+ id : 'patients' ,
55+ name : 'Patients'
56+ }
57+ } ,
58+ {
59+ method : 'streams.create' ,
60+ params : {
61+ id : 'patients-inbox' ,
62+ name : 'Patients Inbox' ,
63+ parentId : 'patients'
64+ }
65+ } ,
66+ {
67+ method : 'streams.create' ,
68+ params : {
69+ id : 'patients-validated' ,
70+ name : 'Patients Validted' ,
71+ parentId : 'patients'
72+ }
73+ } ,
74+ {
75+ method : 'streams.create' ,
76+ params : {
77+ id : 'demo-dr-forms' ,
78+ name : 'Demo Dr Forms'
79+ }
80+ } ,
81+ {
82+ method : 'streams.create' ,
83+ params : {
84+ id : 'demo-dr-forms-questionary-x' ,
85+ name : 'Questionnary x'
86+ }
87+ }
88+ ] ;
89+ const result = await connection . api ( apiCalls ) ;
90+ console . log ( result ) ;
91+ console . log ( '## Dr account initialized' )
92+ }
0 commit comments