Skip to content

Commit 9fba9df

Browse files
committed
Merging finished authentication branch into develop. Features full user name and password sign up, sign in authentication and requires users to be signed in to perform higher order actions such as deletion or editing of tags, articles, comments and authors.
2 parents f1cddfe + 6ffc79d commit 9fba9df

37 files changed

+975
-9
lines changed

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ ruby '2.6.5'
66
# Paperclip as per TOP specifications
77
gem 'paperclip'
88

9+
# Sorcery for simple authentication
10+
gem 'sorcery'
11+
912
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
1013
gem 'rails', '~> 5.2.3'
1114
# Use sqlite3 as the database for Active Record

Gemfile.lock

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ GEM
4747
archive-zip (0.12.0)
4848
io-like (~> 0.3.0)
4949
arel (9.0.0)
50+
bcrypt (3.1.13)
5051
bindex (0.8.1)
5152
bootsnap (1.4.5)
5253
msgpack (~> 1.0)
@@ -76,6 +77,8 @@ GEM
7677
crass (1.0.6)
7778
erubi (1.9.0)
7879
execjs (2.7.0)
80+
faraday (1.0.0)
81+
multipart-post (>= 1.2, < 3)
7982
ffi (1.12.2)
8083
globalid (0.4.2)
8184
activesupport (>= 4.2.0)
@@ -84,6 +87,7 @@ GEM
8487
io-like (0.3.0)
8588
jbuilder (2.9.1)
8689
activesupport (>= 4.2.0)
90+
jwt (2.2.1)
8791
listen (3.1.5)
8892
rb-fsevent (~> 0.9, >= 0.9.4)
8993
rb-inotify (~> 0.9, >= 0.9.7)
@@ -104,9 +108,19 @@ GEM
104108
mini_portile2 (2.4.0)
105109
minitest (5.14.0)
106110
msgpack (1.3.3)
111+
multi_json (1.14.1)
112+
multi_xml (0.6.0)
113+
multipart-post (2.1.1)
107114
nio4r (2.5.2)
108115
nokogiri (1.10.7)
109116
mini_portile2 (~> 2.4.0)
117+
oauth (0.5.4)
118+
oauth2 (1.4.3)
119+
faraday (>= 0.8, < 2.0)
120+
jwt (>= 1.0, < 3.0)
121+
multi_json (~> 1.3)
122+
multi_xml (~> 0.5)
123+
rack (>= 1.2, < 3)
110124
paperclip (6.1.0)
111125
activemodel (>= 4.2.0)
112126
activesupport (>= 4.2.0)
@@ -163,6 +177,10 @@ GEM
163177
selenium-webdriver (3.142.7)
164178
childprocess (>= 0.5, < 4.0)
165179
rubyzip (>= 1.2.2)
180+
sorcery (0.14.0)
181+
bcrypt (~> 3.1)
182+
oauth (~> 0.4, >= 0.4.4)
183+
oauth2 (~> 1.0, >= 0.8.0)
166184
spring (2.1.0)
167185
spring-watcher-listen (2.0.1)
168186
listen (>= 2.7, < 4.0)
@@ -214,6 +232,7 @@ DEPENDENCIES
214232
rails (~> 5.2.3)
215233
sass-rails (~> 5.0)
216234
selenium-webdriver
235+
sorcery
217236
spring
218237
spring-watcher-listen (~> 2.0.0)
219238
sqlite3
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Place all the behaviors and hooks related to the matching controller here.
2+
# All this logic will automatically be available in application.js.
3+
# You can use CoffeeScript in this file: http://coffeescript.org/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the AuthorSessions controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/

