Commit 257b84a6 by Mykhailo Makohin

finish registration via google

parent 07bf996d
......@@ -5,51 +5,29 @@ git_source(:github) do |repo_name|
"https://github.com/#{repo_name}.git"
end
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.7', '>= 5.0.7.2'
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.3.18', '< 0.6.0'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
gem 'devise'
gem 'omniauth'
gem 'omniauth-facebook'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
gem 'omniauth-google-oauth2'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '~> 3.0.5'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
......@@ -105,6 +105,10 @@ GEM
rack (>= 1.6.2, < 3)
omniauth-facebook (5.0.0)
omniauth-oauth2 (~> 1.2)
omniauth-google-oauth2 (0.8.0)
jwt (>= 2.0)
omniauth (>= 1.1.1)
omniauth-oauth2 (>= 1.6)
omniauth-oauth2 (1.6.0)
oauth2 (~> 1.1)
omniauth (~> 1.9)
......@@ -198,7 +202,9 @@ DEPENDENCIES
jquery-rails
listen (~> 3.0.5)
mysql2 (>= 0.3.18, < 0.6.0)
omniauth
omniauth-facebook
omniauth-google-oauth2
puma (~> 3.0)
rails (~> 5.0.7, >= 5.0.7.2)
sass-rails (~> 5.0)
......
class TemplatesController < ApplicationController
before_action :set_template, only: [:show, :edit, :update, :destroy]
# GET /templates
# GET /templates.json
def index
@templates = Template.all
end
# GET /templates/1
# GET /templates/1.json
def show
end
# GET /templates/new
def new
@template = Template.new
end
# GET /templates/1/edit
def edit
end
# POST /templates
# POST /templates.json
def create
@template = Template.new(template_params)
respond_to do |format|
if @template.save
format.html { redirect_to @template, notice: 'Template was successfully created.' }
format.json { render :show, status: :created, location: @template }
else
format.html { render :new }
format.json { render json: @template.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /templates/1
# PATCH/PUT /templates/1.json
def update
respond_to do |format|
if @template.update(template_params)
format.html { redirect_to @template, notice: 'Template was successfully updated.' }
format.json { render :show, status: :ok, location: @template }
else
format.html { render :edit }
format.json { render json: @template.errors, status: :unprocessable_entity }
end
end
end
# DELETE /templates/1
# DELETE /templates/1.json
def destroy
@template.destroy
respond_to do |format|
format.html { redirect_to templates_url, notice: 'Template was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_template
@template = Template.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def template_params
params.require(:template).permit(:title, :desc)
end
end
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, event: :authentication #this will throw if @user is not activated
sign_in_and_redirect @user, event: :authentication
set_flash_message(:notice, :success, kind: "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
......@@ -12,6 +13,19 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
end
def failure
redirect_to new_template_path
redirect_to root_path
end
def google_oauth2
@user = User.from_omniauth(request.env['omniauth.auth'])
if @user.persisted?
flash[:notice] = I18n.t 'devise.omniauth_callbacks.success', kind: 'Google'
sign_in_and_redirect @user, event: :authentication
else
session['devise.google_data'] = request.env['omniauth.auth'].except(:extra)
redirect_to new_user_registration_url, alert: @user.errors.full_messages.join("\n")
end
end
end
\ No newline at end of file
class Template < ApplicationRecord
end
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :omniauthable, omniauth_providers: [:facebook]
:recoverable, :rememberable, :omniauthable,
omniauth_providers: [:facebook, :google_oauth2]
def self.new_with_session(params, session)
......@@ -10,9 +11,9 @@ class User < ApplicationRecord
user.email = data["email"] if user.email.blank?
end
end
end
end
def self.from_omniauth(auth)
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
# user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
......@@ -22,5 +23,19 @@ def self.from_omniauth(auth)
# uncomment the line below to skip the confirmation emails.
# user.skip_confirmation!
end
end
end
def self.from_omniauth(access_token)
data = access_token.info
user = User.where(email: data['email']).first
# Uncomment the section below if you want users to be created if they don't exist
unless user
user = User.create(name: data['name'],
email: data['email'],
password: Devise.friendly_token[0,20]
)
end
user
end
end
<% unless current_user %>
<%= link_to "Sign in with Facebook", user_facebook_omniauth_authorize_path %>
<%= link_to "Sign in with Google", user_google_oauth2_omniauth_authorize_path %>
<% else %> <%= current_user[:name] %>
<%= link_to "Logout", destroy_user_session_path, method: :delete %>
<% end %>
<!DOCTYPE html>
<html>
<head>
<title>WarmCity</title>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<%= yield %>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
/* Email styles need to be inline */
</style>
</head>
<body>
<%= yield %>
</body>
</html>
<%= form_for(template) do |f| %>
<% if template.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(template.errors.count, "error") %> prohibited this template from being saved:</h2>
<ul>
<% template.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :desc %>
<%= f.text_area :desc %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
json.extract! template, :id, :title, :desc, :created_at, :updated_at
json.url template_url(template, format: :json)
<h1>Editing Template</h1>
<%= render 'form', template: @template %>
<%= link_to 'Show', @template %> |
<%= link_to 'Back', templates_path %>
<p id="notice"><%= notice %></p>
<h1>Templates</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Desc</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @templates.each do |template| %>
<tr>
<td><%= template.title %></td>
<td><%= template.desc %></td>
<td><%= link_to 'Show', template %></td>
<td><%= link_to 'Edit', edit_template_path(template) %></td>
<td><%= link_to 'Destroy', template, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Template', new_template_path %>
<% unless current_user %> <%= link_to "Sign in with Facebook", user_facebook_omniauth_authorize_path %><% else %> <%= current_user[:name] %> <%= link_to "Logout", destroy_user_session_path, method: :delete %><% end %>
<%= link_to "Sign in with Facebook", user_facebook_omniauth_authorize_path %>
\ No newline at end of file
json.array! @templates, partial: "templates/template", as: :template
<h1>New Template</h1>
<%= render 'form', template: @template %>
<%= link_to 'Back', templates_path %>
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= @template.title %>
</p>
<p>
<strong>Desc:</strong>
<%= @template.desc %>
</p>
<%= link_to 'Edit', edit_template_path(@template) %> |
<%= link_to 'Back', templates_path %>
json.partial! "templates/template", template: @template
Rails.application.routes.draw do
resources :templates
root 'application#index'
devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
end
# Be sure to restart your server when you modify this file.
# Your secret key is used for verifying the integrity of signed cookies.
# If you change this key, all old signed cookies will become invalid!
# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
# You can use `rails secret` to generate a secure secret key.
# Make sure the secrets in this file are kept private
# if you're sharing your code publicly.
development:
secret_key_base: b1b8b2c8f26c31fb891d8f8eaf37f5bc26ea31ac8913a4acfe7e9013853eeb3df5e681187e8ed35df9e6526d9528973791f904727fe56d250d903ff21083ece6
GOOGLE_CLIENT_ID: 444952886435-8s76oeuc53otc8q84jork9mq4php7e7t.apps.googleusercontent.com
GOOGLE_SECRET_KEY: vfJkP71fOkfDKVYa3RgXR3lW
test:
secret_key_base: dd24cc39a6555956e85cb7d60612c685b91249ed8760a957b155ced0177eb172674137f73ba33d8b886f14089469703830d76ddbd42a996897d8b77aac43b33a
# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
class CreateTemplates < ActiveRecord::Migration[5.0]
def change
create_table :templates do |t|
t.string :title
t.text :desc
t.timestamps
end
end
end
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20190829110429) do
create_table "templates", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "title"
t.text "desc", limit: 65535
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
......
require 'test_helper'
class TemplatesControllerTest < ActionDispatch::IntegrationTest
setup do
@template = templates(:one)
end
test "should get index" do
get templates_url
assert_response :success
end
test "should get new" do
get new_template_url
assert_response :success
end
test "should create template" do
assert_difference('Template.count') do
post templates_url, params: { template: { desc: @template.desc, title: @template.title } }
end
assert_redirected_to template_url(Template.last)
end
test "should show template" do
get template_url(@template)
assert_response :success
end
test "should get edit" do
get edit_template_url(@template)
assert_response :success
end
test "should update template" do
patch template_url(@template), params: { template: { desc: @template.desc, title: @template.title } }
assert_redirected_to template_url(@template)
end
test "should destroy template" do
assert_difference('Template.count', -1) do
delete template_url(@template)
end
assert_redirected_to templates_url
end
end
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
title: MyString
desc: MyText
two:
title: MyString
desc: MyText
require 'test_helper'
class TemplateTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment