simple_jwt_auth 0.1.1 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +6 -10
- data/app/assets/config/simple_jwt_auth_manifest.js +0 -1
- data/app/assets/stylesheets/{simple_jwt_auth/sessions.css → sessions.css} +0 -0
- data/app/assets/stylesheets/{simple_jwt_auth/users.css → users.css} +0 -0
- data/app/controllers/sessions_controller.rb +26 -0
- data/app/controllers/users_controller.rb +45 -0
- data/app/helpers/sessions_helper.rb +2 -0
- data/app/helpers/users_helper.rb +2 -0
- data/app/models/user.rb +4 -0
- data/config/routes.rb +6 -3
- data/db/migrate/20191225083243_create_users.rb +12 -0
- data/lib/simple_jwt_auth/engine.rb +1 -2
- data/lib/simple_jwt_auth/version.rb +1 -1
- data/lib/tasks/simple_jwt_auth_tasks.rake +17 -4
- metadata +27 -19
- data/app/assets/stylesheets/simple_jwt_auth/application.css +0 -15
- data/app/controllers/simple_jwt_auth/application_controller.rb +0 -5
- data/app/controllers/simple_jwt_auth/sessions_controller.rb +0 -29
- data/app/controllers/simple_jwt_auth/users_controller.rb +0 -22
- data/app/helpers/simple_jwt_auth/sessions_helper.rb +0 -4
- data/app/helpers/simple_jwt_auth/users_helper.rb +0 -4
- data/app/jobs/simple_jwt_auth/application_job.rb +0 -4
- data/app/mailers/simple_jwt_auth/application_mailer.rb +0 -6
- data/app/models/simple_jwt_auth/application_record.rb +0 -5
- data/app/models/simple_jwt_auth/user.rb +0 -6
- data/app/views/layouts/simple_jwt_auth/application.html.erb +0 -15
- data/db/migrate/20191222061616_create_simple_jwt_auth_users.rb +0 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ef5e600d8bca44e3a6951dcbb9dfe0a2497b757eb311fc52e287629eb2c4a931
|
4
|
+
data.tar.gz: 547a3a937faa8cae04af5728e670fe1e375eb2a06aa288897067f70af3cb9d06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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:
|
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
|
-
|
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
|
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
|
File without changes
|
File without changes
|
@@ -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
|
data/app/models/user.rb
ADDED
data/config/routes.rb
CHANGED
@@ -1,4 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
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
|
@@ -1,7 +1,6 @@
|
|
1
1
|
module SimpleJwtAuth
|
2
2
|
class Engine < ::Rails::Engine
|
3
|
-
|
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,4 +1,17 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
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.
|
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:
|
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/
|
81
|
-
- app/assets/stylesheets/
|
82
|
-
- app/
|
83
|
-
- app/controllers/
|
84
|
-
- app/
|
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/
|
88
|
-
- app/
|
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/
|
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,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,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>
|