revise_auth-jets 0.3.1 → 0.3.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: ad5830138b573376c725bdd2f4cf927451b5ba9f561b45cd88b5235e438459d6
4
- data.tar.gz: 6458c724a0afcd5164c6cb1a3d822fdcfc7da9f3d22234057a24b2e6e4d7d90a
3
+ metadata.gz: 825c70cb5515ef92564d9ba805da0c6629b8822c3b88b3760f4cee3b44e18c6c
4
+ data.tar.gz: f3befb88b7f16a042ed269bcc3e2bf12a50c58a512a0215f992587bbb95d010c
5
5
  SHA512:
6
- metadata.gz: a1b995b99756a0626ac174ccbba43aa4444f2136c87f91482192799e9f53baf00702e9ce73534af5302050eb75a4fb2024dcab1ce94de030f1fe978ebe94fc78
7
- data.tar.gz: 4d4503bbdd0025ae4a5a966bb82ea0740a251190d1e5b1514f287ae0704b9edc1ed683100a118e81eeed7d264782090572a69aa1bb76ebc9b4e6b0e1837c7b7f
6
+ metadata.gz: 3edc1eff821369e7225b82e883d43e24373ae528d29682d01fdd9a14ad3c9549cbed623ec535d3448ab499986ff581a86a15811aeaf11965f9629ce7fbd55750
7
+ data.tar.gz: 70fedcacdf4b1d572810bf1120c8f915f10014ff0c89423a237e23299d542f42610a6d3db4ac20bfb1daac2d17fbd2fab37d33c3dfe4ae3ac69336811d2d17b1
data/README.md CHANGED
@@ -12,7 +12,7 @@ bundle add "revise_auth-jets"
12
12
 
13
13
  And then execute the following to generate a `User` model (optionally adding other fields such as `first_name` and `last_name`):
14
14
  ```bash
15
- $ jets g revise_auth:model User first_name last_name
15
+ $ jets g revise_auth:model User
16
16
  $ jets db:migrate
17
17
  $ jets g revise_auth:views
18
18
  ```
data/app/config/routes.rb CHANGED
@@ -23,6 +23,13 @@ Jets.application.routes.draw do
23
23
  delete "logout", to: "sessions#delete"
24
24
  end
25
25
 
26
+ # API routes
27
+ namespace :api do
28
+ namespace :v1 do
29
+ resource :me, only: :show
30
+ end
31
+ end
32
+
26
33
  # The jets/public#show controller can serve static utf8 content out of the public folder.
27
34
  # Note, as part of the deploy process Jets uploads files in the public folder to s3
28
35
  # and serves them out of s3 directly. S3 is well suited to serve static assets.
@@ -0,0 +1,42 @@
1
+ class Api::BaseController < ApplicationController
2
+ include ReviseAuth::Authentication
3
+ skip_before_action :verify_authenticity_token
4
+ prepend_before_action :authenticate_api_token!
5
+ rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
6
+ rescue_from ActionController::ParameterMissing, with: :handle_parameter_missing
7
+
8
+ private
9
+
10
+ def record_not_found
11
+ render json: {error: "Record Not Found"}, status: :not_found
12
+ end
13
+
14
+ def handle_parameter_missing(exception)
15
+ render json: {error: exception.message}, status: :bad_request
16
+ end
17
+
18
+ def authenticate_api_token!
19
+ if user_from_token
20
+ login(user_from_token)
21
+ else
22
+ head :unauthorized
23
+ end
24
+ end
25
+
26
+ def token_from_header
27
+ request.headers.fetch("authorization", "").split(" ").last
28
+ end
29
+
30
+ def api_token
31
+ @_api_token ||= ApiToken.find_by(token: token_from_header)
32
+ end
33
+
34
+ # Only for use within authenticate_api_token! above
35
+ # Use current_user/Current.user or current_account/Current.account within app controllers
36
+ def user_from_token
37
+ if api_token.present?
38
+ api_token.touch(:last_used_at)
39
+ api_token.user
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,11 @@
1
+ # Just a demo api controller you can remove this
2
+ class Api::V1::MesController < Api::BaseController
3
+ def show
4
+ render json: current_user
5
+ end
6
+
7
+ def destroy
8
+ current_user.destroy
9
+ render json: {}
10
+ end
11
+ end
@@ -9,6 +9,7 @@ class ReviseAuth::RegistrationsController < ReviseAuthController
9
9
  @user = User.new(sign_up_params)
10
10
  if @user.save
11
11
  login(@user)
12
+ current_user.api_tokens.first_or_create(name: ApiToken::APP_NAME)
12
13
  redirect_to root_path
13
14
  else
14
15
  render :new, status: :unprocessable_entity
@@ -5,6 +5,7 @@ class ReviseAuth::SessionsController < ReviseAuthController
5
5
  def create
6
6
  if (user = User.authenticate_by(email: params[:email], password: params[:password]))
7
7
  login(user)
8
+ current_user.api_tokens.first_or_create(name: ApiToken::APP_NAME)
8
9
  redirect_to root_path
9
10
  else
10
11
  #flash[:alert] = I18n.t("revise_auth.invalid_email_or_password")
@@ -11,20 +11,26 @@ module ReviseAuth
11
11
  argument :attributes, type: :array, default: [], banner: "field:type field:type"
12
12
 
13
13
  def initialize(args, *options)
14
- @original_attributes = args[1..] || []
15
14
  super
16
15
  end
17
16
 
18
17
  def generate_model
19
- model_attributess = model_attributes.join(', ').gsub(':index', '').gsub(',', '')
18
+ model_attributess = model_attributes.join(', ').gsub(',', '')
19
+ puts "Adding #{name}"
20
20
  puts "jets g model #{name} #{model_attributess}"
