Re: Sinatra Authentication

Ale Miralles <[email protected]>
Newsgroups gmane.comp.lang.ruby.general
Message-ID <CALbVz+8wp-pEA+1oKnDEd3F9NvOcpMiuwqNskhTs_C+RbyVLpA@mail.gmail.com>
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



On Sat, 20 Oct 2018 at 06:14, Surya Poojary <[email protected]> wrote:

> Hello 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.
>
> Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
>


Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.