forked from coala/community
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The contributor can sign-in with either GitHub or GitLab account provided the user is member of the organization. The validation is being performed via a http request which accepts the access_token retrieved from the netlify o-authentication. Closes coala#262
- Loading branch information
Showing
4 changed files
with
201 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,120 @@ | ||
/* globals Cookies, netlify */ | ||
$(document).ready(function(){ | ||
|
||
var login_user_el = $('.login-user'); | ||
var logout_user_el = $('.user-logout'); | ||
|
||
function activate_dropdown(){ | ||
if ($('nav').width() < 992 ){ | ||
$(".dropdown-trigger-sidenav").dropdown({coverTrigger: false}); | ||
} | ||
else { | ||
$(".dropdown-trigger").dropdown({hover: true, | ||
constrainWidth: false, | ||
varrainWidth: false, | ||
coverTrigger: false}); | ||
} | ||
} | ||
|
||
function check_user_authenticated_or_not() { | ||
if(Cookies.get('authenticated')){ | ||
modify_html_elements('none', 'none','block', 'block'); | ||
} | ||
} | ||
|
||
function get_error_message(oauth_provider, err){ | ||
return 'Error Authenticating with ' + oauth_provider + '. ' + err + | ||
'. Please try again later!'; | ||
} | ||
|
||
function display_error_message(oauth_provider, error_info) { | ||
$('.error-message').text(get_error_message(oauth_provider, error_info)); | ||
$('.oauth-error').css('display', 'block'); | ||
} | ||
|
||
function modify_html_elements(popup_form_display, login_option_display, | ||
profile_option_display, | ||
logout__option_display) { | ||
$('.form-popup').css('display', popup_form_display); | ||
login_user_el.css('display', login_option_display); | ||
$('.user-profile').css('display', profile_option_display); | ||
logout_user_el.css('display', logout__option_display); | ||
} | ||
|
||
function manipulate_web_page_data(oauth_provider, http_response_text) { | ||
var json_data = JSON.parse(http_response_text); | ||
if (json_data.valid) { | ||
Cookies.set('authenticated', true); | ||
Cookies.set('username', json_data.user); | ||
modify_html_elements('none', 'none','block', 'block'); | ||
} | ||
else { | ||
display_error_message(oauth_provider, json_data.message); | ||
} | ||
} | ||
|
||
function validate_user(oauth_provider, access_token){ | ||
var url = 'https://webservices.coala.io/'+ access_token +'/validate'; | ||
var xhttp = new XMLHttpRequest(); | ||
xhttp.onreadystatechange = function() { | ||
if (this.readyState === 4 && this.status === 200) { | ||
manipulate_web_page_data(oauth_provider, this.responseText); | ||
} | ||
}; | ||
xhttp.open("GET", url, true); | ||
xhttp.send(); | ||
} | ||
|
||
function login_with(oauth_provider){ | ||
var authenticator = new netlify.default({}); | ||
authenticator.authenticate( | ||
{ | ||
provider:oauth_provider, | ||
scope: oauth_provider==='github'?"user":"read_user" | ||
}, function(err, data) { | ||
if(err){ | ||
display_error_message(oauth_provider, err); | ||
} | ||
else { | ||
validate_user(oauth_provider, data.token); | ||
} | ||
} | ||
); | ||
} | ||
|
||
activate_dropdown(); | ||
|
||
check_user_authenticated_or_not(); | ||
|
||
$('.sidenav').sidenav(); | ||
|
||
$(window).resize(function(){ | ||
activate_dropdown(); | ||
}); | ||
|
||
$('#current-year').html(new Date().getFullYear()); | ||
|
||
login_user_el.click(function () { | ||
$('.form-popup').css('display', 'block'); | ||
}); | ||
|
||
$('.close-form').click(function () { | ||
$('.form-popup').css('display', 'none'); | ||
$('.oauth-error').css('display', 'none'); | ||
}); | ||
|
||
logout_user_el.click(function () { | ||
Cookies.remove('authenticated'); | ||
Cookies.remove('username'); | ||
modify_html_elements('none', 'block','none', 'none'); | ||
}); | ||
|
||
$('.login-with-github').click(function(e) { | ||
e.preventDefault(); | ||
login_with('github'); | ||
}); | ||
|
||
$('.login-with-gitlab').click(function(e) { | ||
e.preventDefault(); | ||
login_with('gitlab'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters