mirror of https://github.com/fspc/BikeShed-1.git
jnm
12 years ago
10 changed files with 164 additions and 3 deletions
@ -0,0 +1,43 @@ |
|||||
|
$("#checkin_menu").show(); |
||||
|
$("#checkin").click( function(e){ |
||||
|
var username = $("#user_email").val(); |
||||
|
var password = $("#user_password").val(); |
||||
|
$.ajax({ |
||||
|
type: 'POST', |
||||
|
url: '/api/v1/checkin', |
||||
|
dataType: 'json', |
||||
|
contentType: 'application/json', |
||||
|
processData: false, |
||||
|
data: JSON.stringify({"username": username, "password": password }), |
||||
|
complete: function() { }, |
||||
|
success: function(data) { |
||||
|
alert("Checked IN!"); |
||||
|
$("#user_email").val(''); |
||||
|
$("#user_password").val(''); |
||||
|
}, |
||||
|
error: function(data,textStatus) { |
||||
|
alert( "Error: " + JSON.parse(data.responseText)["error"]); |
||||
|
} |
||||
|
}) |
||||
|
}); |
||||
|
$("#checkout").click( function(e){ |
||||
|
var username = $("#user_email").val(); |
||||
|
var password = $("#user_password").val(); |
||||
|
$.ajax({ |
||||
|
type: 'POST', |
||||
|
url: '/api/v1/checkout', |
||||
|
dataType: 'json', |
||||
|
contentType: 'application/json', |
||||
|
processData: false, |
||||
|
data: JSON.stringify({"username": username, "password": password }), |
||||
|
complete: function() { }, |
||||
|
success: function(data) { |
||||
|
alert("Checked OUT!"); |
||||
|
$("#user_email").val(''); |
||||
|
$("#user_password").val(''); |
||||
|
}, |
||||
|
error: function(data,textStatus) { |
||||
|
alert( "Error: " + JSON.parse(data.responseText)["error"]); |
||||
|
} |
||||
|
}) |
||||
|
}); |
@ -0,0 +1,22 @@ |
|||||
|
class Api::V1::BaseController < ActionController::Base |
||||
|
respond_to :json |
||||
|
|
||||
|
before_filter :authenticate_user |
||||
|
|
||||
|
private |
||||
|
def authenticate_user |
||||
|
if params[:username] |
||||
|
user = User.find_for_database_authentication( :email => params[:username] ) |
||||
|
@current_user = user if user && user.valid_password?( params[:password] ) |
||||
|
|
||||
|
if @current_user.nil? |
||||
|
msg = "Username/Password/Token invalid" |
||||
|
render :json => {:error => msg }, :status => 403 and return |
||||
|
end |
||||
|
else |
||||
|
authenticate_user! |
||||
|
@current_user = current_user |
||||
|
end |
||||
|
end |
||||
|
end |
||||
|
|
@ -0,0 +1,22 @@ |
|||||
|
class Api::V1::LogsController < Api::V1::BaseController |
||||
|
|
||||
|
def checkin |
||||
|
#must use @current_user since user may not have signed in |
||||
|
if @current_user.checked_in? |
||||
|
render :json => { "error" => "You are already checked in."}, :status => 404 and return |
||||
|
else |
||||
|
@current_user.checkin |
||||
|
render :nothing => true, :status => 204 and return |
||||
|
end |
||||
|
end |
||||
|
|
||||
|
def checkout |
||||
|
#must use @current_user since user may not have signed in |
||||
|
if !@current_user.checked_in? |
||||
|
render :json => { "error" => "You were not even checked in."}, :status => 404 and return |
||||
|
else |
||||
|
@current_user.checkout |
||||
|
render :nothing => true, :status => 204 and return |
||||
|
end |
||||
|
end |
||||
|
end |
Loading…
Reference in new issue