1+ /* globals Cookies, netlify */
12$ ( document ) . ready ( function ( ) {
3+
4+ var login_user_el = $ ( '.login-user' ) ;
5+ var logout_user_el = $ ( '.user-logout' ) ;
6+
27 function activate_dropdown ( ) {
38 if ( $ ( 'nav' ) . width ( ) < 992 ) {
49 $ ( ".dropdown-trigger-sidenav" ) . dropdown ( { coverTrigger : false } ) ;
@@ -10,13 +15,107 @@ $(document).ready(function(){
1015 }
1116 }
1217
18+ function check_user_authenticated_or_not ( ) {
19+ if ( Cookies . get ( 'authenticated' ) ) {
20+ modify_html_elements ( 'none' , 'none' , 'block' , 'block' ) ;
21+ }
22+ }
23+
24+ function get_error_message ( oauth_provider , err ) {
25+ return 'Error Authenticating with ' + oauth_provider + '. ' + err +
26+ '. Please try again later!' ;
27+ }
28+
29+ function display_error_message ( oauth_provider , error_info ) {
30+ $ ( '.error-message' ) . text ( get_error_message ( oauth_provider , error_info ) ) ;
31+ $ ( '.oauth-error' ) . css ( 'display' , 'block' ) ;
32+ }
33+
34+ function modify_html_elements ( popup_form_display , login_option_display ,
35+ profile_option_display ,
36+ logout__option_display ) {
37+ $ ( '.form-popup' ) . css ( 'display' , popup_form_display ) ;
38+ login_user_el . css ( 'display' , login_option_display ) ;
39+ $ ( '.user-profile' ) . css ( 'display' , profile_option_display ) ;
40+ logout_user_el . css ( 'display' , logout__option_display ) ;
41+ }
42+
43+ function manipulate_web_page_data ( oauth_provider , http_response_text ) {
44+ var json_data = JSON . parse ( http_response_text ) ;
45+ if ( json_data . valid ) {
46+ Cookies . set ( 'authenticated' , true ) ;
47+ Cookies . set ( 'username' , json_data . user ) ;
48+ modify_html_elements ( 'none' , 'none' , 'block' , 'block' ) ;
49+ }
50+ else {
51+ display_error_message ( oauth_provider , json_data . message ) ;
52+ }
53+ }
54+
55+ function validate_user ( oauth_provider , access_token ) {
56+ var url = 'https://webservices.coala.io/' + oauth_provider + '/' +
57+ access_token + '/validate' ;
58+ var xhttp = new XMLHttpRequest ( ) ;
59+ xhttp . onreadystatechange = function ( ) {
60+ if ( this . readyState === 4 && this . status === 200 ) {
61+ manipulate_web_page_data ( oauth_provider , this . responseText ) ;
62+ }
63+ } ;
64+ xhttp . open ( "GET" , url , true ) ;
65+ xhttp . send ( ) ;
66+ }
67+
68+ function login_with ( oauth_provider ) {
69+ var authenticator = new netlify . default ( { } ) ;
70+ authenticator . authenticate (
71+ {
72+ provider :oauth_provider ,
73+ scope : oauth_provider === 'github' ?"user" :"api"
74+ } , function ( err , data ) {
75+ if ( err ) {
76+ display_error_message ( oauth_provider , err ) ;
77+ }
78+ else {
79+ validate_user ( data . provider , data . token ) ;
80+ }
81+ }
82+ ) ;
83+ }
84+
1385 activate_dropdown ( ) ;
1486
87+ check_user_authenticated_or_not ( ) ;
88+
1589 $ ( '.sidenav' ) . sidenav ( ) ;
1690
1791 $ ( window ) . resize ( function ( ) {
1892 activate_dropdown ( ) ;
1993 } ) ;
2094
2195 $ ( '#current-year' ) . html ( new Date ( ) . getFullYear ( ) ) ;
96+
97+ login_user_el . click ( function ( ) {
98+ $ ( '.form-popup' ) . css ( 'display' , 'block' ) ;
99+ } ) ;
100+
101+ $ ( '.close-form' ) . click ( function ( ) {
102+ $ ( '.form-popup' ) . css ( 'display' , 'none' ) ;
103+ $ ( '.oauth-error' ) . css ( 'display' , 'none' ) ;
104+ } ) ;
105+
106+ logout_user_el . click ( function ( ) {
107+ Cookies . remove ( 'authenticated' ) ;
108+ Cookies . remove ( 'username' ) ;
109+ modify_html_elements ( 'none' , 'block' , 'none' , 'none' ) ;
110+ } ) ;
111+
112+ $ ( '.login-with-github' ) . click ( function ( e ) {
113+ e . preventDefault ( ) ;
114+ login_with ( 'github' ) ;
115+ } ) ;
116+
117+ $ ( '.login-with-gitlab' ) . click ( function ( e ) {
118+ e . preventDefault ( ) ;
119+ login_with ( 'gitlab' ) ;
120+ } ) ;
22121} ) ;
0 commit comments