21
21
  system "jets g model #{name} #{model_attributess}"
22
+ puts "Adding ApiToken"
23
+ system "jets g model ApiTokens #{name.downcase}:references token:string:uniq name:string metadata:jsonb transient:boolean last_used_at:datetime expires_at:datetime"
22
24
  #generate :model, name, *model_attributes
23
25
  end
24
26
 
25
27
  def add_revise_auth_model
28
+ prepend_to_file "app/models/api_token.rb", "require 'revise_auth-jets'\n"
29
+ inject_into_class "app/models/api_token.rb", "ApiToken", " include ReviseAuth::ApiModel\n"
30
+
26
31
  prepend_to_file model_path, "require 'revise_auth-jets'\n"
27
32
  inject_into_class model_path, class_name, " include ReviseAuth::Model\n"
33
+ inject_into_class model_path, class_name, " has_many :api_tokens, dependent: :destroy\n"
28
34
  end
29
35
 
30
36
  def add_uniq_to_email_index
@@ -51,13 +57,16 @@ module ReviseAuth
51
57
 
52
58
  def model_attributes
53
59
  [
54
- "email:string:index",
60
+ "email:string:uniq",
55
61
  "password_digest:string",
62
+ "first_name:string",
63
+ "last_name:string",
64
+ "admin:boolean",
56
65
  "confirmation_token:string",
57
66
  "confirmed_at:datetime",
58
67
  "confirmation_sent_at:datetime",
59
68
  "unconfirmed_email:string"
60
- ] + @original_attributes
69
+ ]
61
70
  end
62
71
  end
63
72
  end
@@ -1,8 +1,7 @@
1
1
  🚚 Your Revise auth database model has been generated!
2
2
 
3
3
  Next step:
4
- Add "add_index :users, :email, unique: true" at the bottom of the change method
4
+ Add t.jsonb :metadata, default: {} and t.boolean :transient, default: false into your ApiToken migration
5
5
  Run "jets db:migrate"
6
- Add ActiveRecord::Base.signed_id_verifier_secret = "custom_verfifier_secret" in your initializers/ Set this as an env var
7
6
  Add your stmp settings in your development.rb
8
7
  Run "jets g revise_auth:views"
@@ -12,7 +12,7 @@ module ReviseAuth
12
12
  end
13
13
 
14
14
  def copy_styles
15
- template "app/stylesheet/theme.scss", "app/javascripts/packs"
15
+ template "app/stylesheet/theme.scss", "app/javascript/packs/theme.scss"
16
16
  end
17
17
 
18
18
  def copy_controllers
@@ -24,6 +24,7 @@ module ReviseAuth
24
24
  end
25
25
  else
26
26
  directory "app/controllers/revise_auth"
27
+ directory "app/controllers/api"
27
28
  end
28
29
  end
29
30
 
@@ -35,6 +36,7 @@ module ReviseAuth
35
36
  else
36
37
  directory "app/views/revise_auth"
37
38
  directory "app/views/main"
39
+ directory "app/views/shared"
38
40
  end
39
41
  end
40
42
  end
@@ -0,0 +1,46 @@
1
+ module ReviseAuth
2
+ module ApiModel
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ DEFAULT_NAME = "api_token"
7
+ APP_NAME = "my_app"
8
+
9
+ belongs_to :user
10
+
11
+ scope :sorted, -> { order("last_used_at DESC NULLS LAST, created_at DESC") }
12
+
13
+ has_secure_token :token
14
+
15
+ validates :name, presence: true
16
+
17
+ def can?(permission)
18
+ Array.wrap(data("permissions")).include?(permission)
19
+ end
20
+
21
+ def cant?(permission)
22
+ !can?(permission)
23
+ end
24
+
25
+ def data(key, default: nil)
26
+ (metadata || {}).fetch(key, default)
27
+ end
28
+
29
+ def expired?
30
+ expires_at? && Time.current >= expires_at
31
+ end
32
+
33
+ def touch_last_used_at
34
+ return if transient?
35
+ update(last_used_at: Time.current)
36
+ end
37
+
38
+ def generate_token
39
+ loop do
40
+ self.token = SecureRandom.hex(16)
41
+ break unless ApiToken.where(token: token).exists?
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,3 +1,3 @@
1
1
  module ReviseAuth
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
@@ -7,5 +7,6 @@ module ReviseAuth
7
7
  autoload :Backports, "revise_auth/backports"
8
8
  autoload :Current, "revise_auth/current"
9
9
  autoload :Model, "revise_auth/model"
10
+ autoload :ApiModel, "revise_auth/api_model"
10
11
  autoload :RouteConstraint, "revise_auth/route_constraint"
11
12
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: revise_auth-jets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremiah Parrack
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-19 00:00:00.000000000 Z
11
+ date: 2023-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bcrypt
@@ -35,6 +35,8 @@ files:
35
35
  - README.md
36
36
  - Rakefile
37
37
  - app/config/routes.rb
38
+ - app/controllers/api/base_controller.rb
39
+ - app/controllers/api/v1/mes_controller.rb
38
40
  - app/controllers/main_controller.rb
39
41
  - app/controllers/revise_auth/email_controller.rb
40
42
  - app/controllers/revise_auth/password_controller.rb
@@ -62,6 +64,7 @@ files:
62
64
  - lib/generators/revise_auth/templates/README
63
65
  - lib/generators/revise_auth/views_generator.rb
64
66
  - lib/revise_auth-jets.rb
67
+ - lib/revise_auth/api_model.rb
65
68
  - lib/revise_auth/authentication.rb
66
69
  - lib/revise_auth/backports.rb
67
70
  - lib/revise_auth/current.rb