-
Notifications
You must be signed in to change notification settings - Fork 35
BrAPI Login
Amber Lockrow edited this page Mar 21, 2023
·
7 revisions
For external applications to access a BreeDBase instance, they first need authorization from the BreeDBase end. Please contact the BreeDBase team at https://breedbase.org/contact/form to get the required authorization for your application.
From there, follow the steps below to provide your application access to the instance of your choice:
- Retrieve the sgn_session_id token from [breedbase instance]/user/logged_in (e.g. cassavabase.org/user/logged_in) by using a jQuery (https://jquery.com/download/) ajax request in javascript (or similar function).
- Acquire the cookie token using js-cookie (https://github.com/js-cookie/js-cookie) or similar API.
- Prompt the user for permission to use their login credentials for an external application. To do this, use a BrAPI authorize call and redirect back to the external application. (e.g. window.location.href='http://cassavabase.org/brapi/authorize?redirect_uri=http://your_url.com').
- Use the cookie to retrieve information from the breedbase instance. For example, user information can be found at [breedbase instance]/user/cookie_login/[cookie] (From here you can retrieve first_name, last_name, username, and user_id credentials).
Below is an example of code for "your_url.com" to access cassavabase.org and acquire username information:
<script src="/static/js/node_modules/jquery/dist/jquery.js"></script>
<script src="/static/js/node_modules/js-cookie/dist/js.cookie.js"></script>
<script>
var username;
$( document ).ready(function(){
jQuery.ajax({
url : 'http://cassavabase.org/user/logged_in',
}).done(function(r){
var sgn_cookie = Cookies.get('sgn_session_id')
get_username(sgn_cookie)
});
jQuery('#login_button').on('click', function(){
window.location.href='http://cassavabase.org/brapi/authorize?redirect_uri=http://your_url.com'
})
function get_username(cookie){
jQuery.ajax( {
url : 'http://cassavabase.org/user/cookie_login/'+cookie,
success: function(response){
if(response.error){
alert(response.error);
return false;
}
if(response.username){
let username = response.username;
}
}
})
}
});
</script>