app/assets/stylesheets/styles.css.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ a {
1919

2020
img {
2121
border-radius: 4px;
22+
max-width: 100%;
2223
}
2324

2425
.clear {
@@ -28,7 +29,7 @@ img {
2829
}
2930

3031
#container {
31-
width: 75%;
32+
width: auto;
3233
margin: 0 auto;
3334
background: #f0f0f0;
3435
padding: 20px 40px;

app/controllers/articles_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
class ArticlesController < ApplicationController
2-
32
include ArticlesHelper
43

4+
before_action :require_login, except: [:index, :show]
5+
56
def index
67
@articles = Article.all
78
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class AuthorSessionsController < ApplicationController
2+
def new
3+
end
4+
5+
def create
6+
if login(params[:email], params[:password])
7+
redirect_back_or_to(articles_path, notice: 'Logged in successfully.')
8+
else
9+
flash.now.alert = 'Login failed. Please try again.'
10+
render action: :new
11+
end
12+
end
13+
14+
def destroy
15+
logout
16+
redirect_to(:authors, notice: 'Logged Out!')
17+
end
18+
end
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
class AuthorsController < ApplicationController
2+
before_action :set_author, only: [:show, :edit, :update, :destroy]
3+
before_action :zero_authors_or_authenticated, only: [:new, :create]
4+
before_action :require_login, except: [:new, :create]
5+
6+
def zero_authors_or_authenticated
7+
unless Author.count == 0 || current_user
8+
redirect_to root_path
9+
return false
10+
end
11+
end
12+
13+
# GET /authors
14+
# GET /authors.json
15+
def index
16+
@authors = Author.all
17+
end
18+
19+
# GET /authors/1
20+
# GET /authors/1.json
21+
def show
22+
end
23+
24+
# GET /authors/new
25+
def new
26+
@author = Author.new
27+
end
28+
29+
# GET /authors/1/edit
30+
def edit
31+
end
32+
33+
# POST /authors
34+
# POST /authors.json
35+
def create
36+
@author = Author.new(author_params)
37+
38+
respond_to do |format|
39+
if @author.save
40+
format.html { redirect_to @author, notice: 'Author was successfully created.' }
41+
format.json { render :show, status: :created, location: @author }
42+
else
43+
format.html { render :new }
44+
format.json { render json: @author.errors, status: :unprocessable_entity }
45+
end
46+
end
47+
end
48+
49+
# PATCH/PUT /authors/1
50+
# PATCH/PUT /authors/1.json
51+
def update
52+
respond_to do |format|
53+
if @author.update(author_params)
54+
format.html { redirect_to @author, notice: 'Author was successfully updated.' }
55+
format.json { render :show, status: :ok, location: @author }
56+
else
57+
format.html { render :edit }
58+
format.json { render json: @author.errors, status: :unprocessable_entity }
59+
end
60+
end
61+
end
62+
63+
# DELETE /authors/1
64+
# DELETE /authors/1.json
65+
def destroy
66+
@author.destroy
67+
respond_to do |format|
68+
format.html { redirect_to authors_url, notice: 'Author was successfully destroyed.' }
69+
format.json { head :no_content }
70+
end
71+
end
72+
73+
private
74+
# Use callbacks to share common setup or constraints between actions.
75+
def set_author
76+
@author = Author.find(params[:id])
77+
end
78+
79+
# Never trust parameters from the scary internet, only allow the white list through.
80+
def author_params
81+
params.require(:author).permit(:username, :email, :password, :password_confirmation)
82+
end
83+
end

app/controllers/comments_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class CommentsController < ApplicationController
2+
before_action :require_login, except: [:create]
3+
24
def create
35
@comment = Comment.new(comment_params)
46
@comment.article_id = params[:article_id]

app/controllers/tags_controller.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
class TagsController < ApplicationController
2+
before_action :require_login, only: [:destroy]
3+
24
def index
35
@tags = Tag.all
46
end
57

68
def show
79
@tag = Tag.find(params[:id])
810
end
11+
12+
def destroy
13+
@tag = Tag.find(params[:id])
14+
@tag.destroy
15+
flash.notice = "The '#{@tag.name}' tag has been deleted."
16+
redirect_to tags_path
17+
end
918
end

0 commit comments

Comments
 (0)