forked from mjackson/sinatra-session
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
122 lines (95 loc) · 3.74 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
Sinatra::Session
================
Sinatra::Session is a small extension for Sinatra apps that provides several
convenient settings and methods when working with cookie-based sessions.
Settings
--------
session_fail -> A URL in your app that the client will be redirected to
when `session!` is called and the session is not valid.
Defaults to '/login'
session_name -> The name to use for the session cookie
session_path -> The session cookie path
session_domain -> The session cookie domain
session_expire -> The number of seconds from now the cookie will be valid
session_secret -> A secret key that is used to check the integrity of
session data
Helper Methods
--------------
session_start! -> Starts the session
session_end!(destroy=true) -> Ends the session. If `destroy` is false then
session data will be preserved, even though
future calls to `session?` will return false
session? -> Returns true if the session is valid
session! -> Redirects the client to the URL specified in
the `session_fail` option unless the session is
valid
Installation
------------
Via RubyGems:
$ sudo gem install sinatra-session
From a local copy:
$ git clone git://github.com/mjijackson/sinatra-session.git
$ cd sinatra-session
$ rake package && sudo rake install
Usage
-----
As with all Sinatra extensions, Sinatra::Session may be used in both classic
and modular-style apps. First, classic.
require 'sinatra'
require 'sinatra/session'
set :session_fail, '/login'
set :session_secret, 'So0perSeKr3t!'
get '/' do
session!
'Hello, ' + session[:name] + '! Click <a href="/logout">here</a> to logout.'
end
get '/login' do
if session?
redirect '/'
else
'<form method="POST" action="/login">' +
'Please enter your name: <input type="text" name="name">' +
'<input type="submit" value="Submit">' +
'</form>'
end
end
post '/login' do
if params[:name]
session_start!
session[:name] = params[:name]
redirect '/'
else
redirect '/login'
end
end
get '/logout' do
session_end!
redirect '/'
end
Modular apps use very similar code but must also mixin Sinatra::Session via
Sinatra#register, like so:
require 'sinatra/base'
require 'sinatra/session'
class MyApp < Sinatra::Base
register Sinatra::Session
# insert settings and routes here, same as in
# classic example above
end
License
-------
Copyright 2010 Michael Jackson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.