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,8 +15,76 @@ $(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' ) ;
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+ logout__option_display ) {
36+ $ ( '.form-popup' ) . css ( 'display' , popup_form_display ) ;
37+ login_user_el . css ( 'display' , login_option_display ) ;
38+ logout_user_el . css ( 'display' , logout__option_display ) ;
39+ }
40+
41+ function manipulate_web_page_data ( oauth_provider , http_response_text ) {
42+ var json_data = JSON . parse ( http_response_text ) ;
43+ if ( json_data . valid ) {
44+ // Cookies expires in 3 days
45+ Cookies . set ( 'authenticated' , true , { expires : 3 } ) ;
46+ Cookies . set ( 'username' , json_data . user , { expires : 3 } ) ;
47+ modify_html_elements ( 'none' , 'none' , 'block' ) ;
48+ }
49+ else {
50+ display_error_message ( oauth_provider , json_data . message ) ;
51+ }
52+ }
53+
54+ function validate_user ( oauth_provider , access_token ) {
55+ var url = 'https://webservices.coala.io/' + oauth_provider + '/' +
56+ access_token + '/validate' ;
57+ var xhttp = new XMLHttpRequest ( ) ;
58+ xhttp . onreadystatechange = function ( ) {
59+ if ( this . readyState === 4 && this . status === 200 ) {
60+ manipulate_web_page_data ( oauth_provider , this . responseText ) ;
61+ }
62+ } ;
63+ xhttp . open ( "GET" , url , true ) ;
64+ xhttp . send ( ) ;
65+ }
66+
67+ function login_with ( oauth_provider ) {
68+ var authenticator = new netlify . default ( { } ) ;
69+ authenticator . authenticate (
70+ {
71+ provider :oauth_provider ,
72+ scope : oauth_provider === 'github' ?"user" :"api"
73+ } , function ( err , data ) {
74+ if ( err ) {
75+ display_error_message ( oauth_provider , err ) ;
76+ }
77+ else {
78+ validate_user ( data . provider , data . token ) ;
79+ }
80+ }
81+ ) ;
82+ }
83+
1384 activate_dropdown ( ) ;
1485
86+ check_user_authenticated_or_not ( ) ;
87+
1588 $ ( '.sidenav' ) . sidenav ( ) ;
1689 $ ( 'select' ) . formSelect ( ) ;
1790
@@ -20,4 +93,29 @@ $(document).ready(function(){
2093 } ) ;
2194
2295 $ ( '#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' ) ;
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+ } ) ;
23121} ) ;
0 commit comments