This repository has been archived by the owner on Sep 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from enpitut2018/1120/qqhann/create-user
1120/qqhann/create user
- Loading branch information
Showing
11 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
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
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
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/ |
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
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/ |
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
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 |
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
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 |
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
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 |
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
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> |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,4 +22,6 @@ test: | |
|
||
production: | ||
<<: *default | ||
database: db/production.sqlite3 | ||
adapter: postgresql | ||
encoding: unicode | ||
pool: 5 |
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
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
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 |