Permalink
Browse files

created db, models, auth bcrypt

  • Loading branch information...
annamgithub committed Nov 7, 2019
1 parent 4f59f5f commit fd75cd2a378ae1f3a7ed4745a2d87f33da7b54a8
Showing with 1,284 additions and 0 deletions.
  1. BIN .DS_Store
  2. +22 −0 flashcards/.gitignore
  3. +1 −0 flashcards/.ruby-version
  4. +46 −0 flashcards/Gemfile
  5. +170 −0 flashcards/Gemfile.lock
  6. +6 −0 flashcards/Rakefile
  7. +4 −0 flashcards/app/channels/application_cable/channel.rb
  8. +4 −0 flashcards/app/channels/application_cable/connection.rb
  9. +28 −0 flashcards/app/controllers/application_controller.rb
  10. +26 −0 flashcards/app/controllers/authentication_controller.rb
  11. 0 flashcards/app/controllers/concerns/.keep
  12. +52 −0 flashcards/app/controllers/users_controller.rb
  13. +7 −0 flashcards/app/jobs/application_job.rb
  14. +4 −0 flashcards/app/mailers/application_mailer.rb
  15. +3 −0 flashcards/app/models/application_record.rb
  16. +4 −0 flashcards/app/models/card.rb
  17. 0 flashcards/app/models/concerns/.keep
  18. +4 −0 flashcards/app/models/game.rb
  19. +8 −0 flashcards/app/models/user.rb
  20. +13 −0 flashcards/app/views/layouts/mailer.html.erb
  21. +1 −0 flashcards/app/views/layouts/mailer.text.erb
  22. +105 −0 flashcards/bin/bundle
  23. +9 −0 flashcards/bin/rails
  24. +9 −0 flashcards/bin/rake
  25. +33 −0 flashcards/bin/setup
  26. +17 −0 flashcards/bin/spring
  27. +5 −0 flashcards/config.ru
  28. +37 −0 flashcards/config/application.rb
  29. +4 −0 flashcards/config/boot.rb
  30. +10 −0 flashcards/config/cable.yml
  31. +1 −0 flashcards/config/credentials.yml.enc
  32. +85 −0 flashcards/config/database.yml
  33. +5 −0 flashcards/config/environment.rb
  34. +52 −0 flashcards/config/environments/development.rb
  35. +105 −0 flashcards/config/environments/production.rb
  36. +48 −0 flashcards/config/environments/test.rb
  37. +8 −0 flashcards/config/initializers/application_controller_renderer.rb
  38. +7 −0 flashcards/config/initializers/backtrace_silencers.rb
  39. +16 −0 flashcards/config/initializers/cors.rb
  40. +4 −0 flashcards/config/initializers/filter_parameter_logging.rb
  41. +16 −0 flashcards/config/initializers/inflections.rb
  42. +4 −0 flashcards/config/initializers/mime_types.rb
  43. +14 −0 flashcards/config/initializers/wrap_parameters.rb
  44. +33 −0 flashcards/config/locales/en.yml
  45. +38 −0 flashcards/config/puma.rb
  46. +9 −0 flashcards/config/routes.rb
  47. +6 −0 flashcards/config/spring.rb
  48. +34 −0 flashcards/config/storage.yml
  49. +11 −0 flashcards/db/migrate/20191107182345_create_games.rb
  50. +12 −0 flashcards/db/migrate/20191107182549_create_cards.rb
  51. +11 −0 flashcards/db/migrate/20191107184133_create_users.rb
  52. +7 −0 flashcards/db/seeds.rb
  53. 0 flashcards/lib/tasks/.keep
  54. 0 flashcards/log/.keep
  55. +1 −0 flashcards/public/robots.txt
  56. 0 flashcards/storage/.keep
  57. +11 −0 flashcards/test/channels/application_cable/connection_test.rb
  58. 0 flashcards/test/controllers/.keep
  59. +7 −0 flashcards/test/controllers/authentication_controller_test.rb
  60. +38 −0 flashcards/test/controllers/users_controller_test.rb
  61. 0 flashcards/test/fixtures/.keep
  62. +13 −0 flashcards/test/fixtures/cards.yml
  63. 0 flashcards/test/fixtures/files/.keep
  64. +11 −0 flashcards/test/fixtures/games.yml
  65. +11 −0 flashcards/test/fixtures/users.yml
  66. 0 flashcards/test/integration/.keep
  67. 0 flashcards/test/mailers/.keep
  68. 0 flashcards/test/models/.keep
  69. +7 −0 flashcards/test/models/card_test.rb
  70. +7 −0 flashcards/test/models/game_test.rb
  71. +7 −0 flashcards/test/models/user_test.rb
  72. +13 −0 flashcards/test/test_helper.rb
  73. 0 flashcards/tmp/.keep
  74. 0 flashcards/vendor/.keep
