Skip to content

Instantly share code, notes, and snippets.

@stevesohcot
stevesohcot / omniauth.rb
Created January 30, 2016 18:33
OmniAuth config initializer
#/config/initializers/omniauth.rb
Rails.application.config.middleware.use OmniAuth::Builder do
provider :identity, fields: [:first_name, :last_name, :email],
on_failed_registration: lambda{|env| IdentitiesController.action(:new).call(env)}\
end
@stevesohcot
stevesohcot / sessions_controller.rb
Created January 30, 2016 19:04
OmniAuth Identity - Sessions Controller
class SessionsController < ApplicationController
# to avoid a redirect loop, need to skip (exclude) it validating the autentication in this controller
skip_before_action :require_authentication
def create
user = User.from_omniauth(auth, params)
if logged_out?
@stevesohcot
stevesohcot / routes.rb
Created January 30, 2016 19:11
OmniAuth - Routes
#Omniauth
match 'auth/:provider/callback', to: 'sessions#create', via: :all
match 'auth/failure', to: 'sessions#failure', via: :all
match 'signup', to: 'identities#new', via: :get
match 'login', to: 'sessions#new', via: :get
match 'logout', to: 'sessions#destroy', via: [:get, :delete], as: 'logout'
@stevesohcot
stevesohcot / application_controller.rb
Created January 30, 2016 19:18
OmniAuth - Application Controller
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
#protect_from_forgery with: :exception
before_action :require_authentication # will happen for ALL controllers/actions
private
def current_user
@stevesohcot
stevesohcot / new.html.erb
Created January 30, 2016 19:20
OmniAuth - Sessions NEW
# NOTE: this uses Bootstrap for styling
# and there's some extra javascript validation
<%= form_tag('/auth/identity/callback') do %>
<header align="center" style="margin-bottom:15px;">
<h2>Log In</h2>
</header>
<div style="width:80%; margin:auto;">
@stevesohcot
stevesohcot / identities_controller.rb
Created January 30, 2016 19:34
OmniAuth Identities Controller
class IdentitiesController < ApplicationController
skip_before_action :require_authentication, only: [:new]
def new
# When the app first opens, they're brought to the sign-up page
# If they have a cookie set, try to log in with it
if logged_out?
check_if_remembered
@stevesohcot
stevesohcot / new.html.erb
Created January 30, 2016 19:41
OmniAuth Identities - "new" view
<!--
NOTE: this is using Bootstrap for styling, and has some additional javascript not in this file.
- Specifically, the "Confirm Password" is hidden, so it will automatically be updated to whatever is in the original
- and the "Show Password" checkbox toggle
-->
<%= form_for(@identity, url:'/auth/identity/register') do |f| %>
<% if @identity.errors.any? %>
<div class="alert alert-danger">
<h2><%= pluralize(@identity.errors.count, "error") %> prohibited this user from being saved:</h2>
@stevesohcot
stevesohcot / user.rb
Created January 30, 2016 19:46
OmniAuth - User model
class User < ActiveRecord::Base
validates_uniqueness_of :email
def self.from_omniauth(auth,params)
find_by(provider: auth.provider, uid: auth.uid) || create_from_omniauth(auth,params)
end
def self.create_from_omniauth(auth,params)
@stevesohcot
stevesohcot / identity.rb
Created January 30, 2016 19:56
OmniAuth - Identity Model
class Identity < OmniAuth::Identity::Models::ActiveRecord
before_save :convert_to_lowercase
validates_presence_of :email, :password #, message: "can't be blank"
validates_uniqueness_of :email
private
def convert_to_lowercase
@stevesohcot
stevesohcot / gist:cd48e7717b9541361021
Created January 30, 2016 22:02
OmniAuth - javascript
// Show Password with checkbox
//$.toggleShowPassword({
// field: '#test1',
// control: '#test2'
//});
(function ($) {
$.toggleShowPassword = function (options) {