-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Amber Lynn | Media Ranker Revisited - Edges #40
base: master
Are you sure you want to change the base?
Changes from all commits
5882f86
d03f92e
c0f5960
71cdc12
d226f12
239c62d
f7a27df
681401e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,5 +13,8 @@ | |
!/log/.keep | ||
!/tmp/.keep | ||
|
||
# ignoring the env file | ||
.env | ||
|
||
# Ignore Byebug command history file. | ||
.byebug_history |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,67 @@ | ||
class SessionsController < ApplicationController | ||
def login_form | ||
end | ||
|
||
def login | ||
username = params[:username] | ||
if username and user = User.find_by(username: username) | ||
session[:user_id] = user.id | ||
def create | ||
auth_hash = request.env['omniauth.auth'] | ||
user = User.find_by(uid: auth_hash[:uid], provider: 'github') | ||
if user | ||
# User was found in the database | ||
flash[:status] = :success | ||
flash[:result_text] = "Successfully logged in as existing user #{user.username}" | ||
flash[:messages] = user.errors.messages | ||
flash[:result_text] = "Logged in as returning user #{user.name}" | ||
|
||
else | ||
user = User.new(username: username) | ||
# User doesn't match anything in the DB | ||
# TODO: Attempt to create a new user | ||
user = User.new(username: auth_hash[:info][:nickname], uid: auth_hash[:uid], provider: 'github') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is no longer a TODO |
||
|
||
if user.save | ||
session[:user_id] = user.id | ||
flash[:status] = :success | ||
flash[:result_text] = "Successfully created new user #{user.username} with ID #{user.id}" | ||
flash[:messages] = user.errors.messages | ||
flash[:result_text] = "Successfully created username #{user.username}" | ||
else | ||
flash.now[:status] = :failure | ||
flash.now[:result_text] = "Could not log in" | ||
flash.now[:messages] = user.errors.messages | ||
render "login_form", status: :bad_request | ||
flash[:status] = :error | ||
flash[:messages] = user.errors.messages | ||
flash[:result_text] = "Could not create new user #{user.username}" | ||
return | ||
end | ||
|
||
session[:user_id] = user.id | ||
redirect_to root_path | ||
end | ||
end | ||
|
||
def destroy | ||
session[:user_id] = nil | ||
flash[:success] = "Successfully logged out!" | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this and the |
||
redirect_to root_path | ||
end | ||
|
||
def login_form | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we're using OAuth, do we still need the login form? |
||
# | ||
# def login | ||
# username = params[:username] | ||
# if username and user = User.find_by(username: username) | ||
# session[:user_id] = user.id | ||
# flash[:status] = :success | ||
# flash[:result_text] = "Successfully logged in as existing user #{user.username}" | ||
# else | ||
# user = User.new(username: username) | ||
# if user.save | ||
# session[:user_id] = user.id | ||
# flash[:status] = :success | ||
# flash[:result_text] = "Successfully created new user #{user.username} with ID #{user.id}" | ||
# else | ||
# flash.now[:status] = :failure | ||
# flash.now[:result_text] = "Could not log in" | ||
# flash.now[:messages] = user.errors.messages | ||
# render "login_form", status: :bad_request | ||
# return | ||
# end | ||
# end | ||
# redirect_to root_path | ||
# end | ||
|
||
def logout | ||
session[:user_id] = nil | ||
flash[:status] = :success | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
<h2>Add a new work</h2> | ||
|
||
<%= render partial: "form" %> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Rails.application.config.middleware.use OmniAuth::Builder do | ||
provider :github, ENV["GITHUB_CLIENT_ID"], ENV["GITHUB_CLIENT_SECRET"], scope: "user:email" | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddNameToUsers < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :users, :name, :string | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class AddUidToUsers < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :users, :email, :string | ||
add_column :users, :uid, :integer, null: false | ||
add_column :users, :provider, :string, null: false | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,39 @@ | ||
require 'test_helper' | ||
|
||
describe UsersController do | ||
describe "index" do | ||
it "goes to index view" do | ||
|
||
get users_path | ||
|
||
assert_response :success | ||
end | ||
|
||
it "renders users" do | ||
users = User.all | ||
|
||
get users_path | ||
|
||
expect( response.body ).must_include("dan") | ||
expect( response.body ).must_include("kari") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is more work than we typically need to do for controller tests - you're looking at the rendered HTML, and making sure it includes these two values. If your fixture data or view code changes, this test will also need to change. |
||
end | ||
|
||
it "response with success with no users" do | ||
count = User.all.count | ||
users = User.all | ||
users.each do |user| | ||
user.votes.destroy_all | ||
user.destroy | ||
end | ||
|
||
get users_path | ||
|
||
assert_response :success | ||
end | ||
end | ||
|
||
describe "show" do | ||
|
||
|
||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't need to set error messages if nothing went wrong