-
Notifications
You must be signed in to change notification settings - Fork 2
/
model.coffee
57 lines (44 loc) · 1.5 KB
/
model.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
crypto = require('crypto')
###*
* User construction
*
* @param {String} name (optional)
* @param {String} password (optional)
###
class User
constructor: (obj) ->
@username = obj.username
@email = obj.email
@nick = obj.nick or @username
@salt = obj.salt or @makeSalt()
@hashed_password = obj.hashed_password or @encryptPassword(obj.password)
@num_posts = if obj.num_posts? then obj.num_posts else 0
@num_following = if obj.num_following? then obj.num_following else 0
@num_follower = if obj.num_follower? then obj.num_follower else 0
@num_fav = if obj.num_fav? then obj.num_fav else 0
@avatar = if obj.avatar? then obj.avatar else null
@_id = obj._id if obj._id?
makeSalt: ->
Math.round(new Date().valueOf() * Math.random()) + ''
encryptPassword: (password) ->
crypto.createHmac('sha1', @salt).update(password).digest('hex')
authenticate: (plainText) ->
@encryptPassword(plainText) is @hashed_password
###*
* LoginToken construction
*
* @param {String} email
* @param {String} series
* @param {String} token
###
class LoginToken
constructor: (obj) ->
@series = obj.series or @randomToken()
@token = obj.token or @randomToken()
@_id = obj._id
randomToken: ->
Math.round((new Date().valueOf() * Math.random())) + ''
cookieValue: ->
JSON.stringify {_id: @_id, token: @token, series: @series}
exports.User = User
exports.LoginToken = LoginToken