Database Reference
In-Depth Information
Login Form
The HTML required for the user login form is shown in Listing 10-19 and can be found in the
{PROJECTROOT}/ app/
views/global/homeheader.mustache
layout file.
Listing 10-19.
The Login Form
<form class="navbar-form navbar-right" action="/login" role="form" method="post">
<div class="form-group">
<input type="text" placeholder="Username" name="username" class="form-control">
</div>
<button type="submit" class="btn btn-success">Sign in</button>
</form>
Login Route
In the
App
class, use the login route to control the flow of the login process, as shown in Listing 10-20. Inside the login
route, use the
get_user_by_user_name
method to check if the user exists in the database.
Listing 10-20.
The
login
Route
# login
post '/login' do
# search db for user
user = get_user_by_user_name(neo,params[:username])
# found it! set cookie and redirect
if !user.nil? && !user.empty?
@user = user
response.set_cookie(graphstoryUserAuthKey, :value => @user["username"], :path => "/", :expires
=> Time.now + 86400000)
redirect to('/social')
# not found. show message
else
@title = "Home"
@error ="The username you entered was not found."
mustache :"home/index"
end
end