Module: Apihelpers::ApiAuth

Defined in:
app/api/apihelpers/api_auth.rb

Instance Method Summary collapse

Instance Method Details

#authenticatedObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/api/apihelpers/api_auth.rb', line 6

def authenticated
	if warden.authenticated?
		return true
	elsif params[:api_token]
		user = User.find_by(api_token: params[:api_token])
		if user && user.valid_api_token?
			return true
		else
			error!('401 Unauthorized', 401)
		end
	else
  		error!('401 Unauthorized', 401)
	end
end

#authenticated_adminObject



46
47
48
49
# File 'app/api/apihelpers/api_auth.rb', line 46

def authenticated_admin
	authenticated
	error!('401 Unauthorized', 401) unless is_admin?
end

#authenticated_userObject



41
42
43
44
# File 'app/api/apihelpers/api_auth.rb', line 41

def authenticated_user
	authenticated
	error!('401 Unauthorized', 401) unless current_user
end

#current_userObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/api/apihelpers/api_auth.rb', line 23

def current_user
	if warden.user
		return warden.user
	else
		user = nil
		user = User.find_by(api_token: params[:api_token]) if params[:api_token]
		if user && user.valid_api_token?
			return user
		else
			return nil
		end
	end
end

#is_admin?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/api/apihelpers/api_auth.rb', line 37

def is_admin?
	current_user && current_user.type == "Admin"
end

#wardenObject



2
3
4
# File 'app/api/apihelpers/api_auth.rb', line 2

def warden
	env['warden']
end