simple_sessions 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/simple_sessions.rb +107 -0
- metadata +49 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 97772e8f7c61538f9e1f968824d252f3726a5e85481857fd9b934b6a9665905a
|
4
|
+
data.tar.gz: d02c7d5bbf09c8735b3912fb7a8464b93c22b9823486aaf75890382554e6418c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 26b875a6370e18fc23d385ccc8d1526fd4659e160766344076a112cf0fbd4ec69c1fe119fe14b5d7ddc5651c50dac6a54835b3768372d8d18b99984f52e3b99d
|
7
|
+
data.tar.gz: 4b0555f96b3bf38b418aab58c3982d4cfb858c3eb3357fc149211f8abe93897f01418ab3aab096682ba5fb2542475fd1f8f614970578ae12e95d9bee34d30365
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# This is a module including all the necessary functions for the sessions
|
4
|
+
module Simple
|
5
|
+
def log_in(user)
|
6
|
+
session[:user_id] = user.id
|
7
|
+
end
|
8
|
+
|
9
|
+
def current_user
|
10
|
+
@current_user ||= User.find_by(id: session[:user_id]) if session[:user_id]
|
11
|
+
end
|
12
|
+
|
13
|
+
def logged_in?
|
14
|
+
!current_user.nil?
|
15
|
+
end
|
16
|
+
|
17
|
+
def log_out
|
18
|
+
@current_user = nil
|
19
|
+
session[:user_id] = nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# This module includes all the advanced functions for sessions
|
24
|
+
module Advanced
|
25
|
+
def log_in(user)
|
26
|
+
session[:user_id] = user.id
|
27
|
+
end
|
28
|
+
|
29
|
+
def remember(user)
|
30
|
+
cookies.permanent.signed[:user_id] = user.id
|
31
|
+
token = SecureRandom.urlsafe_base64
|
32
|
+
cookies.permanent[:remember_token] = token
|
33
|
+
user.remember_digest = Digest::SHA2.hexdigest token
|
34
|
+
end
|
35
|
+
|
36
|
+
def create_session(user)
|
37
|
+
log_in(user)
|
38
|
+
remember(user)
|
39
|
+
end
|
40
|
+
|
41
|
+
def current_user
|
42
|
+
if session[:user_id]
|
43
|
+
@current_user ||= User.find_by(id: session[:user_id])
|
44
|
+
elsif cookies.signed[:id]
|
45
|
+
user = User.find_by(id: cookies.signed[:id])
|
46
|
+
cond1 = Digest::SHA2.hexdigest(cookies[:remember_token]) ==
|
47
|
+
user.remember_digest
|
48
|
+
if user & cond1
|
49
|
+
@current_user ||= user
|
50
|
+
session[:user_id] = user.id
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def logged_in?
|
56
|
+
!current_user.nil?
|
57
|
+
end
|
58
|
+
|
59
|
+
def log_out
|
60
|
+
@current_user = nil
|
61
|
+
session[:user_id] = nil
|
62
|
+
cookies.delete :remember_token
|
63
|
+
cookies.delete :id
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# This is the module to create simple sessions in the controller//This does not work
|
68
|
+
module SimpleController
|
69
|
+
def new; end
|
70
|
+
|
71
|
+
def create
|
72
|
+
user = User.find_by(username: params[:session][:username].downcase)
|
73
|
+
if user&.authenticate(params[:session][:password])
|
74
|
+
log_in user
|
75
|
+
redirect_to user
|
76
|
+
else
|
77
|
+
flash.now[:danger] = 'Invalid username/password combination'
|
78
|
+
render 'new'
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def destroy
|
83
|
+
log_out
|
84
|
+
redirect_to root_path
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# This is the module to create simple sessions in the controller//This does not work
|
89
|
+
module AdvancedController
|
90
|
+
def new; end
|
91
|
+
|
92
|
+
def create
|
93
|
+
user = User.find_by(username: params[:session][:username].downcase)
|
94
|
+
if user&.authenticate(params[:session][:password])
|
95
|
+
create_session user
|
96
|
+
redirect_to user
|
97
|
+
else
|
98
|
+
flash.now[:danger] = 'Invalid username/password combination'
|
99
|
+
render 'new'
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def destroy
|
104
|
+
log_out
|
105
|
+
redirect_to root_path
|
106
|
+
end
|
107
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_sessions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Lucas Mazo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-02-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |-
|
14
|
+
I created this gem to simplify my life (and hopefully, your\
|
15
|
+
life) when creating sessions in rails, in the README file is more\
|
16
|
+
information. As always, my recommendation is to learn how to create\
|
17
|
+
sessions by yourself and then use the gem. More information about this\
|
18
|
+
gem in https://github.com/lucasmazo32/simple-sessions
|
19
|
+
email: lucasmazo32@gmail.com
|
20
|
+
executables: []
|
21
|
+
extensions: []
|
22
|
+
extra_rdoc_files: []
|
23
|
+
files:
|
24
|
+
- lib/simple_sessions.rb
|
25
|
+
homepage: https://rubygems.org/gems/simple_sessions
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubygems_version: 3.0.3
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: This is a gem for creating simple sessions (without a\ permanent cookie)
|
48
|
+
and sessions with permanent cookies
|
49
|
+
test_files: []
|