1
+ /* globals Cookies, netlify */
1
2
$ ( document ) . ready ( function ( ) {
3
+
4
+ var login_user_el = $ ( '.login-user' ) ;
5
+ var logout_user_el = $ ( '.user-logout' ) ;
6
+
2
7
function activate_dropdown ( ) {
3
8
if ( $ ( 'nav' ) . width ( ) < 992 ) {
4
9
$ ( ".dropdown-trigger-sidenav" ) . dropdown ( { coverTrigger : false } ) ;
@@ -10,13 +15,106 @@ $(document).ready(function(){
10
15
}
11
16
}
12
17
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/' + 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" :"read_user"
73
+ } , function ( err , data ) {
74
+ if ( err ) {
75
+ display_error_message ( oauth_provider , err ) ;
76
+ }
77
+ else {
78
+ validate_user ( oauth_provider , data . token ) ;
79
+ }
80
+ }
81
+ ) ;
82
+ }
83
+
13
84
activate_dropdown ( ) ;
14
85
86
+ check_user_authenticated_or_not ( ) ;
87
+
15
88
$ ( '.sidenav' ) . sidenav ( ) ;
16
89
17
90
$ ( window ) . resize ( function ( ) {
18
91
activate_dropdown ( ) ;
19
92
} ) ;
20
93
21
94
$ ( '#current-year' ) . html ( new Date ( ) . getFullYear ( ) ) ;
95
+
96
+ login_user_el . click ( function ( ) {
97
+ $ ( '.form-popup' ) . css ( 'display' , 'block' ) ;
98
+ } ) ;
99
+
100
+ $ ( '.close-form' ) . click ( function ( ) {
101
+ $ ( '.form-popup' ) . css ( 'display' , 'none' ) ;
102
+ $ ( '.oauth-error' ) . css ( 'display' , 'none' ) ;
103
+ } ) ;
104
+
105
+ logout_user_el . click ( function ( ) {
106
+ Cookies . remove ( 'authenticated' ) ;
107
+ Cookies . remove ( 'username' ) ;
108
+ modify_html_elements ( 'none' , 'block' , 'none' , 'none' ) ;
109
+ } ) ;
110
+
111
+ $ ( '.login-with-github' ) . click ( function ( e ) {
112
+ e . preventDefault ( ) ;
113
+ login_with ( 'github' ) ;
114
+ } ) ;
115
+
116
+ $ ( '.login-with-gitlab' ) . click ( function ( e ) {
117
+ e . preventDefault ( ) ;
118
+ login_with ( 'gitlab' ) ;
119
+ } ) ;
22
120
} ) ;
0 commit comments