<!DOCTYPE html>
<html>
<head>
  <title>index.js</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <link rel="stylesheet" media="all" href="../doc-style.css" />
  <script src="../doc-filelist.js"></script>
  <script>
    var relativeDir = "../", thisFile = "Users/mayukudo/Documents/workspace/CS/326/shareninja-master//sessions/index.js", defaultSidebar = true;
  </script>
  <script src="../doc-script.js"></script>
</head>
<body>
  <div id="sidebar_wrapper">
    <div id="sidebar_switch">
      <span class="tree">Files</span>
      <span class="headings">Headings</span>
    </div>
    <div id="tree"></div>
    <div id="headings">
      <div class="heading h3">
        <a href="#function%3A%20new">function: new</a>
      </div>
      <div class="heading h3">
        <a href="#function%3A%20create">function: create</a>
      </div>
      <div class="heading h3">
        <a href="#function%3A%20delete">function: delete</a>
      </div>
    </div>
  </div>
  <div id="sidebar-toggle"></div>
  <div id="container"><div class="background highlight"></div>
<table cellpadding="0" cellspacing="0">
  <tbody>
      <tr>
        <td class="docs">
          <h1>index.js</h1>
        </td>
        <td class="code highlight"></td>
      </tr>
      <tr>
        <td class="docs">
<div class="pilwrap">
  <a class="pilcrow" href="#section-1" id="section-1">&#182;</a>
</div>

        </td>
        <td class="code highlight"><div class="highlight"><pre><span class="kd">var</span> <span class="nx">models</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;../../models&#39;</span><span class="p">)</span>
  <span class="p">,</span> <span class="nx">lib</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;../../lib&#39;</span><span class="p">)</span>
  <span class="p">,</span> <span class="nx">User</span> <span class="o">=</span> <span class="nx">models</span><span class="p">.</span><span class="nx">UserModel</span>
  <span class="p">;</span>



</pre></div>
        </td>
      </tr>
      <tr>
        <td class="docs">
<div class="pilwrap" id="function%3A%20new">
  <h3>
    <a href="#function%3A%20new" name="function%3A%20new" class="pilcrow">&#182;</a>
    <em>function</em>: new
  </h3>
</div>


<div class="dox">
  <div class="summary"><p>Redirects to sessions/new for loggining-in</p>
  </div>
  <div class="body"><p>@param {object} req The HTTP request
 @param {object} res The HTTP response
 @param {object} next Pass handlers to the next </p>
  </div>
</div>
        </td>
        <td class="code highlight"><div class="highlight"><pre>
<span class="nx">exports</span><span class="p">.</span><span class="k">new</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">res</span><span class="p">,</span> <span class="nx">next</span><span class="p">)</span> <span class="p">{</span>
  <span class="nx">res</span><span class="p">.</span><span class="nx">render</span><span class="p">(</span><span class="s1">&#39;sessions/new&#39;</span><span class="p">,</span> <span class="p">{</span>
    <span class="nx">title</span><span class="o">:</span> <span class="s1">&#39;Login&#39;</span>
  <span class="p">});</span>
<span class="p">};</span>


</pre></div>
        </td>
      </tr>
      <tr>
        <td class="docs">
<div class="pilwrap" id="function%3A%20create">
  <h3>
    <a href="#function%3A%20create" name="function%3A%20create" class="pilcrow">&#182;</a>
    <em>function</em>: create
  </h3>
</div>


<div class="dox">
  <div class="summary"><p>Create a user with corresponding messages and cookies</p>
  </div>
  <div class="body"><p>@param {object} req The HTTP request
 @param {object} res The HTTP response</p>
  </div>
</div>
        </td>
        <td class="code highlight"><div class="highlight"><pre>
