Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 489d5d4

Browse files
committed
Added user model and jwt
1 parent e2f9396 commit 489d5d4

32 files changed

+531
-10
lines changed

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
.byebug_history
2222
public/*
2323
.idea
24+
client/*.log

‎Gemfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ gem 'jbuilder', '~> 2.5'
3030
# Use Redis adapter to run Action Cable in production
3131
# gem 'redis', '~> 3.0'
3232
# Use ActiveModel has_secure_password
33-
# gem 'bcrypt', '~> 3.1.7'
34-
33+
gem 'bcrypt', '~> 3.1.7'
34+
gem'delayed_job_active_record'
3535
# Use Capistrano for deployment
3636
# gem 'capistrano-rails', group: :development
3737

3838
gem 'rack-cors', :require => 'rack/cors'
3939
gem 'graphql'
4040
gem 'graphiql-rails'
41+
gem 'jwt'
4142

4243
group :development, :test do
4344
# Call 'byebug' anywhere in the code to stop execution and get a debugger console

‎Gemfile.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ GEM
3939
minitest (~> 5.1)
4040
tzinfo (~> 1.1)
4141
arel (7.1.4)
42+
bcrypt (3.1.11)
4243
bindex (0.5.0)
4344
builder (3.2.3)
4445
byebug (9.0.6)
@@ -50,6 +51,11 @@ GEM
5051
execjs
5152
coffee-script-source (1.12.2)
5253
concurrent-ruby (1.0.5)
54+
delayed_job (4.1.2)
55+
activesupport (>= 3.0, < 5.1)
56+
delayed_job_active_record (4.1.1)
57+
activerecord (>= 3.0, < 5.1)
58+
delayed_job (>= 3.0, < 5)
5359
erubis (2.7.0)
5460
execjs (2.7.0)
5561
ffi (1.9.18)
@@ -66,6 +72,7 @@ GEM
6672
rails-dom-testing (>= 1, < 3)
6773
railties (>= 4.2.0)
6874
thor (>= 0.14, < 2.0)
75+
jwt (1.5.6)
6976
listen (3.0.8)
7077
rb-fsevent (~> 0.9, >= 0.9.4)
7178
rb-inotify (~> 0.9, >= 0.9.7)
@@ -158,12 +165,15 @@ PLATFORMS
158165
ruby
159166

160167
DEPENDENCIES
168+
bcrypt (~> 3.1.7)
161169
byebug
162170
coffee-rails (~> 4.2)
171+
delayed_job_active_record
163172
graphiql-rails
164173
graphql
165174
jbuilder (~> 2.5)
166175
jquery-rails
176+
jwt
167177
listen (~> 3.0.5)
168178
puma (~> 3.0)
169179
rack-cors

‎app/assets/javascripts/users.coffee

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/

‎app/assets/stylesheets/users.scss

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 users controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
11
class ApplicationController < ActionController::Base
22
protect_from_forgery with: :null_session
3+
require 'json_web_token'
4+
5+
def authenticate_request!
6+
if !payload || !JsonWebToken.valid_payload(payload.first)
7+
return invalid_authentication
8+
end
9+
10+
load_current_user!
11+
invalid_authentication unless @current_user
12+
end
13+
14+
def invalid_authentication
15+
render json: { error: 'Invalid Request' }, status: :unauthorized
16+
end
17+
18+
private
19+
20+
def payload
21+
auth_header = request.headers['Authorization']
22+
token = auth_header.split(' ').last
23+
JsonWebToken.decode(token)
24+
rescue
25+
nil
26+
end
27+
28+
def load_current_user!
29+
@current_user = User.find_by(id: payload[0]['user_id'])
30+
end
331
end

‎app/controllers/graphql_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class GraphqlController < ApplicationController
2+
before_filter 'authenticate_request!'
3+
24
def mutations
35
query_string = params[:mutation]
46
query_variables = ensure_hash(params[:variables])

‎app/controllers/users_controller.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class UsersController < ApplicationController
2+
def login
3+
user = User.find_by(email: params[:email].to_s.downcase)
4+
5+
if user && user.authenticate(params[:password])
6+
if user.confirmed_at?
7+
auth_token = JsonWebToken.encode({ user_id: user.id })
8+
render json: { auth_token: auth_token }, status: :ok
9+
else
10+
render json: { error: 'Email not verified' }, status: :unauthorized
11+
end
12+
else
13+
render json: { error: 'Username not found' }, status: :bad_request
14+
end
15+
end
16+
17+
def create
18+
user = User.new(user_params)
19+
if user.save
20+
SendEmailJob.set(wait: 5.seconds).perform_later(user)
21+
render json: { status: 'User created successfully' }, status: :created
22+
else
23+
render json: { errors: user.errors.full_messages }, status: :bad_request
24+
end
25+
end
26+
27+
def confirm
28+
token = params[:token].to_s
29+
30+
user = User.find_by(confirmation_token: token)
31+
32+
if user.present? && user.confirmation_token_valid?
33+
user.mark_as_confirmed!
34+
render json: { status: 'User confirmed successfully' }, status: :ok
35+
else
36+
render json: { status: 'Invalid token' }, status: :not_found
37+
end
38+
end
39+
40+
private
41+
42+
def user_params
43+
params.require(:user).permit(:email, :password, :password_confirmation)
44+
end
45+
end

‎app/helpers/users_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module UsersHelper
2+
end

‎app/jobs/send_email_job.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class SendEmailJob < ApplicationJob
2+
queue_as :default
3+
4+
def perform(user)
5+
# Do something later
6+
@user = user
7+
UserMailer.confirmation_email(user).deliver_later
8+
end
9+
end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /