diff --git a/.gitignore b/.gitignore index bab620d..fbd49fa 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,6 @@ # Ignore Byebug command history file. .byebug_history + +# Ignore key +/config/application.yml diff --git a/Gemfile b/Gemfile index c842f32..2fa9786 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,13 @@ source 'https://rubygems.org' + +gem "devise" ruby '2.3.1' + +gem 'settingslogic' #管理我們的秘密key + +gem 'omniauth-google-oauth2' + # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.0.0' # Use sqlite3 as the database for Active Record diff --git a/Gemfile.lock b/Gemfile.lock index 9599f18..1cf9a09 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -40,6 +40,7 @@ GEM tzinfo (~> 1.1) arel (7.1.0) ast (2.3.0) + bcrypt (3.1.11) builder (3.2.2) byebug (9.0.5) coffee-rails (4.2.1) @@ -51,11 +52,20 @@ GEM coffee-script-source (1.10.0) concurrent-ruby (1.0.2) debug_inspector (0.0.2) + devise (4.2.0) + bcrypt (~> 3.0) + orm_adapter (~> 0.1) + railties (>= 4.1.0, < 5.1) + responders + warden (~> 1.2.3) erubis (2.7.0) execjs (2.7.0) + faraday (0.9.2) + multipart-post (>= 1.2, < 3) ffi (1.9.14) globalid (0.3.6) activesupport (>= 4.1.0) + hashie (3.4.4) i18n (0.7.0) jbuilder (2.6.0) activesupport (>= 3.0.0, < 5.1) @@ -64,6 +74,7 @@ GEM rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) + jwt (1.5.4) listen (3.0.8) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -78,10 +89,30 @@ GEM mini_portile2 (2.1.0) minitest (5.9.0) multi_json (1.12.1) + multi_xml (0.5.5) + multipart-post (2.0.0) nio4r (1.2.1) nokogiri (1.6.8) mini_portile2 (~> 2.1.0) pkg-config (~> 1.1.7) + oauth2 (1.2.0) + faraday (>= 0.8, < 0.10) + jwt (~> 1.0) + multi_json (~> 1.3) + multi_xml (~> 0.5) + rack (>= 1.2, < 3) + omniauth (1.3.1) + hashie (>= 1.2, < 4) + rack (>= 1.0, < 3) + omniauth-google-oauth2 (0.4.1) + jwt (~> 1.5.2) + multi_json (~> 1.3) + omniauth (>= 1.1.1) + omniauth-oauth2 (>= 1.3.1) + omniauth-oauth2 (1.4.0) + oauth2 (~> 1.0) + omniauth (~> 1.2) + orm_adapter (0.5.0) parser (2.3.1.2) ast (~> 2.2) pkg-config (1.1.7) @@ -118,6 +149,8 @@ GEM rb-fsevent (0.9.7) rb-inotify (0.9.7) ffi (>= 0.5.0) + responders (2.2.0) + railties (>= 4.2.0, < 5.1) rubocop (0.42.0) parser (>= 2.3.1.1, < 3.0) powerpack (~> 0.1) @@ -132,6 +165,7 @@ GEM sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) + settingslogic (2.0.9) spring (1.7.2) spring-watcher-listen (2.0.0) listen (>= 2.7, < 4.0) @@ -155,6 +189,8 @@ GEM uglifier (3.0.0) execjs (>= 0.3.0, < 3) unicode-display_width (1.1.0) + warden (1.2.6) + rack (>= 1.0) web-console (3.3.1) actionview (>= 5.0) activemodel (>= 5.0) @@ -170,13 +206,16 @@ PLATFORMS DEPENDENCIES byebug coffee-rails (~> 4.2) + devise jbuilder (~> 2.5) jquery-rails listen (~> 3.0.5) + omniauth-google-oauth2 puma (~> 3.0) rails (~> 5.0.0) rubocop sass-rails (~> 5.0) + settingslogic spring spring-watcher-listen (~> 2.0.0) sqlite3 @@ -185,5 +224,8 @@ DEPENDENCIES uglifier (>= 1.3.0) web-console +RUBY VERSION + ruby 2.3.1p112 + BUNDLED WITH 1.12.5 diff --git a/app/assets/javascripts/account/products.coffee b/app/assets/javascripts/account/products.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/account/products.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/javascripts/products.coffee b/app/assets/javascripts/products.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/products.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/javascripts/testpage.coffee b/app/assets/javascripts/testpage.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/testpage.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/javascripts/users/omniauth_callbacks.coffee b/app/assets/javascripts/users/omniauth_callbacks.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/users/omniauth_callbacks.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/account/products.scss b/app/assets/stylesheets/account/products.scss new file mode 100644 index 0000000..70139de --- /dev/null +++ b/app/assets/stylesheets/account/products.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the account::products controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/products.scss b/app/assets/stylesheets/products.scss new file mode 100644 index 0000000..89e2e8d --- /dev/null +++ b/app/assets/stylesheets/products.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the products controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/testpage.scss b/app/assets/stylesheets/testpage.scss new file mode 100644 index 0000000..632056a --- /dev/null +++ b/app/assets/stylesheets/testpage.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the testpage controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/users/omniauth_callbacks.scss b/app/assets/stylesheets/users/omniauth_callbacks.scss new file mode 100644 index 0000000..155643a --- /dev/null +++ b/app/assets/stylesheets/users/omniauth_callbacks.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Users::OmniauthCallbacks controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/account/products_controller.rb b/app/controllers/account/products_controller.rb new file mode 100644 index 0000000..3afc8c3 --- /dev/null +++ b/app/controllers/account/products_controller.rb @@ -0,0 +1,40 @@ +class Account::ProductsController < ApplicationController + def index + @products = Product.all + end + + def new + @product = Product.new + end + + def edit + @product = Product.find(params[:id]) + end + + def update + @product = Product.find(params[:id]) + + if @product.update(product_params) + redirect_to account_products_path + else + render :edit + end + end + + def create + @product = Product.new(product_params) + + if @product.save + redirect_to account_products_path + else + render :new + end + end + + private + + def product_params + params.require(:product).permit(:title, :description, :quantity, :price) + end + +end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1c07694..b7dbf77 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,3 +1,14 @@ class ApplicationController < ActionController::Base protect_from_forgery with: :exception + + before_action :configure_permitted_parameters, if: :devise_controller? + + protected + + def configure_permitted_parameters + devise_parameter_sanitizer.permit(:sign_up) do |u| + u.permit(:name, :email, :password, :password_confirmation) + end + end + end diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb new file mode 100644 index 0000000..f213707 --- /dev/null +++ b/app/controllers/products_controller.rb @@ -0,0 +1,9 @@ +class ProductsController < ApplicationController + def index + @products = Product.all + end + + def show + @product = Product.find(params[:id]) + end +end diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb new file mode 100644 index 0000000..c33a434 --- /dev/null +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -0,0 +1,14 @@ +class Users::OmniauthCallbacksController < ApplicationController + def google_oauth2 + + @user = User.find_for_google_oauth2(request.env["omniauth.auth"], current_user) + + 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"] + redirect_to new_user_registration_url + end + end +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000..f610610 --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,5 @@ +class UsersController < ApplicationController + def index + @users = User.all + end +end diff --git a/app/helpers/account/products_helper.rb b/app/helpers/account/products_helper.rb new file mode 100644 index 0000000..1797573 --- /dev/null +++ b/app/helpers/account/products_helper.rb @@ -0,0 +1,2 @@ +module Account::ProductsHelper +end diff --git a/app/helpers/products_helper.rb b/app/helpers/products_helper.rb new file mode 100644 index 0000000..ab5c42b --- /dev/null +++ b/app/helpers/products_helper.rb @@ -0,0 +1,2 @@ +module ProductsHelper +end diff --git a/app/helpers/testpage_helper.rb b/app/helpers/testpage_helper.rb new file mode 100644 index 0000000..501d6ae --- /dev/null +++ b/app/helpers/testpage_helper.rb @@ -0,0 +1,2 @@ +module TestpageHelper +end diff --git a/app/helpers/users/omniauth_callbacks_helper.rb b/app/helpers/users/omniauth_callbacks_helper.rb new file mode 100644 index 0000000..7c6e5e0 --- /dev/null +++ b/app/helpers/users/omniauth_callbacks_helper.rb @@ -0,0 +1,2 @@ +module Users::OmniauthCallbacksHelper +end diff --git a/app/models/product.rb b/app/models/product.rb new file mode 100644 index 0000000..4f9439b --- /dev/null +++ b/app/models/product.rb @@ -0,0 +1,5 @@ +class Product < ApplicationRecord + + + belongs_to :owner, class_name: "User", foreign_key: :user_id +end diff --git a/app/models/settings.rb b/app/models/settings.rb new file mode 100644 index 0000000..e9a7e9d --- /dev/null +++ b/app/models/settings.rb @@ -0,0 +1,4 @@ +class Settings < Settingslogic + source "#{Rails.root}/config/application.yml" + namespace Rails.env +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..ec5cd09 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,32 @@ +class User < ApplicationRecord + # Include default devise modules. Others available are: + # :confirmable, :lockable, :timeoutable and :omniauthable + + + has_many :products + + devise :database_authenticatable, :registerable, + :recoverable, :rememberable, :trackable, :validatable, + :omniauthable, :omniauth_providers => [:google_oauth2] + + def self.find_for_google_oauth2(access_token, signed_in_resource=nil) + data = access_token.info + user = User.where(:provider => access_token.provider, :uid => access_token.uid ).first + if user + return user + else + registered_user = User.where(:email => access_token.info.email).first + if registered_user + return registered_user + else + user = User.create(name: data["name"], + provider:access_token.provider, + email: data["email"], + uid: access_token.uid , + password: Devise.friendly_token[0,20] + ) + end + end + end + +end diff --git a/app/views/account/products/edit.html.erb b/app/views/account/products/edit.html.erb new file mode 100644 index 0000000..6d2d622 --- /dev/null +++ b/app/views/account/products/edit.html.erb @@ -0,0 +1,25 @@ +<%= form_for [:account, @product] do |f| %> + +
Welcome <%= @email %>!
+ +You can confirm your account email through the link below:
+ +<%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %>
diff --git a/app/views/devise/mailer/password_change.html.erb b/app/views/devise/mailer/password_change.html.erb new file mode 100644 index 0000000..b41daf4 --- /dev/null +++ b/app/views/devise/mailer/password_change.html.erb @@ -0,0 +1,3 @@ +Hello <%= @resource.email %>!
+ +We're contacting you to notify you that your password has been changed.
diff --git a/app/views/devise/mailer/reset_password_instructions.html.erb b/app/views/devise/mailer/reset_password_instructions.html.erb new file mode 100644 index 0000000..f667dc1 --- /dev/null +++ b/app/views/devise/mailer/reset_password_instructions.html.erb @@ -0,0 +1,8 @@ +Hello <%= @resource.email %>!
+ +Someone has requested a link to change your password. You can do this through the link below.
+ +<%= link_to 'Change my password', edit_password_url(@resource, reset_password_token: @token) %>
+ +If you didn't request this, please ignore this email.
+Your password won't change until you access the link above and create a new one.
diff --git a/app/views/devise/mailer/unlock_instructions.html.erb b/app/views/devise/mailer/unlock_instructions.html.erb new file mode 100644 index 0000000..41e148b --- /dev/null +++ b/app/views/devise/mailer/unlock_instructions.html.erb @@ -0,0 +1,7 @@ +Hello <%= @resource.email %>!
+ +Your account has been locked due to an excessive number of unsuccessful sign in attempts.
+ +Click the link below to unlock your account:
+ +<%= link_to 'Unlock my account', unlock_url(@resource, unlock_token: @token) %>
diff --git a/app/views/devise/passwords/edit.html.erb b/app/views/devise/passwords/edit.html.erb new file mode 100644 index 0000000..6a796b0 --- /dev/null +++ b/app/views/devise/passwords/edit.html.erb @@ -0,0 +1,25 @@ +Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %>
+ +<%= link_to "Back", :back %> diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb new file mode 100644 index 0000000..e728a17 --- /dev/null +++ b/app/views/devise/registrations/new.html.erb @@ -0,0 +1,34 @@ +