Skip to content

Instantly share code, notes, and snippets.

@kevinhq
kevinhq / Gemfile
Created July 3, 2020 06:15
Gemfile with custom environment
group :optimization do
gem 'bullet', '~> 6.1.0'
end
@kevinhq
kevinhq / Gemfile
Created July 3, 2020 06:14
Optimize Rails app with Bullet - Gemfile
gem 'bullet', '~> 6.1.0'
@kevinhq
kevinhq / users_controller.rb
Created June 23, 2020 11:49
Two-Factor authentication users controller
# app/controllers/users_controller.rb
class UsersController < ApplicationController
def activate_2fa
qrcode = RQRCode::QRCode.new(current_user.provisioning_uri(nil, issuer: 'your-app-url.com'), :size => 12, :level => :h)
@svg = qrcode.as_svg(offset: 0, color: '000',
shape_rendering: 'crispEdges',
module_size: 4)
respond_to :html
end
@kevinhq
kevinhq / two_factors_authentication.html.erb
Created June 23, 2020 11:49
Two-Factor authentication - form after login
<%# /app/views/users/sessions/two_factors_authentication.html.erb %>
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name), :html => { :role => 'form', :method => 'POST' }) do |f| %>
<%= f.hidden_field :email, value: params[:user][:email] %>
<%= f.text_field :otp_response_code %>
<%= f.submit %>
<% end %>
@kevinhq
kevinhq / sessions_controller.rb
Created June 23, 2020 11:48
Two-Factor authentication with devise - override sessions controller
# /app/controllers/users/sessions_controller.rb
class Users::SessionsController < Devise::SessionsController
def create
self.resource = resource_class.find_for_authentication(sign_in_params.except(:password, :otp_response_code))
if resource
if resource.active_for_authentication?
if resource && resource.otp_module_disabled?
continue_sign_in(resource, resource_name)
elsif resource && resource.otp_module_enabled?
@kevinhq
kevinhq / Gemfile
Created June 23, 2020 11:47
Gemfile for Two-Factor authentication with devise and active_model_otp gem
gem 'devise', '~> 4.7.1'
gem 'active_model_otp', '~> 2.0.1'
gem 'rqrcode', '~> 1.1.2
@kevinhq
kevinhq / Gemfile
Last active June 23, 2020 11:44
Gemfile for Two-Factor authentication with devise and active_model_otp gem
gem 'devise', '~> 4.7.1'
gem 'active_model_otp', '~> 2.0.1'
gem 'rqrcode', '~> 1.1.2
@kevinhq
kevinhq / activate_2fa.html.erb
Created June 23, 2020 11:43
Two-Factor authentication form with QR code
<# /app/views/users/activate_2fa.html.erb %>
<%= @svg.html_safe %>
<%= form_for(@user, url: activate_2fa_update_path) do |f| %>
<%= f.text_field :otp_response_code %>
<%= f.submit %>
<% end %>
@kevinhq
kevinhq / Gemfile
Last active August 11, 2022 15:20
How to implement two-factor authentication for Rails app by using Devise gem, Google authenticator, and ActiveModel::Otp gem
gem 'devise', '~> 4.7.1'
gem 'active_model_otp', '~> 2.0.1'
gem 'rqrcode', '~> 1.1.2'
@kevinhq
kevinhq / clean.sh
Created May 26, 2020 09:22 — forked from Iman/clean.sh
Free up disk space on Ubuntu - clean log, cache, archive packages/apt archives, orphaned packages, old kernel and remove the trash
#!/bin/sh
#Check the Drive Space Used by Cached Files
du -sh /var/cache/apt/archives
#Clean all the log file
#for logs in `find /var/log -type f`; do > $logs; done
logs=`find /var/log -type f`
for i in $logs