(Follow these steps if not)
require 'Dotenv'
require 'sinatra'
require 'sinatra/activerecord'
require 'sinatra/flash'
require 'omniauth-github'
require_relative 'config/application'
Dotenv.load
Dir['app/**/*.rb'].each { |file| require_relative file }
helpers do
def current_user
user_id = session[:user_id]
@current_user ||= User.find(user_id) if user_id.present?
end
def signed_in?
current_user.present?
end
end
def set_current_user(user)
session[:user_id] = user.id
end
def authenticate!
unless signed_in?
flash[:notice] = 'You need to sign in if you want to do that!'
redirect '/'
end
end
def is_member?(meetup_id, user_id)
if signed_in?
!Membership.where(["user_id = ? and meetup_id = ?", user_id, meetup_id]).empty?
end
end
get '/' do
erb :index
end
get '/auth/github/callback' do
auth = env['omniauth.auth']
user = User.find_or_create_from_omniauth(auth)
set_current_user(user)
flash[:notice] = "You're now signed in as #{user.username}!"
redirect '/'
end
get '/sign_out' do
session[:user_id] = nil
flash[:notice] = "You have been signed out."
redirect '/'
end
get '/example_protected_page' do
authenticate!
end
configure :development do
require 'dotenv'
Dotenv.load
require 'sinatra/reloader'
require 'pry'
also_reload 'app/**/*.rb'
end
configure do
enable :sessions
set :session_secret, ENV['SESSION_SECRET']
set :views, 'app/views'
use OmniAuth::Builder do
provider :github, ENV['GITHUB_KEY'], ENV['GITHUB_SECRET'],
scope: 'user:email'
end
end
def change
create_table :users do |table|
table.string :provider, null: false
table.string :uid, null: false
table.string :username, null: false
table.string :email, null: false
table.string :avatar_url, null: false
table.timestamps
end
add_index :users, [:uid, :provider], unique: true
end
class User < ActiveRecord::Base
def self.find_or_create_from_omniauth(auth)
provider = auth.provider
uid = auth.uid
find_by(provider: provider, uid: uid) || create_from_omniauth(auth)
end
def self.create_from_omniauth(auth)
create(
provider: auth.provider,
uid: auth.uid,
email: auth.info.email,
username: auth.info.nickname,
avatar_url: auth.info.image
)
end
end
- Go to "Settings" (under dropdown on top right)
- Click "Applications", then "Developer Applications", then "Register New Application"
- Fill out the name w/ the name of your app
- Fill out "homepage url" with
http://localhost:4567/
- Fill out "callback url" with
http://localhost:4567/auth/github/callback
- Click "create application"
- Take note of the "Client ID" and "Client Secret" values that are listed in the top right of your app information - you'll need them in just a sec!
- Create a
.env
file in the root directory of your app - Go find your "Client ID" value as provided on your Github Application page (created above). This is your "GITHUB_KEY"
- Go find your "Client Secret" value in the same place as the Client ID value was. This is your "GITHUB_SECRET"
- Open your
.env
file and add the following things:
SESSION_SECRET='this can be anything you want'
GITHUB_KEY='your-github-key-as-found-above'
GITHUB_SECRET='your-github-secret-as-found-above'
Of course, repace the values of GITHUB_KEY
and GITHUB_SECRET
. The SESSION_SECRET
can be whatever you want.
Add links to sign in and out. The path to sign in is "/auth/github", the path to sign out is "/sign_out"