Skip to content

Commit 07071e7

Browse files
committed
clean up ENV vars and fix flash message in login
1 parent 739fe60 commit 07071e7

File tree

16 files changed

+110
-44
lines changed

16 files changed

+110
-44
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ While this is an API-only application you will not be able to access any routes
6868

6969
Point the GraphQL IDE to `http://0.0.0.0:3000/graphql`
7070

71-
**Note:** Make sure that the `.env` file is included in the root of your project and you have defined `CLIENT_URL` and `DEVISE_JWT_SECRET_KEY`. You can try out the [Demo frontend](https://github.com/zauberware/gatsby-starter-redux-saas) or you implement the actions in any other client. Read more about the JSON Web Token [this](https://github.com/zauberware/rails-devise-graphql). There are plenty of packages available.
71+
**Note:** Make sure that the `.env` file is included in the root of your project and you have defined `CLIENT_URL` and `DEVISE_SECRET_KEY`. You can try out the [Demo frontend](https://github.com/zauberware/gatsby-starter-redux-saas) or you implement the actions in any other client. Read more about the JSON Web Token [this](https://github.com/zauberware/rails-devise-graphql). There are plenty of packages available.
7272

7373
## What's included?
7474

@@ -114,7 +114,7 @@ Change rails_admin settins under `config/initializers/rails_admin.rb`.
114114
This app has the default language `en` and already set a secondary language `de`. We included the [rails-i18n](https://github.com/svenfuchs/rails-i18n) to support other languages out of the box. Add more languages under `config/initializers/locale.rb`.
115115

116116
#### Setting locale
117-
To switch locale just append `?locale=de` at the end of your url.
117+
To switch locale just append `?locale=de` at the end of your url. If no `locale` param was set it uses browser default language (request env `HTTP_ACCEPT_LANGUAGE`). If this is unknown it takes the default language of the rails app.
118118

119119
#### Devise
120120
For devise we use [devise-i18n](https://github.com/tigrish/devise-i18n) to support other languages.

app.json

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,29 @@
2323
"postdeploy": "rake db:migrate && rake db:seed"
2424
},
2525
"env": {
26-
"CLIENT_URL": {
27-
"description": "Enter the client domain for CORS. Choose http://0.0.0.0:8000 when using our gatsby frontend.",
28-
"value": "http://0.0.0.0:8000"
26+
"DEVISE_SECRET_KEY": {
27+
"description": "A secret key for verifying the integrity of signed JWT tokens.",
28+
"value": "replace-this-key-with-a-secret"
2929
},
30-
"WEB_CONCURRENCY": {
31-
"description": "Specifies the number of `workers` to boot in clustered mode.",
32-
"value": "2"
30+
"DEFAULT_URL": {
31+
"description": "Enter the url of this application. If you deploy to heroku set your heroku address.",
32+
"value": "http://...herokuapp.com"
3333
},
34-
"RAILS_MAX_THREADS": {
35-
"description": "Puma can serve each request in a thread from an internal thread pool.",
36-
"value": "5"
34+
"CLIENT_URL": {
35+
"description": "Enter the client domain for CORS.",
36+
"value": "http://0.0.0.0:8000"
3737
},
38-
"DEVISE_JWT_SECRET_KEY": {
39-
"description": "A secret key for verifying the integrity of signed JWT tokens.",
40-
"generator": "secret"
38+
"DEVISE_MAILER_FROM": {
39+
"description": "Set the default from address for mailers.",
40+
"value": "[email protected]"
4141
},
4242
"ADMIN_EMAIL": {
4343
"description": "Admin default login.",
4444
"value": "[email protected]"
4545
},
4646
"ADMIN_PASSWORD": {
4747
"description": "Admin default password.",
48-
"value": "demo123"
48+
"value": "demo1234"
4949
},
5050
"ADMIN_FIRST_NAME": {
5151
"description": "Admin default firstname.",
@@ -54,6 +54,26 @@
5454
"ADMIN_LAST_NAME": {
5555
"description": "Admin default lastname.",
5656
"value": "Doe"
57+
},
58+
"IS_HTTP_AUTH_PROTECTED": {
59+
"description": "Enable http auth protection with setting this to true.",
60+
"value": "false"
61+
},
62+
"HTTP_AUTH_USER": {
63+
"description": "Set user for HTTP Auth protection.",
64+
"value": "demo"
65+
},
66+
"HTTP_AUTH_PASSWORD": {
67+
"description": "Set password for HTTP Auth protection.",
68+
"value": "demo1234"
69+
},
70+
"WEB_CONCURRENCY": {
71+
"description": "Specifies the number of `workers` to boot in clustered mode.",
72+
"value": "2"
73+
},
74+
"RAILS_MAX_THREADS": {
75+
"description": "Puma can serve each request in a thread from an internal thread pool.",
76+
"value": "5"
5777
}
5878
},
5979
"image": "heroku/ruby",

app/assets/stylesheets/frontend/_vars.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ $brand-warning: #e1c345;
1717
$brand-danger: #e5734b;
1818

1919

20-
2120
$font-family-sans-serif: "Roboto",-apple-system,BlinkMacSystemFont,"Segoe UI","Helvetica Neue",Arial,sans-serif;
2221
$font-family-serif: "Roboto Slab",-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif;
2322
//** Default monospace fonts for `<code>`, `<kbd>`, and `<pre>`.

app/assets/stylesheets/frontend/main.scss

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,30 @@ h1,h2,h3,h4,h5,h6{
8080
color: $gray;
8181
}
8282

83+
.field{
84+
text-align: left;
85+
}
86+
8387
.logo-wrapper{
8488
background-color: white;
8589
display: inline-block;
8690
padding: 15px 10px 10px 10px;
91+
}
92+
93+
.flash{
94+
padding: 10px;
95+
text-align: center;
96+
background: $brand-info;
97+
margin: 10px 0;
98+
color: white;
99+
border-radius: 3px;
100+
font-weight: 700;
101+
102+
&--error{
103+
background: $brand-danger;
104+
}
105+
106+
&--alert{
107+
background: $brand-warning;
108+
}
87109
}

app/controllers/application_controller.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
# Base controlller for application
44
class ApplicationController < ActionController::Base
55
include HttpAuth
6-
7-
force_ssl if: :ssl_configured?
6+
include LocaleWrapper
87

98
rescue_from CanCan::AccessDenied do |exception|
109
respond_to do |format|
@@ -14,13 +13,6 @@ class ApplicationController < ActionController::Base
1413
end
1514
end
1615

17-
# setting locale from URL parameter
18-
around_action :switch_locale
19-
def switch_locale(&action)
20-
locale = params[:locale] || I18n.default_locale
21-
I18n.with_locale(locale, &action)
22-
end
23-
2416
protected
2517

2618
def current_superadmin
@@ -32,10 +24,6 @@ def current_superadmin
3224

3325
private
3426

35-
def ssl_configured?
36-
Rails.env.production?
37-
end
38-
3927
def after_sign_in_path_for(resource)
4028
if resource.superadmin? || resource.admin?
4129
rails_admin_url

app/controllers/concerns/http_auth.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
module HttpAuth
1+
# frozen_string_literal: true
2+
3+
# Injects http authentication to a controller.
4+
module HttpAuth
25
extend ActiveSupport::Concern
36

47
included do
58
before_action :http_authenticate
69
end
710

811
protected
12+
913
def http_authenticate
1014
return true unless ENV['IS_HTTP_AUTH_PROTECTED'] == 'true'
1115

1216
authenticate_or_request_with_http_basic do |username, password|
1317
username == ENV['HTTP_AUTH_USER'] && password == ENV['HTTP_AUTH_PASSWORD']
1418
end
1519
end
16-
end
20+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
# Handles setting of locale in your controller.
4+
module LocaleWrapper
5+
extend ActiveSupport::Concern
6+
7+
included do
8+
around_action :switch_locale
9+
end
10+
11+
protected
12+
13+
def switch_locale(&action)
14+
locale = params[:locale] || locale_from_http_accept_lang || I18n.default_locale
15+
I18n.with_locale(locale, &action)
16+
end
17+
18+
def locale_from_http_accept_lang
19+
return nil if !request || !request.env['HTTP_ACCEPT_LANGUAGE']
20+
21+
locale = request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first
22+
return nil unless locale
23+
24+
# only if match
25+
I18n.available_locales.map(&:to_s).include?(locale) ? locale : nil
26+
end
27+
end

app/mailers/application_mailer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
# Base class for all application mailer
44
class ApplicationMailer < ActionMailer::Base
5-
default from: '[email protected]'
5+
default from: ENV['DEVISE_MAILER_FROM']
66
layout 'mailer'
77
end

app/views/devise/sessions/new.html.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
<h2><%= t('.sign_in') %></h2>
77

88
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
9+
<%= render "devise/shared/error_messages", resource: resource %>
10+
911
<div class="field">
1012
<%= f.label :email %><br />
1113
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>

app/views/devise/shared/_error_messages.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@
1313
</ul>
1414
</div>
1515
<% end %>
16+
17+
<% flash.each do |key, value| %>
18+
<% key = (key == 'alert' || key == 'error') ? 'error' : 'info' %>
19+
<%= content_tag(:div, value, class: "flash flash--#{key}") %>
20+
<% end %>

0 commit comments

Comments
 (0)