breath 0.1.2 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2785b5ee6dbd2e81dad460ef63f97dcfcb481e93e72d1805019c12911766e55
4
- data.tar.gz: 29c3bcfc6c28916d7a5dbf54bb9937688f008d938c2d27135219f4ec34c6dd72
3
+ metadata.gz: 76b40953cb6e71c98a7870cc2a90360ff11e7dbfeeacb38ba02ec212ac8995d3
4
+ data.tar.gz: 7324b79742b9329015d3e527a6ed5c0abe5402e999933626fb912c2b004e017f
5
5
  SHA512:
6
- metadata.gz: 6749ed6e952b1da8a7e5074f33810293c63d1e1629df7390319f6d4e28fa79948df168c25abb1cc0b5d457c30648fd427a62b089c9b40eef6df2892bb9a4a517
7
- data.tar.gz: fb538d0d914d31c9e693b64fe24bcc7d615b7c6bacb77fcb70b9c82d05b425614cb94d902fc8e8301383f3a6ea8705f2d5389895ee437489a2f5630d965b246a
6
+ metadata.gz: f9c1a788f8de0f6765fc943e721f6d468fc44a57174951c5b0a4e02b9fe93971bd194d835e407dcbc8dac905997a88b1bf22fbb8d6166cd8580ddba857b3af87
7
+ data.tar.gz: 53d89f967a23d992c47d9692f7c8cd9270a1f51422f9bfaf6b17b8a2d11d2cd53ba76eadc525ffd54d8b257e957910a3b089c913f1605dc2331e410dade80743
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  # Breath
2
- Short description and motivation.
3
-
4
- ## Usage
5
- How to use my plugin.
2
+ Rails authentication plugin with API mode.<br />
3
+ Easy introducing login, logout.<br/>
4
+ Compact features set.
6
5
 
7
6
  ## Installation
8
7
  Add this line to your application's Gemfile:
@@ -21,6 +20,118 @@ Or install it yourself as:
21
20
  $ gem install breath
22
21
  ```
23
22
 
23
+ ## Usage
24
+ ### Introduce
25
+
26
+ #### Model
27
+ If you want to introduce a authentication to `User` model.<br/>
28
+ Add these lines to `user.rb` like bellow.
29
+ ```ruby
30
+ class User < ApplicationRecord
31
+ include Breath::Model
32
+
33
+ attr_breath :email
34
+ end
35
+ ```
36
+ Here, with `attr_breath`, you need to specify the user's attribute with authentication.
37
+
38
+ #### Migration
39
+ In migration file, you need to add `password_digest`, and `remember_digest` attributes.<br/>
40
+ And if you specify the email attribute within `attr_breath`, you need to add `email` attribute in migration file.
41
+ ```ruby
42
+ class CreateUsers < ActiveRecord::Migration[7.0]
43
+ def change
44
+ create_table :users do |t|
45
+ t.string :email
46
+ t.string :password_digest
47
+ t.string :remember_digest
48
+
49
+ t.timestamps
50
+ end
51
+ end
52
+ end
53
+ ```
54
+ After these lines you added, excute `bundle exec rails db:migrate`.
55
+
56
+
57
+ #### Controller
58
+ You need to construct the directory like bellow.
59
+ ```
60
+ /app/controllers
61
+ - application_controller.rb
62
+
63
+ /users
64
+ - application_controller.rb
65
+ - sessions_controller.rb
66
+ ```
67
+
68
+ - app/contorllers/users/application_controller.rb
69
+ ```ruby
70
+ class Users::ApplicationController < ApplicationController
71
+ include Breath::ApplicationControllerHelper
72
+ before_action :authenticate!
73
+
74
+ crsf_protect true
75
+ end
76
+ ```
77
+ Here, if `csrf_protect` is `true`, CSRF protection is enabled.<br/>
78
+
79
+ - app/controllers/users/sessions_controller.rb
80
+ ```ruby
81
+ class Users::SessionsController < Users::ApplicationController
82
+ include Breath::SessionsControllerHelper
83
+ end
84
+ ```
85
+
86
+ `Breath::ApplicationControllerHelper` intoroduce the user's authorization.<br/>
87
+ `Breath::SessionsControllerHelper` introduce the actions `login`, and `logout`.
88
+
89
+ Then, you don't need write the codes to introduce authorizations.<br/>
90
+
91
+ You can use `current_user` method which is current logined user.
92
+
93
+ #### Route
94
+ Write `route.rb`
95
+ ```ruby
96
+ Rails.application.routes.draw do
97
+ breath :users
98
+
99
+ ...or...
100
+
101
+ breath :users do
102
+ get "test" => "sessions#test"
103
+ end
104
+ end
105
+ ```
106
+
107
+ After you added these lines, show `bundle exec rails routes` command.<br/>
108
+ You can see these routes are added.
109
+ ```
110
+ GET /users/login
111
+ POST /users/login
112
+ DELETE /users/logout
113
+ ```
114
+ Or, nested users routes.
115
+
116
+ #### Config
117
+ This plugin need cookie, and you can configure the cookie expires like bellow.<br/>
118
+ ```ruby
119
+ module YourApp
120
+ class Application < Rails::Application
121
+ ...
122
+
123
+ config.breath_expires = 3.days
124
+ end
125
+ end
126
+ ```
127
+ If you don't configure this, cookie is set permanently.
128
+
129
+ #### Last Work
130
+ You need to create view side.<br/>
131
+ In view side, you have remaining works.<br/>
132
+ if you `csrf_protect true`, you need to introduce `withCredentials: true` option in client side.<br/>
133
+ And, write csrf token into the cookie with `csrf_token` key.
134
+
24
135
  ## Contributing
25
136
  Contribution directions go here.
26
137
 
@@ -20,7 +20,7 @@ module Breath
20
20
 
21
21
  raise AuthenticationError if target.nil?
22
22
  rescue StandardError => e
23
- send :render_401, e.to_s
23
+ send :render_401, e
24
24
  end
25
25
 
26
26
  define_method current_target do
@@ -66,6 +66,12 @@ module Breath
66
66
 
67
67
  render json: res, status: 409
68
68
  end
69
+
70
+ def render_422(res)
71
+ Rails.logger.error error_message(res)
72
+
73
+ render json: res, status: 422
74
+ end
69
75
 
70
76
  def render_500(res)
71
77
  Rails.logger.error error_message(res)
@@ -7,6 +7,8 @@ module Breath
7
7
  class InvalidPassword < StandardError; end
8
8
 
9
9
  included do
10
+ rescue_from ActionController::InvalidAuthenticityToken, with: :render_422
11
+
10
12
  target_class = to_s.split("::")[-2].singularize.constantize
11
13
  target_name = target_class.to_s.underscore
12
14
  current_target = "current_#{target_name}"
@@ -29,7 +31,7 @@ module Breath
29
31
 
30
32
  render status: 200
31
33
  rescue StandardError => e
32
- send :render_401, e.to_s
34
+ send :render_401, e
33
35
  end
34
36
 
35
37
  # DELETE /schedule_kun/target/logout
@@ -44,7 +46,7 @@ module Breath
44
46
  end
45
47
 
46
48
  define_method :sessions_params do
47
- params.require(:sessions).permit(target_class.auth_attribute.to_sym, :password, :password_confirmation)
49
+ params.require(:sessions).permit(target_class.auth_attribute.to_sym, :password)
48
50
  end
49
51
  end
50
52
 
@@ -1,3 +1,3 @@
1
1
  module Breath
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: breath
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - testCodeV01
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-24 00:00:00.000000000 Z
11
+ date: 2023-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails