From 8693ba5860f1e0e5954f7d01422cd9fdd7662057 Mon Sep 17 00:00:00 2001 From: Zachary Chai Date: Tue, 18 Oct 2016 13:51:05 -0400 Subject: [PATCH] personalized rooms page --- app/assets/stylesheets/landing.scss | 6 ++++++ app/controllers/landing_controller.rb | 13 +++++++++++++ app/controllers/sessions_controller.rb | 2 +- app/models/user.rb | 5 +++++ app/views/landing/room.html.erb | 19 +++++++++++++++++++ config/routes.rb | 1 + .../20161017203809_add_username_to_user.rb | 6 ++++++ db/schema.rb | 4 +++- 8 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 app/views/landing/room.html.erb create mode 100644 db/migrate/20161017203809_add_username_to_user.rb diff --git a/app/assets/stylesheets/landing.scss b/app/assets/stylesheets/landing.scss index 0dbe3ca0..3bcfc498 100644 --- a/app/assets/stylesheets/landing.scss +++ b/app/assets/stylesheets/landing.scss @@ -5,3 +5,9 @@ // Bootstrap @import "bootstrap-sprockets"; @import "bootstrap"; + +.room { + .room-link { + margin: 0 auto; + } +} diff --git a/app/controllers/landing_controller.rb b/app/controllers/landing_controller.rb index b02805b7..08c3ff30 100644 --- a/app/controllers/landing_controller.rb +++ b/app/controllers/landing_controller.rb @@ -5,4 +5,17 @@ class LandingController < ApplicationController @meeting_token = params[:id] || @meeting_token = rand.to_s[2..10] @meeting_url = meeting_url(@meeting_token) end + + def room + @room_name = params[:name] + @user = User.find_by(username: @room_name) + if @user.nil? + redirect_to root_path + end + end + + def admin? + @user == current_user + end + helper_method :admin? end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index e2ab24aa..83820889 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -2,9 +2,9 @@ class SessionsController < ApplicationController def create @user = User.from_omniauth(request.env['omniauth.auth']) session[:user_id] = @user.id + redirect_to controller: 'landing', action: 'room', name: @user.username rescue => e logger.error "Error authenticating via omniauth: #{e}" - ensure redirect_to root_path end diff --git a/app/models/user.rb b/app/models/user.rb index 80c35a02..0941600e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,8 +2,13 @@ class User < ApplicationRecord def self.from_omniauth(auth_hash) user = find_or_create_by(uid: auth_hash['uid'], provider: auth_hash['provider']) + user.username = self.send("#{auth_hash['provider']}_username", auth_hash) rescue nil user.name = auth_hash['info']['name'] user.save! user end + + def self.twitter_username(auth_hash) + auth_hash['info']['nickname'] + end end diff --git a/app/views/landing/room.html.erb b/app/views/landing/room.html.erb new file mode 100644 index 00000000..fc853c47 --- /dev/null +++ b/app/views/landing/room.html.erb @@ -0,0 +1,19 @@ +
+
+
+ <% if admin? %> + <%= current_user.name %> + <%= link_to "Logout", "/logout" %> + <% else %> + <%= link_to "Login", "/auth/twitter" %> + <% end %> +
+
+
+
+ +
+
+
diff --git a/config/routes.rb b/config/routes.rb index e1e04b54..09aa1490 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ Rails.application.routes.draw do get 'meeting(/:id)', to: 'landing#index' get '/auth/:provider/callback', to: 'sessions#create' get '/logout', to: 'sessions#destroy' + get '/rooms/:name', to: 'landing#room' root to: 'landing#index' # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html diff --git a/db/migrate/20161017203809_add_username_to_user.rb b/db/migrate/20161017203809_add_username_to_user.rb new file mode 100644 index 00000000..58cde222 --- /dev/null +++ b/db/migrate/20161017203809_add_username_to_user.rb @@ -0,0 +1,6 @@ +class AddUsernameToUser < ActiveRecord::Migration[5.0] + def change + add_column :users, :username, :string + add_index :users, :username, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 1834e3e8..9d2bd52c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20161017160526) do +ActiveRecord::Schema.define(version: 20161017203809) do create_table "users", force: :cascade do |t| t.string "provider", null: false @@ -18,9 +18,11 @@ ActiveRecord::Schema.define(version: 20161017160526) do t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "username" t.index ["provider", "uid"], name: "index_users_on_provider_and_uid", unique: true t.index ["provider"], name: "index_users_on_provider" t.index ["uid"], name: "index_users_on_uid" + t.index ["username"], name: "index_users_on_username", unique: true end end