forked from derwiki/singly-rails-api-demo
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate OmniAuth with a Singly provider
Using the new gem at Singly/omniauth-singly make starting up a breeze.
- Loading branch information
Showing
7 changed files
with
32 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,17 @@ | ||
require 'httparty' | ||
require "httparty" | ||
|
||
class AuthController < ApplicationController | ||
|
||
def service | ||
redirect_to auth_url(params[:service]) | ||
end | ||
|
||
SINGLY_API_BASE = "https://api.singly.com" | ||
|
||
def callback | ||
data = HTTParty.post( | ||
token_url, | ||
{:body => token_params(params[:code])} | ||
).parsed_response | ||
session[:access_token] = data['access_token'] | ||
auth = request.env["omniauth.auth"] | ||
session[:access_token] = auth.credentials.token | ||
redirect_to "/" | ||
end | ||
|
||
def logout | ||
session.clear | ||
redirect_to "/" | ||
end | ||
|
||
private | ||
|
||
def token_params(code) | ||
{ | ||
:client_id => ENV["SINGLY_ID"], | ||
:client_secret => ENV["SINGLY_SECRET"], | ||
:code => code | ||
} | ||
end | ||
|
||
def token_url | ||
"#{SINGLY_API_BASE}/oauth/access_token" | ||
end | ||
|
||
def auth_params(service) | ||
{ | ||
:client_id => ENV["SINGLY_ID"], | ||
:redirect_uri => callback_url, | ||
:service => service | ||
}.map {|key, value| | ||
"#{key}=#{value}" | ||
}.join("&") | ||
end | ||
|
||
def auth_url(servie) | ||
"#{SINGLY_API_BASE}/oauth/authorize?#{auth_params(params[:service])}" | ||
end | ||
|
||
def callback_url | ||
"#{request.protocol}#{request.host}:#{request.port}/auth_callback" | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
require 'httparty' | ||
require "httparty" | ||
|
||
class DefaultController < ApplicationController | ||
def home | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Rails.application.config.middleware.use OmniAuth::Builder do | ||
provider :singly, ENV['SINGLY_ID'], ENV['SINGLY_SECRET'] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
SinglyRailsSkeleton::Application.routes.draw do | ||
match 'auth/:service' => 'auth#service' | ||
match 'auth_callback' => 'auth#callback' | ||
match 'logout' => 'auth#logout' | ||
root :to => 'default#home' | ||
match "auth/:service" => "auth#service" | ||
match "auth/:service/callback" => "auth#callback" | ||
match "logout" => "auth#logout" | ||
root :to => "default#home" | ||
end |