revise_auth-jets 0.3.0 → 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: 60a796b82520022a11e3865b8e78b878a5cfbff520a27e7fd45eac1e60a74217
4
- data.tar.gz: 99bce8157842ac16506e47d78ec76fde277387058f72135bd6a6d6092d6558a3
3
+ metadata.gz: 825c70cb5515ef92564d9ba805da0c6629b8822c3b88b3760f4cee3b44e18c6c
4
+ data.tar.gz: f3befb88b7f16a042ed269bcc3e2bf12a50c58a512a0215f992587bbb95d010c
5
5
  SHA512:
6
- metadata.gz: 29e42e645f8f994e5c8115206b1ec7c7972a841d891a0a10f921a8f9acf24ba2f50fcb11fdefd0668aad78881226f343b2fc4add0805bae4dfbc76d4ec408217
7
- data.tar.gz: '0904636191cbdb182eeb4e8361062e3a93799929380f3c08550283b81acdfb066f610baacc7657cfcf8805a1c89208c4f13f53ad358364af89a98242126faae4'
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")