<span class="nx">exports</span><span class="p">.</span><span class="nx">create</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">res</span><span class="p">)</span> <span class="p">{</span>
  <span class="kd">var</span> <span class="nx">condition</span> <span class="o">=</span> <span class="p">{</span>
    <span class="nx">username</span><span class="o">:</span> <span class="nx">req</span><span class="p">.</span><span class="nx">param</span><span class="p">(</span><span class="s1">&#39;username&#39;</span><span class="p">),</span>
    <span class="nx">password</span><span class="o">:</span> <span class="nx">req</span><span class="p">.</span><span class="nx">param</span><span class="p">(</span><span class="s1">&#39;password&#39;</span><span class="p">)</span>
  <span class="p">};</span>
  <span class="kd">var</span> <span class="nx">rememberme</span> <span class="o">=</span> <span class="nx">req</span><span class="p">.</span><span class="nx">param</span><span class="p">(</span><span class="s1">&#39;rememberme&#39;</span><span class="p">);</span>
  <span class="nx">User</span><span class="p">.</span><span class="nx">findOne</span><span class="p">(</span><span class="nx">condition</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">result</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">err</span><span class="p">)</span> <span class="p">{</span>
      <span class="k">return</span> <span class="nx">next</span><span class="p">(</span><span class="nx">err</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">result</span><span class="p">)</span> <span class="p">{</span>
      <span class="nx">req</span><span class="p">.</span><span class="nx">flash</span><span class="p">(</span><span class="s1">&#39;loginErr&#39;</span><span class="p">,</span> <span class="s1">&#39;Authentication failed&#39;</span><span class="p">);</span>
      <span class="k">return</span> <span class="nx">res</span><span class="p">.</span><span class="nx">redirect</span><span class="p">(</span><span class="s1">&#39;back&#39;</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">rememberme</span><span class="p">)</span> <span class="p">{</span>

</pre></div>
        </td>
      </tr>
      <tr>
        <td class="docs">
<div class="pilwrap">
  <a class="pilcrow" href="#section-4" id="section-4">&#182;</a>
</div>
<p>Store cookie</p>
        </td>
        <td class="code highlight"><div class="highlight"><pre>
      <span class="kd">var</span> <span class="nx">newtoken</span> <span class="o">=</span> <span class="p">{</span>
        <span class="nx">username</span><span class="o">:</span> <span class="nx">result</span><span class="p">.</span><span class="nx">username</span><span class="p">,</span>
        <span class="nx">authcookie</span><span class="o">:</span> <span class="nx">result</span><span class="p">.</span><span class="nx">authcookie</span>
      <span class="p">};</span>
      <span class="nx">lib</span><span class="p">.</span><span class="nx">setCookie</span><span class="p">(</span><span class="nx">res</span><span class="p">,</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">newtoken</span><span class="p">));</span>
    <span class="p">}</span>
    <span class="nx">req</span><span class="p">.</span><span class="nx">session</span><span class="p">.</span><span class="nx">username</span> <span class="o">=</span> <span class="nx">result</span><span class="p">.</span><span class="nx">username</span><span class="p">;</span>
    <span class="nx">res</span><span class="p">.</span><span class="nx">redirect</span><span class="p">(</span><span class="s1">&#39;top&#39;</span><span class="p">);</span>
  <span class="p">});</span>
<span class="p">};</span>



</pre></div>
        </td>
      </tr>
      <tr>
        <td class="docs">
<div class="pilwrap" id="function%3A%20delete">
  <h3>
    <a href="#function%3A%20delete" name="function%3A%20delete" class="pilcrow">&#182;</a>
    <em>function</em>: delete
  </h3>
</div>


<div class="dox">
  <div class="summary"><p>Destroyes the session = logs out from the server and redirects to the sessions/new</p>
  </div>
  <div class="body"><p>@param {object} req The HTTP request
 @param {object} res The HTTP response</p>
  </div>
</div>
        </td>
        <td class="code highlight"><div class="highlight"><pre>
<span class="nx">exports</span><span class="p">.</span><span class="k">delete</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">req</span><span class="p">,</span> <span class="nx">res</span><span class="p">)</span> <span class="p">{</span>
  <span class="nx">req</span><span class="p">.</span><span class="nx">session</span><span class="p">.</span><span class="nx">destroy</span><span class="p">();</span>
  <span class="nx">res</span><span class="p">.</span><span class="nx">clearCookie</span><span class="p">(</span><span class="s1">&#39;authtoken&#39;</span><span class="p">,</span> <span class="p">{</span> <span class="nx">path</span><span class="o">:</span> <span class="s1">&#39;/&#39;</span> <span class="p">});</span>
  <span class="nx">res</span><span class="p">.</span><span class="nx">redirect</span><span class="p">(</span><span class="s1">&#39;/sessions/new&#39;</span><span class="p">);</span>
<span class="p">};</span>
</pre></div>
        </td>
      </tr>
  </tbody>
</table>
  </div>
</body>
</html>