BIN +6 KB .DS_Store
Binary file not shown.
@@ -0,0 +1,22 @@
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'

# Ignore bundler config.
/.bundle

# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep

# Ignore uploaded files in development.
/storage/*
!/storage/.keep
.byebug_history

# Ignore master key for decrypting credentials and more.
/config/master.key
@@ -0,0 +1 @@
2.6.3
@@ -0,0 +1,46 @@
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.3'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.1'
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
# Use Puma as the app server
gem 'puma', '~> 4.1'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
# gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'

# Use Active Model has_secure_password
gem 'bcrypt', '~> 3.1.7'
# Use Json Web Token (JWT) for token based authentication
gem 'jwt'



# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false

# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
# gem 'rack-cors'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
@@ -0,0 +1,170 @@
GEM
remote: https://rubygems.org/
specs:
actioncable (6.0.1)
actionpack (= 6.0.1)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (6.0.1)
actionpack (= 6.0.1)
activejob (= 6.0.1)
activerecord (= 6.0.1)
activestorage (= 6.0.1)
activesupport (= 6.0.1)
mail (>= 2.7.1)
actionmailer (6.0.1)
actionpack (= 6.0.1)
actionview (= 6.0.1)
activejob (= 6.0.1)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (6.0.1)
actionview (= 6.0.1)
activesupport (= 6.0.1)
rack (~> 2.0)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (6.0.1)
actionpack (= 6.0.1)
activerecord (= 6.0.1)
activestorage (= 6.0.1)
activesupport (= 6.0.1)
nokogiri (>= 1.8.5)
actionview (6.0.1)
activesupport (= 6.0.1)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (6.0.1)
activesupport (= 6.0.1)
globalid (>= 0.3.6)
activemodel (6.0.1)
activesupport (= 6.0.1)
activerecord (6.0.1)
activemodel (= 6.0.1)
activesupport (= 6.0.1)
activestorage (6.0.1)
actionpack (= 6.0.1)
activejob (= 6.0.1)
activerecord (= 6.0.1)
marcel (~> 0.3.1)
activesupport (6.0.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
zeitwerk (~> 2.2)
bcrypt (3.1.13)
bootsnap (1.4.5)
msgpack (~> 1.0)
builder (3.2.3)
byebug (11.0.1)
concurrent-ruby (1.1.5)
crass (1.0.5)
erubi (1.9.0)
ffi (1.11.1)
globalid (0.4.2)
activesupport (>= 4.2.0)
i18n (1.7.0)
concurrent-ruby (~> 1.0)
jwt (2.2.1)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
ruby_dep (~> 1.2)
loofah (2.3.1)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (0.3.3)
mimemagic (~> 0.3.2)
method_source (0.9.2)
mimemagic (0.3.3)
mini_mime (1.0.2)
mini_portile2 (2.4.0)
minitest (5.13.0)
msgpack (1.3.1)
nio4r (2.5.2)
nokogiri (1.10.5)
mini_portile2 (~> 2.4.0)
pg (1.1.4)
puma (4.2.1)
nio4r (~> 2.0)
rack (2.0.7)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (6.0.1)
actioncable (= 6.0.1)
actionmailbox (= 6.0.1)
actionmailer (= 6.0.1)
actionpack (= 6.0.1)
actiontext (= 6.0.1)
actionview (= 6.0.1)
activejob (= 6.0.1)
activemodel (= 6.0.1)
activerecord (= 6.0.1)
activestorage (= 6.0.1)
activesupport (= 6.0.1)
bundler (>= 1.3.0)
railties (= 6.0.1)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.3.0)
loofah (~> 2.3)
railties (6.0.1)
actionpack (= 6.0.1)
activesupport (= 6.0.1)
method_source
rake (>= 0.8.7)
thor (>= 0.20.3, < 2.0)
rake (13.0.0)
rb-fsevent (0.10.3)
rb-inotify (0.10.0)
ffi (~> 1.0)
ruby_dep (1.5.0)
spring (2.1.0)
spring-watcher-listen (2.0.1)
listen (>= 2.7, < 4.0)
spring (>= 1.2, < 3.0)
sprockets (4.0.0)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.2.1)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
thor (0.20.3)
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
websocket-driver (0.7.1)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.4)
zeitwerk (2.2.1)

PLATFORMS
ruby

DEPENDENCIES
bcrypt (~> 3.1.7)
bootsnap (>= 1.4.2)
byebug
jwt
listen (>= 3.0.5, < 3.2)
pg (>= 0.18, < 2.0)
puma (~> 4.1)
rails (~> 6.0.1)
spring
spring-watcher-listen (~> 2.0.0)
tzinfo-data

RUBY VERSION
ruby 2.6.3p62

BUNDLED WITH
2.0.2
@@ -0,0 +1,6 @@
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require_relative 'config/application'

Rails.application.load_tasks
@@ -0,0 +1,4 @@
module ApplicationCable
class Channel < ActionCable::Channel::Base
end
end
@@ -0,0 +1,4 @@
module ApplicationCable
class Connection < ActionCable::Connection::Base
end
end
@@ -0,0 +1,28 @@
class ApplicationController < ActionController::API
# SECRET_KEY = Rails.application.secrets.secret_key_base.to_s

# def encode(payload, exp = 24.hours.from_now)
# payload[:exp] = exp.to_i
# JWT.encode(payload, SECRET_KEY)
# end

# def decode(token)
# decoded = JWT.decode(token, SECRET_KEY)[0]
# HashWithIndifferentAccess.new decoded
# end

def authorize_request
header = request.headers['Authorization']
header = header.split(' ').last if header
begin
@decoded = decode(header)
@current_user = User.find(@decoded[:user_id])
rescue ActiveRecord::RecordNotFound => e
render json: { errors: e.message }, status: :unauthorized
rescue JWT::DecodeError => e
render json: { errors: e.message }, status: :unauthorized
end
end

end

@@ -0,0 +1,26 @@
class AuthenticationController < ApplicationController
before_action :authorize_request, except: :login

# POST /auth/login
def login
@user = User.find_by_username(params[:username])
if @user.authenticate(params[:password]) #authenticate method provided by Bcrypt and 'has_secure_password'
token = encode(user_id: @user.id, username: @user.username)
render json: { user: @user, token: token }, status: :ok
else
render json: { error: 'unauthorized' }, status: :unauthorized
end
end

# GET /auth/verify
def verify
render json: @current_user, status: :ok
end


private

def login_params
params.permit(:username, :password)
end
end
@@ -0,0 +1,52 @@
class UsersController < ApplicationController
before_action :set_user, only: [:show, :update, :destroy]
before_action :authorize_request, except: :create

# GET /users
def index
@users = User.all

render json: @users
end

# GET /users/1
def show
render json: @user
end

# POST /users
def create
@user = User.new(user_params)

if @user.save
render json: @user, status: :created, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end

# PATCH/PUT /users/1
def update
if @user.update(user_params)
render json: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end

# DELETE /users/1
def destroy
@user.destroy
end

private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end

# Only allow a trusted parameter "white list" through.
def user_params
params.require(:user).permit(:username, :email, :password)
end
end
@@ -0,0 +1,7 @@
class ApplicationJob < ActiveJob::Base
# Automatically retry jobs that encountered a deadlock
# retry_on ActiveRecord::Deadlocked

# Most jobs are safe to ignore if the underlying records are no longer available
# discard_on ActiveJob::DeserializationError
end
@@ -0,0 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: 'from@example.com'
layout 'mailer'
end
@@ -0,0 +1,3 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
@@ -0,0 +1,4 @@
class Card < ApplicationRecord
belongs_to :game
belongs_to :user
end
@@ -0,0 +1,4 @@
class Game < ApplicationRecord
has_many :cards
# belongs_to :user
end
Oops, something went wrong.

0 comments on commit fd75cd2

Please sign in to comment.