The APIs usually take your login request and send you an authentication token, which you need to pass in every call. This is very similar to cookie-based sessions in which your browser automatically transmits cookies that were acquired when you first visited the website.
From what I saw in the Sinatra docs, you can make a session-based authentication system like this:
enable :session disable :show_exceptions use Rack::Session::Pool, key: 'session_id' post '/login' do user = User.login_success(params) halt 401 if user.nil? session[:user] = user 200 end get '/fun' do user = session[:user] halt 401 if user.nil? halt 403 if !user.has_permission_for '/fun' "fun was had" end
Now all you have to do in your client is to pass back the cookie token returned in response to the initial visit when requesting the API function. This can be done with any web client library that supports cookie stores (e.g. libcurl) or by manually inserting a session cookie into the request header. Rack::Minitest Functionality also supports cookies, so you can test your API with minitest.
source share