rails_jwt_auth_generators 0.1.0 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a147f93e0c05451471cd4b443affcf90e6b9ee93d030432d44a00efcecdde795
4
- data.tar.gz: aa55a3f14074d34a9e40ed903b13cab4fd960f017faaa7f9a5970fff581da59d
3
+ metadata.gz: cdbd322796f82f5d7b736de32c66dd37461f08cf0e5c6a8cee3ea0e3f82caa80
4
+ data.tar.gz: 2c09274c84b92ee6af9b5c9ce2c71e196845b8998e09a7faad084e5d9b514cf8
5
5
  SHA512:
6
- metadata.gz: ddc5d1b264f270a460c3f7520276bbf61a2d5a6bfc83e46a966e04c486fa3cbf3136558f1592dabc68979a0b6638bec7756ef915c80d554de6eda976546be919
7
- data.tar.gz: 56a823c1e5fd07bb613de731da8a451d0aef83975503d9ed7ba9633495ddb88d0e7dbd24bc4ea04fe102a3f974dc82937a8fa207d3735f74a5d97e5a14ac927e
6
+ metadata.gz: ec3c48c1639510b7f39942291ca705f8ef72cedd3c03431661c24d67567e9405a6adfdf76dd3655624a97773fe3f892e9e34089154fe0877d663c9a94ffeaa5f
7
+ data.tar.gz: 3bd098494a916585db65f2e65b012f93ef03a07f491025007960bcefe56ea8b74899c38c7477efd6c60735beac1faa0ea988aa8e88b9ca7aed4bb1c79a8c5630
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in rails_jwt_auth_generators.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/README.md CHANGED
@@ -1,39 +1,153 @@
1
- # RailsJwtAuthGenerators
1
+ # Rails JWT Auth Generators
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ [![Gem Version](https://badge.fury.io/rb/rails_jwt_auth_generators.svg)](https://badge.fury.io/rb/rails_jwt_auth_generators)
4
+ [![Build Status](https://github.com/Zeyad-Hassan-1/rails_jwt_auth_generators/actions/workflows/main.yml/badge.svg)](https://github.com/Zeyad-Hassan-1/rails_jwt_auth_generators/actions)
4
5
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rails_jwt_auth_generators`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
18
+ Add this line to your application's Gemfile:
10
19
 
11
- Install the gem and add to the application's Gemfile by executing:
20
+ ```ruby
21
+ gem 'rails_jwt_auth_generators'
22
+ ```plaintext
12
23
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
24
+ And then execute:
14
25
 
15
- If bundler is not being used to manage dependencies, install the gem by executing:
26
+ ```bash
27
+ bundle install
28
+ ```plaintext
16
29
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
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
- TODO: Write usage instructions here
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 `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
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/[USERNAME]/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](https://github.com/[USERNAME]/rails_jwt_auth_generators/blob/master/CODE_OF_CONDUCT.md).
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](https://opensource.org/licenses/MIT).
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](https://github.com/[USERNAME]/rails_jwt_auth_generators/blob/master/CODE_OF_CONDUCT.md).
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -1,20 +1,13 @@
1
1
  module Auth
2
2
  class JwtGenerator < Rails::Generators::NamedBase
3
+ desc "This generator creates JWT auth files (no arguments needed)"
3
4
  source_root File.expand_path('templates', __dir__)
4
5
 
5
- desc "This generator creates a JWT auth concern and authentication controller"
6
-
6
+ # Remove the argument requirement
7
7
  def create_auth_files
8
- # 1. Generate JWT Concern
9
8
  template "json_web_token.rb", "app/controllers/concerns/json_web_token.rb"
10
-
11
- # 2. Generate Authentication Controller
12
9
  template "authentication_controller.rb", "app/controllers/authentication_controller.rb"
13
-
14
- # 3. Generate UsersController
15
10
  template "users_controller.rb", "app/controllers/users_controller.rb"
16
-
17
- # 4. Generate User Model (optional, but recommended)
18
11
  template "user.rb", "app/models/user.rb"
19
12
  end
20
13
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailsJwtAuthGenerators
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
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.0
4
+ version: 0.1.2
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
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
@@ -1,4 +0,0 @@
1
- module RailsJwtAuthGenerators
2
- VERSION: String
3
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
- end