I recommend you to check the Sinatra's home page. They put in there almost
all you need to get a Sinatra app off the ground.
Anyway, here is a minimum example you can use to fiddle around a little bit.
This is the contents of the main file. (i.e., app.rb):
require 'sinatra'
enable :sessions
get "/" do
if session[:usr]
erb :index
else
erb :login
end
end
post "/create_session" do
if params[:usr] == "amiralles" && params[:pwd] == "123"
session[:usr] = params[:usr]
redirect "/"
else
erb :login
end
end
Now, create a directory called "views" and place this files on it:
Homepage: index.erb
<h1>
Wellcome <%= session[:usr] %>!
</h1>
Login page: login.erb
<form action="/create_session" method="POST">
<input name="usr" type="text"></input> <br/>
<input name="pwd" type="password"></input> <br/>
<input type="submit" value="Login"></input>
</form>
Finally, run `ruby app.rb` and you will be ready to go.
Hope this helps!
~ Ale Miralles.
https://medium.com/amiralles
http://amiralles.com.ar
Post by Surya PoojaryHello rubyists!
I'm trying to learn Sinatra and want to implement an authentication system
like
If username == "me" and password == "she" // go to the next page
Else
// Take to some page that says "you are not authorised"
How do I do this ?
Gracias,
Rubyists.
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>