Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 35 lines (29 sloc) 0.901 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
require 'sinatra/base'

module Sinatra
  module CommonHelper

    def require_login
      redirect_with_message('/login', 'Please login first') unless session[:id]
    end

    def authenticate(email, password)
      response = RestClient.post('https://www.google.com/accounts/ClientLogin',
      'accountType' => 'HOSTED_OR_GOOGLE',
      'Email' => email,
      'Passwd' => password,
      :service => 'xapi',
      :source => 'Goog-Auth-1.0') do |response, request, result, &block|

        user = User.first :email => email
        if response.code == 200 and not user.nil?
          session[:id] = response.to_s
          session[:user] = user.id.to_s
          return true
        end
        return false
      end
    end

    def redirect_with_message(to_location, message)
      flash[:message] = message
      redirect to_location
    end

  end
  helpers CommonHelper
end
Something went wrong with that request. Please try again.