rails_jwt_auth_generators 0.1.0 → 0.1.1
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/Gemfile +10 -0
- data/README.md +127 -13
- data/bin/console +11 -0
- data/bin/setup +8 -0
- data/lib/rails_jwt_auth_generators/version.rb +1 -1
- metadata +12 -5
- data/.rspec +0 -3
- data/sig/rails_jwt_auth_generators.rbs +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 44f98c84018593164941b864aba84068138f6ab389f879db2f3f89fb2add657f
|
4
|
+
data.tar.gz: 10c26c421a4d06306c587f89584b8d09a7f2417bf80b9d69c7f5d51488062472
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ea8461a2ccfd12bf17fd9f8f3d494a8b94c2f84edb392b3b4f11c05101508ede708859cff36b2b3431dc36381a98bc7e4a9ad5b4f8d118b7df81e1a4ac2b4c2
|
7
|
+
data.tar.gz: d29182a3511aab7835dc959726cd818a31ca30063d70b11a47df257f72b936020d9601b97f79ded63745657a369eebaa7874d312579ee12fca185c279992bfdc
|
data/Gemfile
ADDED
data/README.md
CHANGED
@@ -1,39 +1,153 @@
|
|
1
|
-
#
|
1
|
+
# Rails JWT Auth Generators
|
2
2
|
|
3
|
-
|
3
|
+
[](https://badge.fury.io/rb/rails_jwt_auth_generators)
|
4
|
+
[](https://github.com/Zeyad-Hassan-1/rails_jwt_auth_generators/actions)
|
4
5
|
|
5
|
-
|
6
|
+
Rails JWT Auth Generators is a Ruby gem that provides Rails generators to quickly scaffold JWT-based authentication boilerplate for Rails APIs. It includes concerns, controllers, and user models to streamline the process of setting up authentication in your Rails application.
|
7
|
+
|
8
|
+
## Features
|
9
|
+
|
10
|
+
- Generates a `JsonWebToken` concern for encoding and decoding JWTs.
|
11
|
+
- Creates an `AuthenticationController` for handling login requests.
|
12
|
+
- Scaffolds a `UsersController` for managing user resources.
|
13
|
+
- Generates a `User` model with secure password handling.
|
14
|
+
- Compatible with Rails 6.0 and above.
|
6
15
|
|
7
16
|
## Installation
|
8
17
|
|
9
|
-
|
18
|
+
Add this line to your application's Gemfile:
|
10
19
|
|
11
|
-
|
20
|
+
```ruby
|
21
|
+
gem 'rails_jwt_auth_generators'
|
22
|
+
```plaintext
|
12
23
|
|
13
|
-
|
24
|
+
And then execute:
|
14
25
|
|
15
|
-
|
26
|
+
```bash
|
27
|
+
bundle install
|
28
|
+
```plaintext
|
16
29
|
|
17
|
-
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
```bash
|
33
|
+
gem install rails_jwt_auth_generators
|
34
|
+
```
|
18
35
|
|
19
36
|
## Usage
|
20
37
|
|
21
|
-
|
38
|
+
To generate the JWT authentication boilerplate, run the following command:
|
39
|
+
|
40
|
+
```bash
|
41
|
+
rails generate auth:jwt
|
42
|
+
```
|
43
|
+
|
44
|
+
This will create the following files in your Rails application:
|
45
|
+
|
46
|
+
- `app/controllers/concerns/json_web_token.rb`
|
47
|
+
- `app/controllers/authentication_controller.rb`
|
48
|
+
- `app/controllers/users_controller.rb`
|
49
|
+
- `app/models/user.rb`
|
50
|
+
|
51
|
+
### Example Workflow
|
52
|
+
|
53
|
+
1. **Setup User Model**: Ensure your database has a `users` table with the necessary fields (`username`, `email`, `password_digest`).
|
54
|
+
|
55
|
+
2. **Authentication**: Use the `AuthenticationController` to handle login requests. It will return a JWT token upon successful authentication.
|
56
|
+
|
57
|
+
3. **Authorization**: Use the `JsonWebToken` concern to decode and verify tokens in your application.
|
58
|
+
|
59
|
+
4. **User Management**: Use the `UsersController` to manage user resources.
|
60
|
+
|
61
|
+
## Generated Files
|
62
|
+
|
63
|
+
### JsonWebToken Concern
|
64
|
+
|
65
|
+
Handles JWT encoding and decoding.
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
module JsonWebToken
|
69
|
+
SECRET_KEY = Rails.application.secret_key_base
|
70
|
+
|
71
|
+
def jwt_encode(payload, exp = 7.days.from_now)
|
72
|
+
payload[:exp] = exp.to_i
|
73
|
+
JWT.encode(payload, SECRET_KEY)
|
74
|
+
end
|
75
|
+
|
76
|
+
def jwt_decode(token)
|
77
|
+
decoded = JWT.decode(token, SECRET_KEY)[0]
|
78
|
+
HashWithIndifferentAccess.new(decoded)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
### AuthenticationController
|
84
|
+
|
85
|
+
Handles user login and token generation.
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
class AuthenticationController < ApplicationController
|
89
|
+
def login
|
90
|
+
@user = User.find_by_email(params[:email])
|
91
|
+
if @user&.authenticate(params[:password])
|
92
|
+
token = jwt_encode(user_id: @user.id)
|
93
|
+
render json: { token: token }, status: :ok
|
94
|
+
else
|
95
|
+
render json: { error: 'unauthorized' }, status: :unauthorized
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
### UsersController
|
102
|
+
|
103
|
+
Manages user resources.
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
class UsersController < ApplicationController
|
107
|
+
def create
|
108
|
+
@user = User.new(user_params)
|
109
|
+
if @user.save
|
110
|
+
render json: @user, status: :created
|
111
|
+
else
|
112
|
+
render json: { errors: @user.errors.full_messages }, status: :unprocessable_entity
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
```
|
117
|
+
|
118
|
+
### User Model
|
119
|
+
|
120
|
+
Defines the user with secure password handling.
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
class User < ApplicationRecord
|
124
|
+
has_secure_password
|
125
|
+
validates :email, :username, presence: true
|
126
|
+
end
|
127
|
+
```
|
22
128
|
|
23
129
|
## Development
|
24
130
|
|
25
131
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
26
132
|
|
27
|
-
To install this gem onto your local machine, run
|
133
|
+
To install this gem onto your local machine, run:
|
134
|
+
|
135
|
+
```bash
|
136
|
+
bundle exec rake install
|
137
|
+
```
|
28
138
|
|
29
139
|
## Contributing
|
30
140
|
|
31
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
141
|
+
Bug reports and pull requests are welcome on GitHub at [https://github.com/Zeyad-Hassan-1/rails_jwt_auth_generators](https://github.com/Zeyad-Hassan-1/rails_jwt_auth_generators). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](CODE_OF_CONDUCT.md).
|
32
142
|
|
33
143
|
## License
|
34
144
|
|
35
|
-
The gem is available as open source under the terms of the [MIT License](
|
145
|
+
The gem is available as open source under the terms of the [MIT License](LICENSE.txt).
|
36
146
|
|
37
147
|
## Code of Conduct
|
38
148
|
|
39
|
-
Everyone interacting in the RailsJwtAuthGenerators project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](
|
149
|
+
Everyone interacting in the RailsJwtAuthGenerators project's codebases, issue trackers, chat rooms, and mailing lists is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).
|
150
|
+
|
151
|
+
```
|
152
|
+
|
153
|
+
Similar code found with 2 license types
|
data/bin/console
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "rails_jwt_auth_generators"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
require "irb"
|
11
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_jwt_auth_generators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zeyad-Hassan-1
|
@@ -14,16 +14,22 @@ dependencies:
|
|
14
14
|
name: rails
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '6.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '9.0'
|
20
23
|
type: :runtime
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
|
-
- - "
|
27
|
+
- - ">="
|
25
28
|
- !ruby/object:Gem::Version
|
26
29
|
version: '6.0'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '9.0'
|
27
33
|
description: Generates JWT auth concerns, controllers, and user models for Rails APIs
|
28
34
|
email:
|
29
35
|
- studying.zezo@gmail.com
|
@@ -31,12 +37,14 @@ executables: []
|
|
31
37
|
extensions: []
|
32
38
|
extra_rdoc_files: []
|
33
39
|
files:
|
34
|
-
- ".rspec"
|
35
40
|
- CHANGELOG.md
|
36
41
|
- CODE_OF_CONDUCT.md
|
42
|
+
- Gemfile
|
37
43
|
- LICENSE.txt
|
38
44
|
- README.md
|
39
45
|
- Rakefile
|
46
|
+
- bin/console
|
47
|
+
- bin/setup
|
40
48
|
- lib/generators/auth/jwt_generator.rb
|
41
49
|
- lib/generators/auth/templates/authentication_controller.rb
|
42
50
|
- lib/generators/auth/templates/json_web_token.rb
|
@@ -44,7 +52,6 @@ files:
|
|
44
52
|
- lib/generators/auth/templates/users_controller.rb
|
45
53
|
- lib/rails_jwt_auth_generators.rb
|
46
54
|
- lib/rails_jwt_auth_generators/version.rb
|
47
|
-
- sig/rails_jwt_auth_generators.rbs
|
48
55
|
homepage: https://github.com/Zeyad-Hassan-1/rails_jwt_auth_generators
|
49
56
|
licenses:
|
50
57
|
- MIT
|
data/.rspec
DELETED