Skip to content
This repository has been archived by the owner on Sep 20, 2019. It is now read-only.

Commit

Permalink
Merge pull request #31 from enpitut2018/1120/qqhann/create-user
Browse files Browse the repository at this point in the history
1120/qqhann/create user
  • Loading branch information
qqpann authored Nov 21, 2018
2 parents 2e8c281 + 3876b48 commit a8daca2
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ gem 'dotenv-rails' #環境変数を簡単に利用できるgem
gem 'rails', '~> 5.1.6'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use postgres in production
gem 'pg'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/sessions.coffee
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/sessions.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Sessions controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include SessionsHelper
end
40 changes: 40 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class SessionsController < ApplicationController
def new
end

def create
#request.env['omniauth.auth']はTwitter認証で得た情報を格納するもの
user = User.find_or_create_from_auth_hash(request.env['omniauth.auth'])
if user
log_in user
# remember(user) ?
redirect_to root_path, notice: "ログインしました。"
else
redirect_to root_path, notice: "失敗しました。"
end
end

# def create
# user = User.find_by(email: params[:session][:email].downcase)
# if user && user.authenticate(params[:session][:password])
# if user.activated?
# log_in user
# params[:session][:remember_me] == '1' ? remember(user) : forget(user)
# redirect_back_or user # = redirect_to user_url(@user)
# else
# message = "Account not activated."
# message += "Check your email for the activation link."
# flash[:warning] = message
# redirect_to root_url
# end
# else
# flash.now[:danger] = 'Invalid email/password combination'
# render 'new'
# end
# end

def destroy
log_out if logged_in?
redirect_to root_url
end
end
58 changes: 58 additions & 0 deletions app/helpers/sessions_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module SessionsHelper
# Login with the passed user
def log_in(user)
session[:user_id] = user.id
end

# remember user session for long term
def remember(user)
user.remember
cookies.permanent.signed[:user_id] = user.id
cookies.permanent[:remember_token] = user.remember_token
end

# return true if the passed user is currently logged in user
def current_user?(user)
user == current_user
end

def current_user
if (user_id = session[:user_id])
@current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by(id: user_id)
if user && user.authenticated?(:remember, cookies[:remember_token])
log_in user
@current_user = user
end
end
end

def logged_in?
!current_user.nil?
end

# permanently abandon session
def forget(user)
user.forget
cookies.delete(:user_id)
cookies.delete(:remember_token)
end

def log_out
forget(current_user)
session.delete(:user_id)
@current_user = nil
end

# redirect to what url that it remembers
def redirect_back_or(default)
redirect_to(session[:forwarding_url] || default)
session.delete(:forwarding_url)
end

# remember what url you tried to access
def store_location
session[:forwarding_url] = request.original_url if request.get?
end
end
2 changes: 2 additions & 0 deletions app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Sessions#new</h1>
<p>Find me in app/views/sessions/new.html.erb</p>
6 changes: 6 additions & 0 deletions app/views/static_pages/home.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<h1>Collatter</h1>
<%= link_to 'Twitterでログイン', '/auth/twitter' %>
<% if logged_in? %>
ログインしています
<% else %>
ログインしてください
<% end %>


<p>検索欄がここに入る</p>

Expand Down
4 changes: 3 additions & 1 deletion config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ test:

production:
<<: *default
database: db/production.sqlite3
adapter: postgresql
encoding: unicode
pool: 5
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
Rails.application.routes.draw do
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'

get 'user/show'

get 'user/index'
Expand All @@ -8,7 +12,7 @@
get 'user/edit'

#このpathを通して認証が行われる。
get 'auth/:provider/callback' => 'users#create'
get 'auth/:provider/callback' => 'sessions#create'

resources :collas
resources :templates
Expand Down
9 changes: 9 additions & 0 deletions test/controllers/sessions_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'test_helper'

class SessionsControllerTest < ActionDispatch::IntegrationTest
test "should get new" do
get sessions_new_url
assert_response :success
end

end

0 comments on commit a8daca2

Please sign in to comment.