simple_jwt_auth 0.1.1 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f3410eb7555c21ab78d237f49cd7ae8d70867936b4c5d123fddc950cacabd37
4
- data.tar.gz: 981dc01478eeaad45523accb7420de4f8f563b610060e8d9d658e8652ea8e292
3
+ metadata.gz: ef5e600d8bca44e3a6951dcbb9dfe0a2497b757eb311fc52e287629eb2c4a931
4
+ data.tar.gz: 547a3a937faa8cae04af5728e670fe1e375eb2a06aa288897067f70af3cb9d06
5
5
  SHA512:
6
- metadata.gz: d38cc551e81b6df1b8b20a4fbe4d9c2f143e85cbd552fe16778a7b7b875bf23ed1bb80b6a5e0da5e952e4dd47dcfae618e9fe92830a047fc5dbf38fd126ae288
7
- data.tar.gz: 54c254551ec766af5892b5e090f359e8c44189c5d219beef6ac23fed19ed48c996f8556992efbf2346c2dbc64cf2a57bd7641c7e5427ece9555cac83210feee8
6
+ metadata.gz: a204552f9c1742a47c0277d2f9ecbe00fba8289842da4895a20f695ae1c4d24de0d00f9e242c7ab124d7c03d292afcd5a21d651a2be543d79290c8b895eeef8f
7
+ data.tar.gz: 6be0c2c6eef0095eb6ce49aaf9c7cd685b1be0b69c5a59e5e72d27f920084eeadd4355551c804e06b686cf2cc41a88290034f876cacf09e20f5b78dd43cbc12e
data/README.md CHANGED
@@ -22,20 +22,15 @@ $ gem install simple_jwt_auth
22
22
  ### Models
23
23
  After installation run
24
24
  ```bash
25
- $ rails simple_jwt_auth:install:migrations
25
+ $ rails simple_jwt_auth:setup
26
26
  ```
27
- this will create a user model in your rails application.
27
+ this will create a user model in your rails application. As well as creating a file for adding associations and user model methods at app/models/user.rb
28
28
 
29
29
  then run
30
30
  ```bash
31
31
  $ rails db:migrate
32
32
  ```
33
- ### Routes
34
- in order to access gems provided routes add
35
- ```bash
36
- mount SimpleJwtAuth::Engine, at: "/auth"
37
- ```
38
- to the top of your routes in your rails project
33
+
39
34
  ### Creating Users
40
35
  Users can be created by making a POST request to
41
36
  https://localhost:3000/auth/users
@@ -47,7 +42,8 @@ https://localhost:3000/auth/sessions
47
42
  with email and password as params.
48
43
 
49
44
  #### Logged in
50
- Add header called "Authorization" and set it to "Bearer <your jwt token here>" include this in any request you want the user to be logged in for.
51
-
45
+ Add header called "Authorization" and set it to "Bearer #your-jwt-token-here" include this in any request you want the user to be logged in for.
46
+ A GET request can then be made to https://localhost:3000/auth/user to return the current user object.
47
+
52
48
  ## License
53
49
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -1 +0,0 @@
1
- //= link_directory ../stylesheets/simple_jwt_auth .css
@@ -0,0 +1,26 @@
1
+ require_dependency "jwt"
2
+
3
+ class SessionsController < ApplicationController
4
+ def create
5
+ user = User.find_by(email: session_params[:email])
6
+ if user && user.authenticate(session_params[:password])
7
+ jwt = JWT.encode(
8
+ {
9
+ user_id: user.id, # the data to encode
10
+ exp: 24.hours.from_now.to_i # the expiration time
11
+ },
12
+ Rails.application.credentials.fetch(:secret_key_base), # the secret key
13
+ "HS256" # the encryption algorithm
14
+ )
15
+ render json: { jwt: jwt, email: user.email, user_id: user.id, isPasswordReset: user.isPasswordReset }, status: :created
16
+ else
17
+ render json: {}, status: :unauthorized
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def session_params
24
+ params.permit(:email, :password)
25
+ end
26
+ end
@@ -0,0 +1,45 @@
1
+ class UsersController < ApplicationController
2
+ def show
3
+ if current_user
4
+ render json: { email: current_user.email, user_id: current_user.id }
5
+ else
6
+ render json: {}, status: :unauthorized
7
+ end
8
+ end
9
+
10
+ def create
11
+ user = User.new(user_params)
12
+
13
+ if user.save
14
+ render json: {message: "User created successfully"}, status: :created
15
+ else
16
+ render json: {errors: user.errors.full_messages}, status: :bad_request
17
+ end
18
+ end
19
+
20
+ def update
21
+ if current_user
22
+ user = User.find_by_id(current_user.id)
23
+ user.isPasswordReset = true
24
+ user.update(update_params)
25
+
26
+ if user.save
27
+ render json: {message: "User updated successfully"}, status: :ok
28
+ else
29
+ render json: {errors: user.errors.full_messages}, status: :bad_request
30
+ end
31
+ else
32
+ render json: {}, status: :unauthorized
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def user_params
39
+ params.permit(:name, :email, :password, :password_confirmation)
40
+ end
41
+
42
+ def update_params
43
+ params.permit(:password, :password_confirmation)
44
+ end
45
+ end
@@ -0,0 +1,2 @@
1
+ module SessionsHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module UsersHelper
2
+ end
@@ -0,0 +1,4 @@
1
+ class User < ApplicationRecord
2
+ has_secure_password
3
+ validates :email, presence: true, uniqueness: true
4
+ end
data/config/routes.rb CHANGED
@@ -1,4 +1,7 @@
1
- SimpleJwtAuth::Engine.routes.draw do
2
- resources :users, only: [:create]
3
- resources :sessions, only: [:create]
1
+ Rails.application.routes.draw do
2
+ scope :auth do
3
+ resources :users, only: [:create]
4
+ resource :user, only: [:show, :update]
5
+ resources :sessions, only: [:create]
6
+ end
4
7
  end
