Created
April 23, 2015 01:09
-
-
Save k00ka/91383db5046c37539a12 to your computer and use it in GitHub Desktop.
W5D2 Session Management
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
An account for <%= current_user.username %> was just created! | |
Why don't you try <a href="/profile">looking at the profile?</a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
helpers do | |
def current_user | |
User.find_by(id: session[:user_id]) if session[:user_id] | |
end | |
end | |
get '/' do | |
#erb :index | |
redirect '/sign-in' | |
end | |
get '/sign-in' do | |
erb :sign_in | |
end | |
post '/sign-in' do | |
username = params[:username] | |
password = params[:password] | |
# retrieve user | |
user = User.find_by(username: username) | |
# check values | |
if user && user.password == password | |
session[:user_id] = user.id | |
erb :signed_in | |
else | |
redirect '/sign-in' | |
end | |
end | |
get '/sign-up' do | |
erb :sign_up | |
end | |
post '/sign-up' do | |
username = params[:username] | |
password = params[:password] | |
user = User.find_by(username: username) | |
if user #already exists | |
redirect '/sign-in' | |
else | |
# create the new user here | |
user = User.create(username: username, password: password) | |
session[:user_id] = user.id | |
erb :account_created | |
end | |
end | |
get '/profile' do | |
erb :profile | |
end | |
post '/profile' do | |
erb :profile_updated | |
end | |
get '/logout' do | |
session.clear | |
erb :logout | |
end | |
get '/movies/new' do | |
erb :new_movie | |
end | |
post '/movies/create' do | |
erb :movie_created | |
end | |
get '/pins/new' do | |
erb :new_pin | |
end | |
post '/pins/create' do | |
erb :pin_created | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<p> | |
<% if current_user %> | |
This current user is <%= current_user.username %> | |
<% else %> | |
No one is logged in! | |
<% end %> | |
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<!-- | |
normalize.css removes cross-browser differences in defaults, e.g., | |
differences in how form elements appear between Firefox and IE | |
See: http://necolas.github.com/normalize.css/ | |
--> | |
<link rel="stylesheet" href="/stylesheets/normalize.css"> | |
<!-- | |
application.css is where you put your styles | |
--> | |
<link rel="stylesheet" href="/stylesheets/application.css"> | |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
<script src="/javascript/application.js"></script> | |
<title></title> | |
</head> | |
<body> | |
<%= erb(:header, layout: false) %> | |
<%= yield %> | |
<%= erb(:footer, layout: false) %> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
profile | |
<form action="/profile" method="POST"> | |
<input type="text" name="username" value="<%= current_user.username %>" /> | |
<input type="text" name="password" value="<%= current_user.password %>" /> | |
<button type="submit">Update profile</button> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
profile created | |
Hey, we got the value <%= @key %> for the key parameter! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Sign in</h1> | |
<br/> | |
<form method="POST" action="/sign-in"> | |
<input type="text" name="username" placeholder="username"> | |
<input type="password" name="password" placeholder="password"> | |
<button type="submit">Sign in</button> | |
</form> | |
<br/> | |
Don't have an account? <a href="/sign-up">Sign up!</a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>Sign up</h1> | |
<br/> | |
<form action="/sign-up" method="POST"> | |
<input type="text" name="username" placeholder="username"> | |
<input type="password" name="password" placeholder="password"> | |
<button type="submit">Sign up</button> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Signed in | |
You signed in! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment