WIP: Karol's implementation #1
9 changed files with 18 additions and 13 deletions
|
@ -3,7 +3,7 @@ class SessionsController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@user = User.find_by(username: params[:username])
|
@user = User.find_by(email: params[:email])
|
||||||
if @user && @user.authenticate(params[:password])
|
if @user && @user.authenticate(params[:password])
|
||||||
session[:user_id] = @user.id
|
session[:user_id] = @user.id
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,7 +4,7 @@ class UsersController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@user = User.create(params.require(:user).permit(:username, :password))
|
@user = User.create(params.require(:user).permit(:email, :password))
|
||||||
session[:user_id] = @user.id
|
session[:user_id] = @user.id
|
||||||
redirect_to '/welcome'
|
redirect_to '/welcome'
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<h1>Login</h1>
|
<h1>Login</h1>
|
||||||
<%= form_tag '/login' do %>
|
<%= form_tag '/login' do %>
|
||||||
<%= label_tag :username%>
|
<%= label_tag :email%>
|
||||||
<%= text_field_tag :username %>
|
<%= text_field_tag :email %>
|
||||||
<%= label_tag :password%>
|
<%= label_tag :password%>
|
||||||
<%= password_field_tag :password%>
|
<%= password_field_tag :password%>
|
||||||
<%= submit_tag "Login"%>
|
<%= submit_tag "Login"%>
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<h1>Welcome</h1>
|
<h1>Welcome</h1>
|
||||||
<% if logged_in? %>
|
<% if logged_in? %>
|
||||||
<h1>You are Logged In, <%= current_user.username %></h1>
|
<h1>You are Logged In, <%= current_user.email %></h1>
|
||||||
<%= button_to "Logout", '/logout', method: :get%>
|
<%= button_to "Logout", '/logout', method: :get%>
|
||||||
<%else%>
|
<%else%>
|
||||||
<%= button_to "Login", '/login', method: :get%>
|
<%= button_to "Login", '/login', method: :get%>
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<h1>Sign Up</h1>
|
<h1>Sign Up</h1>
|
||||||
<%= form_for @user do |f|%>
|
<%= form_for @user do |f|%>
|
||||||
<%= f.label :username%><br>
|
<%= f.label :email%><br>
|
||||||
<%= f.text_field :username%><br>
|
<%= f.text_field :email%><br>
|
||||||
<%= f.label :password%><br>
|
<%= f.label :password%><br>
|
||||||
<%= f.password_field :password%><br>
|
<%= f.password_field :password%><br>
|
||||||
<%= f.submit %>
|
<%= f.submit %>
|
||||||
|
|
5
db/migrate/20210320212922_change_username_to_email.rb
Normal file
5
db/migrate/20210320212922_change_username_to_email.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class ChangeUsernameToEmail < ActiveRecord::Migration[6.1]
|
||||||
|
def change
|
||||||
|
rename_column :users, :username, :email
|
||||||
|
end
|
||||||
|
end
|
4
db/schema.rb
generated
4
db/schema.rb
generated
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 2021_03_20_130542) do
|
ActiveRecord::Schema.define(version: 2021_03_20_212922) do
|
||||||
|
|
||||||
create_table "authors", force: :cascade do |t|
|
create_table "authors", force: :cascade do |t|
|
||||||
t.string "first_name"
|
t.string "first_name"
|
||||||
|
@ -39,7 +39,7 @@ ActiveRecord::Schema.define(version: 2021_03_20_130542) do
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table "users", force: :cascade do |t|
|
create_table "users", force: :cascade do |t|
|
||||||
t.string "username"
|
t.string "email"
|
||||||
t.string "password_digest"
|
t.string "password_digest"
|
||||||
t.datetime "created_at", precision: 6, null: false
|
t.datetime "created_at", precision: 6, null: false
|
||||||
t.datetime "updated_at", precision: 6, null: false
|
t.datetime "updated_at", precision: 6, null: false
|
||||||
|
|
|
@ -3,12 +3,12 @@ require 'rails_helper'
|
||||||
RSpec.describe ApplicationController do
|
RSpec.describe ApplicationController do
|
||||||
describe 'current_user' do
|
describe 'current_user' do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
User.create(username: 'karol.selak@gmail.com', password: 'abcde')
|
User.create(email: 'karol.selak@gmail.com', password: 'abcde')
|
||||||
end
|
end
|
||||||
context 'when a user is logged in' do
|
context 'when a user is logged in' do
|
||||||
it 'returns the user' do
|
it 'returns the user' do
|
||||||
# TODO
|
# TODO
|
||||||
# expect(current_user.username).to eql('karol.selak@gmail.com')
|
# expect(current_user.email).to eql('karol.selak@gmail.com')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,11 +12,11 @@ RSpec.describe UsersController do
|
||||||
User.destroy_all
|
User.destroy_all
|
||||||
end
|
end
|
||||||
subject {
|
subject {
|
||||||
get :create, params: {user: {username: 'karol.selak@gmail.com', password: 'abcde'}}
|
get :create, params: {user: {email: 'karol.selak@gmail.com', password: 'abcde'}}
|
||||||
}
|
}
|
||||||
it 'creates a user' do
|
it 'creates a user' do
|
||||||
subject
|
subject
|
||||||
expect(User.where(username: 'karol.selak@gmail.com').size).to eql(1)
|
expect(User.where(email: 'karol.selak@gmail.com').size).to eql(1)
|
||||||
end
|
end
|
||||||
it 'redirects to /welcome' do
|
it 'redirects to /welcome' do
|
||||||
subject
|
subject
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue