This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,3 +65,5 @@ group :test do | |
end | ||
|
||
gem "devise", "~> 4.9" | ||
|
||
gem "device_detector", "~> 1.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,43 @@ | ||
class ApplicationController < ActionController::Base | ||
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has. | ||
allow_browser versions: :modern | ||
|
||
before_action :require_login, if: :current_user | ||
|
||
def after_sign_in_path_for(resource) | ||
# create_login # you can move this to your sessions_controller#create | ||
root_path | ||
end | ||
|
||
private | ||
|
||
def create_login | ||
device_id = Digest::SHA256.hexdigest("#{request.user_agent}#{request.remote_ip}") | ||
current_login = current_user.logins.find_or_create_by(device_id: device_id, ip_address: request.remote_ip, user_agent: request.user_agent) | ||
session[:device_id] = device_id | ||
end | ||
|
||
# trigger this in your sessions_controller#destroy | ||
def destroy_login | ||
current_user.logins.find_by(device_id: session[:device_id])&.destroy | ||
session.delete(:device_id) | ||
end | ||
|
||
def require_login | ||
# after_sign_in_path_for is triggered after require_login | ||
# return if controller_path == 'devise/sessions' && action_name == 'create' | ||
return if controller_path == 'users/sessions' && action_name == 'create' # if you are overriding devise sessions_controller | ||
Check failure on line 29 in app/controllers/application_controller.rb GitHub Actions / lint
|
||
|
||
if Rails.env.test? | ||
# mock | ||
current_login = current_user.logins.create(device_id: "test_device_id") | ||
else | ||
current_login = current_user.logins.find_by(device_id: session[:device_id]) | ||
end | ||
|
||
if current_login.nil? | ||
sign_out current_user | ||
redirect_to new_user_session_path, alert: "Device not recognized." | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class LoginsController < ApplicationController | ||
before_action :authenticate_user! | ||
|
||
def index | ||
@logins = current_user.logins | ||
end | ||
|
||
def destroy | ||
@login = current_user.logins.find(params[:id]) | ||
@login.destroy! | ||
redirect_to logins_url, notice: "Device disconnected." | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
class Users::SessionsController < Devise::SessionsController | ||
# before_action :configure_sign_in_params, only: [:create] | ||
|
||
# GET /resource/sign_in | ||
# def new | ||
# super | ||
# end | ||
|
||
# POST /resource/sign_in | ||
def create | ||
# add require_login | ||
super do |resource| | ||
create_login if resource.persisted? | ||
end | ||
end | ||
|
||
# DELETE /resource/sign_out | ||
def destroy | ||
destroy_login | ||
super | ||
end | ||
|
||
# protected | ||
|
||
# If you have extra params to permit, append them to the sanitizer. | ||
# def configure_sign_in_params | ||
# devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute]) | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module LoginsHelper | ||
def device_description(user_agent) | ||
device = DeviceDetector.new(user_agent) | ||
[device.name, device.os_name, device.device_type].join(' / ') | ||
Check failure on line 4 in app/helpers/logins_helper.rb GitHub Actions / lint
Check failure on line 4 in app/helpers/logins_helper.rb GitHub Actions / lint
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Login < ApplicationRecord | ||
belongs_to :user | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<h1> | ||
Logins: | ||
<%= @logins.size %> | ||
</h1> | ||
|
||
<% @logins.order(updated_at: :desc).each do |login| %> | ||
<div class="border"> | ||
<%= device_description(login.user_agent) %> | ||
|
||
Last login at: | ||
<%= login.updated_at %> | ||
|
||
IP address: | ||
<%= login.ip_address %> | ||
<% if login.device_id == session[:device_id] %> | ||
<span class="text-green-500">current session</span> | ||
<% else %> | ||
<%= button_to 'Disconnect', login_path(login), method: :delete, class: "text-red-500" %> | ||
<% end %> | ||
|
||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CreateLogins < ActiveRecord::Migration[7.2] | ||
def change | ||
create_table :logins do |t| | ||
t.references :user, null: false, foreign_key: true | ||
t.string :device_id | ||
t.string :ip_address | ||
t.string :user_agent | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.