api_authentication_gem 0.1.3 → 0.1.4

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: 5ef82ce77c6130e40ba62ffa3eb29c4c0536de438168d59052f3e4ac0abcba90
4
- data.tar.gz: a4d81cc6a0b0ad3b21130868b6b3cdebde7026680bb7403e67fa5fe873c432b0
3
+ metadata.gz: ec9f993dd7036eee23f50421a5ef5e197370ba2349abcacf95bcf0eb81b8370c
4
+ data.tar.gz: 52e9145805cd449d3e4ee0959a48b217e3bea0da412587fabe4c0e36db9ed89c
5
5
  SHA512:
6
- metadata.gz: 8f4b52e667b8f7de5bea164d3bf201bfe4b69a098d0cbe40dcd82c0b971e952ffba32e91e89306d85c984c9cd11563138f5d5ea0366d1b42d3ac912b2638d4db
7
- data.tar.gz: 8f4a182fb392ad8d2fe2289f96cbe2d25620a566295ad1a82aa857cec9702a1cf1a309bd8886bb5fdfd415c739025ff680e547bb13d049c75d5283b911bb0bda
6
+ metadata.gz: 452a6501dc344085c0e30e9550fe046b115b7c69c87a70fa49e74e3711211428ada98d69a119e3a0c913eb4c73a7ae82d19d9132b85dce8fab921d4f856b1c5d
7
+ data.tar.gz: 956f9e09e241360429e214e1b96e79ea2c0f9f775c0889df36a33c44d876c239ef5e26f60b68d9b04a54d683399c4fcadd8ed38bbf18bab10deb52f85b757016
data/README.md CHANGED
@@ -12,59 +12,84 @@ No need to write boilerplate authentication code again — just plug it in, and
12
12
  - ✅ Login with JWT token generation
13
13
  - ✅ Easy integration with any Rails API
14
14
  - ✅ Customizable user model support
15
+ - ✅ Configurable `secret_key` and `user_class`
16
+ - ✅ Tested with RSpec
17
+ - ✅ Rails-friendly design
15
18
 
16
19
  ---
17
20
 
18
21
  ## 📦 Installation
19
22
 
20
23
  Add this line to your Rails application's `Gemfile`:
21
- gem 'api_authentication_gem'
24
+ `gem 'api_authentication_gem'`
22
25
  Then run:
23
- bundle install
26
+ `bundle install`
24
27
 
25
28
  🛠 Setup in Your Rails API
26
- 1. Generate the User Model (if not already created)
27
- rails generate model User email:string password_digest:string
28
- rails db:migrate
29
-
30
- 2. Create a Controller to Handle Auth Actions
31
-
32
- class UsersController < ApplicationController
33
- def signup
34
- result = ApiAuthenticationGem::Auth.signup(
35
- email: params[:email],
36
- password: params[:password],
37
- user_class: User
38
- )
39
-
40
- if result[:error]
41
- render json: { error: result[:error] }, status: :unprocessable_entity
42
- else
43
- render json: { message: result[:message], user: result[:user] }, status: :created
29
+ 1. Add gem configs to your app:
30
+ ```ruby
31
+ # config/initializers/api_authentication_gem.rb
32
+
33
+ ApiAuthenticationGem.configure do |config|
34
+ config.secret_key = ENV["SECRET_KEY_BASE"] || "fallback-key" # Always set your secret_key via environment variable in production
35
+ config.user_class = "User"
44
36
  end
37
+ ```
38
+
39
+ 2. Generate the User Model (if not already created)
40
+ ```bash
41
+ rails generate model User email:string password_digest:string
42
+ rails db:migrate
43
+ ```
44
+
45
+ Your User model must use has_secure_password:
46
+ ```ruby
47
+ class User < ApplicationRecord
48
+ has_secure_password
45
49
  end
50
+ ```
51
+ No need to manually validate emails or handle password_digest — the gem takes care of it internally.
52
+
53
+ 3. Create a Controller to Handle Auth Actions
54
+
55
+ ```ruby
56
+ class UsersController < ApplicationController
57
+ def signup
58
+ result = ApiAuthenticationGem::Auth.signup(
59
+ email: params[:email],
60
+ password: params[:password]
61
+ )
62
+
63
+ if result[:error]
64
+ render json: { error: result[:error] }, status: :unprocessable_entity
65
+ else
66
+ render json: { message: result[:message], user: result[:user] }, status: :created
67
+ end
68
+ end
46
69
 
47
- def login
48
- result = ApiAuthenticationGem::Auth.login(
49
- email: params[:email],
50
- password: params[:password],
51
- user_class: User
52
- )
53
-
54
- if result[:error]
55
- render json: { error: result[:error] }, status: :unauthorized
56
- else
57
- render json: { token: result[:token] }, status: :ok
70
+ def login
71
+ result = ApiAuthenticationGem::Auth.login(
72
+ email: params[:email],
73
+ password: params[:password]
74
+ )
75
+
76
+ if result[:error]
77
+ render json: { error: result[:error] }, status: :unauthorized
78
+ else
79
+ render json: { token: result[:token] }, status: :ok
80
+ end
58
81
  end
59
82
  end
60
- end
83
+ ```
61
84
 
62
85
 
63
86
  3. Define Routes
64
87
  In your config/routes.rb file:
