1313 < div class ="pull-right ">
1414 < a href ="/register " class ="btn btn-primary "> Register</ a >
1515
16- < a onclick =" login() " class ="btn btn-success " > Login </ a >
16+ < button id =" session-action " type =" button " class ="session-action btn btn-default " > Loading... </ button >
1717 </ div >
1818
1919 < h1 > Welcome to the Solid Prototype</ h1 >
@@ -34,33 +34,35 @@ <h1>Welcome to the Solid Prototype</h1>
3434</ div >
3535< script src ="/common/js/solid-auth-client.bundle.js "> </ script >
3636< script >
37- const elements = { } ;
38- [ 'loggedIn' , 'profileLink' ] . forEach ( id => {
39- elements [ id ] = document . getElementById ( id )
40- } )
37+ ( function ( ) {
38+ const elements = { } ;
39+ [ 'loggedIn' , 'profileLink' ] . forEach ( id => {
40+ elements [ id ] = document . getElementById ( id )
41+ } )
42+ var button = document . getElementById ( 'session-action' )
43+ var loggedIn = false
4144
42- async function login ( ) {
43- const session = await solid . auth . popupLogin ( )
44- if ( session ) {
45- // Make authenticated request to the server to establish a session cookie
46- const { status} = await solid . auth . fetch ( location )
47- if ( status === 401 ) {
48- alert ( `Invalid login.` )
49- await solid . auth . logout ( )
45+ solid . auth . trackSession ( async session => {
46+ loggedIn = ! ! session ;
47+ button . innerText = loggedIn ? 'Log out' : 'Log in' ;
48+ button . classList . remove ( 'btn-default' ) ;
49+ button . classList . remove ( 'btn-danger' ) ;
50+ button . classList . remove ( 'btn-success' ) ;
51+ button . classList . add ( loggedIn ? 'btn-danger' : 'btn-success' ) ;
52+ if ( ! session ) {
53+ elements . loggedIn . classList . add ( 'hidden' )
5054 }
51- }
52- }
55+ else {
56+ elements . loggedIn . classList . remove ( 'hidden' )
57+ elements . profileLink . innerText = session . webId
58+ elements . profileLink . href = session . webId
59+ }
60+ } )
5361
54- solid . auth . trackSession ( async session => {
55- if ( ! session ) {
56- elements . loggedIn . classList . add ( 'hidden' )
57- }
58- else {
59- elements . loggedIn . classList . remove ( 'hidden' )
60- elements . profileLink . innerText = session . webId
61- elements . profileLink . href = session . webId
62- }
63- } )
62+ button . addEventListener ( 'click' , function ( ) {
63+ loggedIn ? solid . auth . logout ( ) : solid . auth . popupLogin ( )
64+ } )
65+ } ) ( ) ;
6466</ script >
6567</ body >
6668</ html >
0 commit comments