Module: PropelAuthenticationConcern

Extended by:
ActiveSupport::Concern
Defined in:
lib/generators/propel_authentication/templates/concerns/propel_authentication_concern.rb

Instance Method Summary collapse

Instance Method Details

#authenticate_userObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/generators/propel_authentication/templates/concerns/propel_authentication_concern.rb', line 6

def authenticate_user
  token = extract_jwt_token
  
  unless token
    render json: { error: 'No token provided' }, status: :unauthorized
    return false
  end
  
  begin
    @current_user = User.find_by_jwt_token(token)
    unless @current_user
      render json: { error: 'Invalid token' }, status: :unauthorized
      return false
    end
    
    # Extract organization context from JWT payload
    extract_organization_context(token)
    
  rescue JWT::ExpiredSignature
    render json: { error: 'Token expired' }, status: :unauthorized
    return false
  rescue JWT::DecodeError, JWT::InvalidSignature
    render json: { error: 'Invalid token' }, status: :unauthorized
    return false
  end
  
  true
end

#current_agency_idsObject



47
48
49
50
51
52
# File 'lib/generators/propel_authentication/templates/concerns/propel_authentication_concern.rb', line 47

def current_agency_ids
  return [] unless current_user
  
  # Request-scoped memoization for performance without security risk
  @current_agency_ids ||= current_user.agency_ids
end

#current_organizationObject



43
44
45
# File 'lib/generators/propel_authentication/templates/concerns/propel_authentication_concern.rb', line 43

def current_organization
  @current_organization ||= Organization.find_by(id: current_organization_id) if current_organization_id
end

#current_organization_idObject



39
40
41
# File 'lib/generators/propel_authentication/templates/concerns/propel_authentication_concern.rb', line 39

def current_organization_id
  @current_organization_id
end

#current_userObject



35
36
37
# File 'lib/generators/propel_authentication/templates/concerns/propel_authentication_concern.rb', line 35

def current_user
  @current_user
end

#extract_jwt_tokenObject



58
59
60
61
62
63
64
# File 'lib/generators/propel_authentication/templates/concerns/propel_authentication_concern.rb', line 58

def extract_jwt_token
  auth_header = request.headers['Authorization']
  return nil unless auth_header
  
  # Extract token from "Bearer <token>" format
  auth_header.split(' ').last if auth_header.start_with?('Bearer ')
end

#has_agency_access?(agency_id) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/generators/propel_authentication/templates/concerns/propel_authentication_concern.rb', line 54

def has_agency_access?(agency_id)
  current_agency_ids.include?(agency_id.to_i)
end