@@ -0,0 +1,12 @@
1
+ class CreateUsers < ActiveRecord::Migration[6.0]
2
+ def change
3
+ create_table :users do |t|
4
+ t.string :name
5
+ t.string :email
6
+ t.string :password_digest
7
+ t.boolean :isPasswordReset, default: false
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -1,7 +1,6 @@
1
1
  module SimpleJwtAuth
2
2
  class Engine < ::Rails::Engine
3
- isolate_namespace SimpleJwtAuth
4
- initializer "actualize_auth.load_helpers" do |app|
3
+ initializer "simple_jwt_auth.load_helpers" do |app|
5
4
  ActionController::Base.send :include, SimpleJwtAuth::Engine.helpers
6
5
  end
7
6
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleJwtAuth
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.5'
3
3
  end
@@ -1,4 +1,17 @@
1
- # desc "Explaining what the task does"
2
- # task :simple_jwt_auth do
3
- # # Task goes here
4
- # end
1
+ require_dependency "tty-file"
2
+
3
+ desc "Copy migrations from simple_jwt_auth to application and create model/user.rb file in application"
4
+ namespace :simple_jwt_auth do
5
+ task :setup do
6
+ Rake::Task["simple_jwt_auth_engine:install:migrations"].invoke
7
+ TTY::File.create_file "app/models/user.rb" do
8
+ <<~CONFIG_MODELS
9
+ require File.expand_path('../../app/models/user', SimpleJwtAuth::Engine.called_from)
10
+
11
+ class User
12
+ end
13
+ CONFIG_MODELS
14
+ end
15
+ end
16
+ end
17
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_jwt_auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - brye
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-22 00:00:00.000000000 Z
11
+ date: 2021-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: tty-file
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: pg
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -77,22 +91,16 @@ files:
77
91
  - README.md
78
92
  - Rakefile
79
93
  - app/assets/config/simple_jwt_auth_manifest.js
80
- - app/assets/stylesheets/simple_jwt_auth/application.css
81
- - app/assets/stylesheets/simple_jwt_auth/sessions.css
82
- - app/assets/stylesheets/simple_jwt_auth/users.css
83
- - app/controllers/simple_jwt_auth/application_controller.rb
84
- - app/controllers/simple_jwt_auth/sessions_controller.rb
85
- - app/controllers/simple_jwt_auth/users_controller.rb
94
+ - app/assets/stylesheets/sessions.css
95
+ - app/assets/stylesheets/users.css
96
+ - app/controllers/sessions_controller.rb
97
+ - app/controllers/users_controller.rb
98
+ - app/helpers/sessions_helper.rb
86
99
  - app/helpers/simple_jwt_auth/application_helper.rb
87
- - app/helpers/simple_jwt_auth/sessions_helper.rb
88
- - app/helpers/simple_jwt_auth/users_helper.rb
89
- - app/jobs/simple_jwt_auth/application_job.rb
90
- - app/mailers/simple_jwt_auth/application_mailer.rb
91
- - app/models/simple_jwt_auth/application_record.rb
92
- - app/models/simple_jwt_auth/user.rb
93
- - app/views/layouts/simple_jwt_auth/application.html.erb
100
+ - app/helpers/users_helper.rb
101
+ - app/models/user.rb
94
102
  - config/routes.rb
95
- - db/migrate/20191222061616_create_simple_jwt_auth_users.rb
103
+ - db/migrate/20191225083243_create_users.rb
96
104
  - lib/simple_jwt_auth.rb
