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,78 @@ $(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 expires in 3 days
47+ Cookies . set ( 'authenticated' , true , { expires : 3 } ) ;
48+ Cookies . set ( 'username' , json_data . user , { expires : 3 } ) ;
49+ modify_html_elements ( 'none' , 'none' , 'block' , 'block' ) ;
50+ }
51+ else {
52+ display_error_message ( oauth_provider , json_data . message ) ;
53+ }
54+ }
55+
56+ function validate_user ( oauth_provider , access_token ) {
57+ var url = 'https://webservices.coala.io/' + oauth_provider + '/' +
58+ access_token + '/validate' ;
59+ var xhttp = new XMLHttpRequest ( ) ;
60+ xhttp . onreadystatechange = function ( ) {
61+ if ( this . readyState === 4 && this . status === 200 ) {
62+ manipulate_web_page_data ( oauth_provider , this . responseText ) ;
63+ }
64+ } ;
65+ xhttp . open ( "GET" , url , true ) ;
66+ xhttp . send ( ) ;
67+ }
68+
69+ function login_with ( oauth_provider ) {
70+ var authenticator = new netlify . default ( { } ) ;
71+ authenticator . authenticate (
72+ {
73+ provider :oauth_provider ,
74+ scope : oauth_provider === 'github' ?"user" :"api"
75+ } , function ( err , data ) {
76+ if ( err ) {
77+ display_error_message ( oauth_provider , err ) ;
78+ }
79+ else {
80+ validate_user ( data . provider , data . token ) ;
81+ }
82+ }
83+ ) ;
84+ }
85+
1386 activate_dropdown ( ) ;
1487
88+ check_user_authenticated_or_not ( ) ;
89+
1590 $ ( '.sidenav' ) . sidenav ( ) ;
1691 $ ( 'select' ) . formSelect ( ) ;
1792
@@ -20,4 +95,29 @@ $(document).ready(function(){
2095 } ) ;
2196
2297 $ ( '#current-year' ) . html ( new Date ( ) . getFullYear ( ) ) ;
98+
99+ login_user_el . click ( function ( ) {
100+ $ ( '.form-popup' ) . css ( 'display' , 'block' ) ;
101+ } ) ;
102+
103+ $ ( '.close-form' ) . click ( function ( ) {
104+ $ ( '.form-popup' ) . css ( 'display' , 'none' ) ;
105+ $ ( '.oauth-error' ) . css ( 'display' , 'none' ) ;
106+ } ) ;
107+
108+ logout_user_el . click ( function ( ) {
109+ Cookies . remove ( 'authenticated' ) ;
110+ Cookies . remove ( 'username' ) ;
111+ modify_html_elements ( 'none' , 'block' , 'none' , 'none' ) ;
112+ } ) ;
113+
114+ $ ( '.login-with-github' ) . click ( function ( e ) {
115+ e . preventDefault ( ) ;
116+ login_with ( 'github' ) ;
117+ } ) ;
118+
119+ $ ( '.login-with-gitlab' ) . click ( function ( e ) {
120+ e . preventDefault ( ) ;
121+ login_with ( 'gitlab' ) ;
122+ } ) ;
23123} ) ;
0 commit comments