Skip to main content

Sign in page

AAAA
<% if session[:username] %> <form method="post" action="/users/signout"> <p class="user-status"> Signed in as <%= session[:username] %>. <button type="submit">Sign Out</button> </p> </form> <% else %> <p class="user-status"><a href="/users/signin">Sign In</a></p> <% end %>


Explanaion:
1. if in the session hash, key :username has a value, then perform this:
- create a form , the form will create a HTTP post request to /user/signout
- the form will only submit when the button is clicked, that's why it has a type="submit"
- otherwise, if the button is not clicked, just display the paragraph/message of "Signed in as username"

2. Without username value
- create a link to '/users/signin' page with a HTTP GET request, with a text "Sign In"


BBBB (Views/Sigin)
<form method="post" action="/users/signin"> <div> <label for="username"> Username: <input name="username" value="<%= params[:username] %>"/> </label> </div> <div> <label for="password"> Password: <input type="password" name="password" /> </label> </div> <button type="submit">Sign In</button> </form>


Explanaion:
1. When redirected to the sign in , there is a form with a HTTP post request to "/users/signin"
2. There is two division created by the two <div>
3. First <label> is for username , there is an input tag that create a params[:username].
The value attribute will display username if there is one already.

CCCC
get "/users/signin" do erb :signin end

Explanaion:
When redirected to the sign in via GET , This will render view/sigin erb page.
post "/users/signin" do if params[:username] == "admin" && params[:password] == "secret" session[:username] = params[:username] session[:message] = "Welcome!" redirect "/" else session[:message] = "Invalid credentials" status 422 erb :signin end end

Explanaion:
When directed to the "/users/signin" page via a HTTP post request, then check if params[:username]
is equal to "admin" and params[:secret] is equal to "secret". If yes, then set username variable to
session[:username]. Also create a key/value hash with the value "Welcome!".

If the username is not "admin" or password is not "secret", or both, then create a key/value has with
the value "Invalid credentials". Assigned a status of 422

Status code 422 :
1. understands the content type of the request entity
2. syntax of the request entity is correct 
3. was unable to process the contained instructions

Then render the erb page "signin" again, because it is invalid credential
post "/users/signout" do session.delete(:username) session[:message] = "You have been signed out." redirect "/" end

Explanaion:
In the case of HTTP POST to "users/signout" , then you delete the :username session. Append the
message "You have been signed out."

Redirect to "/".

Comments

Popular posts from this blog

Problem Solving - Refactored

I am going to outline how I approach problem solving. The relative importance and the amount of effort/time required for each is stated as a percentage beside each topic. I borrowed some idea from George Polya's How to Solve It Thoroughly Understand the Problem (30%) When encountering hard problem , you need to deeply understand the problem at hand. Take a paper and list down all known facts and data and what the question is trying to find. Sketch out the problem if applicable. Visualize the problem in your head. A lot of times, we only have to understand the problem well, then the solution will obvious. Have a Plan (20%) You need to have an outline of how you are going to tackle the problem. You need to have a logical pathway that will ultimate produce outcome (nothing to do with coding syntax yet). Without a plan, you are just randomly poking around and got lucky. No hard problem ever gets solved without a plan. Plan using pseudo-code, pen & paper or flowchart. Use wh

My Burnout Experience

I want to share with you my experience of burning out. After registering with Launch School, I am extremely excited about my programming journey. I studied for 10 to 12 hours a day, memorizing fact, trying out practice problems, understanding programming concepts. It was fun and exciting and I love seeing myself growing from nothing in programming to something more. After about 3 months, thing starts to change. I started noticing myself paying less attention to details. I find myself skimming through the course material. I skip "Further Exploration" in the practice problem. I am more interested to study just to pass the assessment rather than truly mastering the concept. It was a gradual burning out process but I continue to study for 10 to 12 hours a day through sheer grit. It felt like doing house chore or working a day job that you don't like. One particular morning I woke up, and I remember this deep feeling of dread because I can anticipate that the next 10 to 1

Sharing my Weakness

It makes sense to know about your weakness and do something about it. Here are my known weaknesses uncovered during my time in Launch School. 1. I don't like to refactor my code   - Your first draft will not be perfect. It works but it may not be efficient/readable/best practices. You final code will almost always be better than your first draft. - It is easier to separate the task between writing code that works and refactor later to make it efficient/readable/best practices. - If you refactor your code often, over time you will discover your bad habits and change it. 2. I don't like to read other people's code - There are more good programming practices in other people than in you (especially for beginners like me). - To be good , you need to know more than one pathways to solve a programming problem (and there are always more than one way). Then you can judge their merit. - Reason for dislikes    1. It is considerably harder to read code than to write one (be