97
105
  - lib/simple_jwt_auth/engine.rb
98
106
  - lib/simple_jwt_auth/version.rb
@@ -101,7 +109,7 @@ homepage: https://github.com/bryewalks/simple_auth
101
109
  licenses:
102
110
  - MIT
103
111
  metadata: {}
104
- post_install_message:
112
+ post_install_message:
105
113
  rdoc_options: []
106
114
  require_paths:
107
115
  - lib
@@ -117,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
125
  version: '0'
118
126
  requirements: []
119
127
  rubygems_version: 3.0.6
120
- signing_key:
128
+ signing_key:
121
129
  specification_version: 4
122
130
  summary: JWT based authorization
123
131
  test_files: []
@@ -1,15 +0,0 @@
1
- /*
2
- * This is a manifest file that'll be compiled into application.css, which will include all the files
3
- * listed below.
4
- *
5
- * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
- * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
- *
8
- * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
- * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
- * files in this directory. Styles in this file should be added after the last require_* statement.
11
- * It is generally better to create a new file per style scope.
12
- *
13
- *= require_tree .
14
- *= require_self
15
- */
@@ -1,5 +0,0 @@
1
- module SimpleJwtAuth
2
- class ApplicationController < ActionController::Base
3
- protect_from_forgery with: :null_session
4
- end
5
- end
@@ -1,29 +0,0 @@
1
- require_dependency "simple_jwt_auth/application_controller"
2
- require_dependency "jwt"
3
-
4
- module SimpleJwtAuth
5
- class SessionsController < ApplicationController
6
- def create
7
- user = User.find_by(email: session_params[:email])
8
- if user && user.authenticate(session_params[:password])
9
- jwt = JWT.encode(
10
- {
11
- user_id: user.id, # the data to encode
12
- exp: 24.hours.from_now.to_i # the expiration time
13
- },
14
- Rails.application.credentials.fetch(:secret_key_base), # the secret key
15
- "HS256" # the encryption algorithm
16
- )
17
- render json: { jwt: jwt, email: user.email, user_id: user.id }, status: :created
18
- else
19
- render json: {}, status: :unauthorized
20
- end
21
- end
22
-
23
- private
24
-
25
- def session_params
26
- params.permit(:email, :password)
27
- end
28
- end
29
- end
@@ -1,22 +0,0 @@
1
- require_dependency "simple_jwt_auth/application_controller"
2
-
3
- module SimpleJwtAuth
4
- class UsersController < ApplicationController
5
-
6
- def create
7
- user = User.new(user_params)
8
-
9
- if user.save
10
- render json: {message: "User created successfully"}, status: :created
11
- else
12
- render json: {errors: user.errors.full_messages}, status: :bad_request
13
- end
14
- end
15
-
16
- private
17
-
18
- def user_params
19
- params.permit(:name, :email, :password, :password_confirmation)
20
- end
21
- end
22
- end
@@ -1,4 +0,0 @@
1
- module SimpleJwtAuth
2
- module SessionsHelper
3
- end
4
- end
@@ -1,4 +0,0 @@
1
- module SimpleJwtAuth
2
- module UsersHelper
3
- end
4
- end
@@ -1,4 +0,0 @@
1
- module SimpleJwtAuth
2
- class ApplicationJob < ActiveJob::Base
3
- end
4
- end
@@ -1,6 +0,0 @@
1
- module SimpleJwtAuth
2
- class ApplicationMailer < ActionMailer::Base
3
- default from: 'from@example.com'
4
- layout 'mailer'
5
- end
6
- end
@@ -1,5 +0,0 @@
1
- module SimpleJwtAuth
2
- class ApplicationRecord < ActiveRecord::Base
3
- self.abstract_class = true
4
- end
5
- end
@@ -1,6 +0,0 @@
1
- module SimpleJwtAuth
2
- class User < ApplicationRecord
3
- has_secure_password
4
- validates :email, presence: true, uniqueness: true
5
- end
6
- end
@@ -1,15 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <title>Simple jwt auth</title>
5
- <%= csrf_meta_tags %>
6
- <%= csp_meta_tag %>
7
-
8
- <%= stylesheet_link_tag "simple_jwt_auth/application", media: "all" %>
9
- </head>
10
- <body>
11
-
12
- <%= yield %>
13
-
14
- </body>
15
- </html>
@@ -1,11 +0,0 @@
1
- class CreateSimpleJwtAuthUsers < ActiveRecord::Migration[6.0]
2
- def change
3
- create_table :simple_jwt_auth_users do |t|
4
- t.string :name
5
- t.string :email
6
- t.string :password_digest
7
-
8
- t.timestamps
9
- end
10
- end
11
- end