nodulator
Version:
Complete NodeJS Framework for Restfull APIs
342 lines (222 loc) • 11.1 kB
HTML
<html>
<head>
<title>AccountResource.ls</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul id="jump_to">
<li>
<a class="large" href="javascript:void(0);">Jump To …</a>
<a class="small" href="javascript:void(0);">+</a>
<div id="jump_wrapper">
<div id="jump_page_wrapper">
<div id="jump_page">
<a class="source" href="Nodulator.html">
Nodulator.ls
</a>
<a class="source" href="ClientDB.html">
ClientDB.ls
</a>
<a class="source" href="index.html">
index.ls
</a>
<a class="source" href="Resource.html">
Resource.ls
</a>
<a class="source" href="Bus.html">
Bus.ls
</a>
<a class="source" href="Cache.html">
Cache.ls
</a>
<a class="source" href="ChangeWatcher.html">
ChangeWatcher.ls
</a>
<a class="source" href="Debug.html">
Debug.ls
</a>
<a class="source" href="Wrappers.html">
Wrappers.ls
</a>
<a class="source" href="Nodulator.html">
Nodulator.ls
</a>
<a class="source" href="Resource.html">
Resource.ls
</a>
<a class="source" href="Schema.html">
Schema.ls
</a>
<a class="source" href="NModule.html">
NModule.ls
</a>
<a class="source" href="AccountResource.html">
AccountResource.ls
</a>
<a class="source" href="User.html">
User.ls
</a>
<a class="source" href="index.html">
index.ls
</a>
<a class="source" href="Nodulator-Angular.html">
Nodulator-Angular.ls
</a>
<a class="source" href="TaskDirective.html">
TaskDirective.ls
</a>
<a class="source" href="index.html">
index.ls
</a>
<a class="source" href="TaskService.html">
TaskService.ls
</a>
<a class="source" href="main.html">
main.ls
</a>
<a class="source" href="index.html">
index.ls
</a>
<a class="source" href="index.html">
index.ls
</a>
<a class="source" href="Task.html">
Task.ls
</a>
<a class="source" href="index.html">
index.ls
</a>
<a class="source" href="index.html">
index.ls
</a>
<a class="source" href="index.html">
index.ls
</a>
<a class="source" href="index.html">
index.ls
</a>
<a class="source" href="index.html">
index.ls
</a>
<a class="source" href="DOM.html">
DOM.ls
</a>
<a class="source" href="View.html">
View.ls
</a>
<a class="source" href="index.html">
index.ls
</a>
<a class="source" href="Nodulator.html">
Nodulator.ls
</a>
<a class="source" href="Mongo.html">
Mongo.ls
</a>
<a class="source" href="Mysql.html">
Mysql.ls
</a>
<a class="source" href="SqlMem.html">
SqlMem.ls
</a>
<a class="source" href="index.html">
index.ls
</a>
<a class="source" href="Resource.html">
Resource.ls
</a>
<a class="source" href="Request.html">
Request.ls
</a>
<a class="source" href="Route.html">
Route.ls
</a>
<a class="source" href="index.html">
index.ls
</a>
</div>
</div>
</li>
</ul>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>AccountResource.ls</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
</div>
<div class="content"><div class='highlight'><pre>LocalStrategy = require('passport-local').Strategy
class AccountResource extends N '_account', {abstract: true}
@userField =
usernameField: 'username'
passwordField: 'password'
@_InitPassport = ->
N.passport.serializeUser (user, done) ~>
done null, user.id
N.passport.deserializeUser (id, done) ~>
@Fetch id, (err, user) ->
return done null, false if err? and err.status is 'not_found'
return done 'Error deserialize user', null if err?
done null, user
N.passport.use new LocalStrategy @userField, (username, password, done) ~>
constraints = {}
constraints[@userField.usernameField] = username
@Fetch constraints, (err, user) ~></pre></div></div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>@[@methodName] username, (err, user) ~></p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-keyword">return</span> done err <span class="hljs-keyword">if</span> err? <span class="hljs-keyword">and</span> err.status <span class="hljs-keyword">isnt</span> <span class="hljs-string">'not_found'</span>
<span class="hljs-keyword">return</span> done <span class="hljs-literal">null</span>, <span class="hljs-literal">false</span>, {message: <span class="hljs-string">'Incorrect Username/password'</span>} <span class="hljs-keyword">if</span> err? <span class="hljs-keyword">or</span> !(user?) <span class="hljs-keyword">or</span> user[@userField.passwordField] <span class="hljs-keyword">isnt</span> password
<span class="hljs-keyword">return</span> done <span class="hljs-literal">null</span>, user
@_InitLoginRoutes = <span class="hljs-function"><span class="hljs-params">(resName)</span> -></span>
@app = N.app
@app.post <span class="hljs-string">'/api/1/'</span> + resName + <span class="hljs-string">'s'</span> + <span class="hljs-string">'/login'</span>, N.passport.authenticate(<span class="hljs-string">'local'</span>), (req, res) ~>
<span class="hljs-keyword">if</span> @config? <span class="hljs-keyword">and</span> @config.loginCallback?
@config.loginCallback req, <span class="hljs-function">-></span>
res.sendStatus(<span class="hljs-number">200</span>)
<span class="hljs-keyword">else</span>
res.sendStatus(<span class="hljs-number">200</span>)
@app.post <span class="hljs-string">'/api/1/'</span> + resName + <span class="hljs-string">'s'</span> + <span class="hljs-string">'/logout'</span>, (req, res) ~>
<span class="hljs-keyword">if</span> @config? <span class="hljs-keyword">and</span> @config.logoutCallback?
@config.logoutCallback req, <span class="hljs-function">-></span>
req.logout()
res.sendStatus(<span class="hljs-number">200</span>)
<span class="hljs-keyword">else</span>
req.logout()
res.sendStatus(<span class="hljs-number">200</span>)
@_InjectUser = <span class="hljs-function"><span class="hljs-params">(req, res)</span> -></span>
userObject = <span class="hljs-string">'<script>var __user = '</span> + (JSON.stringify(req.user?.ToJSON() || {})) + <span class="hljs-string">';</script>\n'</span></pre></div></div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p>FIXME: dirty</p>
</div>
<div class="content"><div class='highlight'><pre> userObject
ToJSON: <span class="hljs-function">-></span>
blob = <span class="hljs-keyword">super</span>()
<span class="hljs-keyword">delete</span> blob[AccountResource.userField.passwordField]
blob
<span class="hljs-built_in">module</span>.exports = AccountResource</pre></div></div>
</li>
</ul>
</div>
</body>
</html>