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
#/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 |
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
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? |
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
#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' |
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
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 |
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
# 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;"> |
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
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 |
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
<!-- | |
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> |
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
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) |
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
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 |
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
// Show Password with checkbox | |
//$.toggleShowPassword({ | |
// field: '#test1', | |
// control: '#test2' | |
//}); | |
(function ($) { | |
$.toggleShowPassword = function (options) { |
OlderNewer