65
- Rails.application.routes.draw do
66
- post 'signup', to: 'users#signup'
67
- post 'login', to: 'users#login'
68
- end
88
+ ```ruby
89
+ Rails.application.routes.draw do
90
+ post 'signup', to: 'users#signup'
91
+ post 'login', to: 'users#login'
92
+ end
93
+ ```
69
94
 
70
95
 
@@ -0,0 +1,36 @@
1
+ require "jwt"
2
+ module ApiAuthenticationGem
3
+ class Auth
4
+ def self.signup(email:, password:)
5
+ user_class = ApiAuthenticationGem.configuration.user_class.constantize
6
+ user = user_class.create!(email: email, password: password)
7
+ token = generate_token(user.id)
8
+ { token: token, user: user }
9
+ end
10
+
11
+ def self.login(email:, password:)
12
+ user_class = ApiAuthenticationGem.configuration.user_class.constantize
13
+ user = user_class.find_by(email: email)
14
+ return nil unless user&.authenticate(password)
15
+
16
+ token = generate_token(user.id)
17
+ { token: token, user: user }
18
+ end
19
+
20
+
21
+ # Decode the JWT token and retrieve the payload
22
+ def self.decode(token)
23
+ decoded = JWT.decode(token, ApiAuthenticationGem.configuration.secret_key, true, algorithm: 'HS256')
24
+ decoded.first # The payload is in the first element
25
+ rescue JWT::DecodeError => e
26
+ nil # If the token is invalid, return nil
27
+ end
28
+
29
+ private
30
+
31
+ def self.generate_token(user_id)
32
+ payload = { user_id: user_id }
33
+ JWT.encode(payload, ApiAuthenticationGem.configuration.secret_key)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,11 @@
1
+ module ApiAuthenticationGem
2
+ class Configuration
3
+ attr_accessor :secret_key, :user_class
4
+
5
+ def initialize
6
+ @secret_key = ENV["API_AUTH_SECRET_KEY"] || "your-default-secret-key"
7
+ @user_class = "User" # Default to 'User' model
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,3 @@
1
+ module ApiAuthenticationGem
2
+ VERSION = "0.1.4"
3
+ end
@@ -1,36 +1,13 @@
1
- require "jwt"
2
- require "bcrypt"
1
+ require "api_authentication_gem/version"
2
+ require "api_authentication_gem/configuration"
3
+ require "api_authentication_gem/auth"
3
4
 
4
5
  module ApiAuthenticationGem
5
- class Auth
6
- SECRET_KEY = "your$ecretK3y"
7
-
8
- def self.signup(email:, password:, user_class:)
9
- return { error: "Email is required" } unless email
10
- return { error: "Password must be at least 5 characters long" } if password.nil? || password.length < 5
11
-
12
- return { error: "Email already exists" } if user_class.find_by(email: email)
13
-
14
- password_digest = BCrypt::Password.create(password)
15
- user = user_class.create(email: email, password_digest: password_digest)
16
-
17
- { message: "User created", user: user }
18
- end
19
-
20
- def self.login(email:, password:, user_class:)
21
- user = user_class.find_by(email: email)
22
-
23
- return { error: "Invalid email or password" } unless user
24
- return { error: "Invalid email or password" } unless BCrypt::Password.new(user.password_digest) == password
25
-
26
- token = JWT.encode({ user_id: user.id, exp: Time.now.to_i + 3600 }, SECRET_KEY, 'HS256')
27
- { token: token }
28
- end
6
+ def self.configuration
7
+ @configuration ||= Configuration.new
8
+ end
29
9
 
30
- def self.decode_token(token)
31
- JWT.decode(token, SECRET_KEY, true, { algorithm: 'HS256' })[0]
32
- rescue
33
- nil
34
- end
10
+ def self.configure
11
+ yield(configuration)
35
12
  end
36
13
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: api_authentication_gem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adarsh Mishra
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2025-04-15 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: bcrypt
@@ -38,6 +37,34 @@ dependencies:
38
37
  - - ">="
39
38
  - !ruby/object:Gem::Version
40
39
  version: '0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rspec
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: rspec-rails
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
41
68
  description: 'Reusable gem for authentication with user-defined models. For detailed
42
69
  usage, visit: https://github.com/Adarsh-07-Mishra/api_authentication_gem/blob/main/README.md'
43
70
  email:
@@ -48,11 +75,13 @@ extra_rdoc_files: []
48
75
  files:
49
76
  - README.md
50
77
  - lib/api_authentication_gem.rb
78
+ - lib/api_authentication_gem/auth.rb
79
+ - lib/api_authentication_gem/configuration.rb
80
+ - lib/api_authentication_gem/version.rb
51
81
  homepage: https://github.com/Adarsh-07-Mishra/api_authentication_gem
52
82
  licenses:
53
83
  - MIT
54
84
  metadata: {}
55
- post_install_message:
56
85
  rdoc_options: []
57
86
  require_paths:
58
87
  - lib
@@ -67,8 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
96
  - !ruby/object:Gem::Version
68
97
  version: '0'
69
98
  requirements: []
70
- rubygems_version: 3.3.3
71
- signing_key:
99
+ rubygems_version: 3.6.8
72
100
  specification_version: 4
73
101
  summary: Provides JWT-based signup and login for APIs
74
102
  test_files: []