aboutsummaryrefslogtreecommitdiffstats
path: root/package-lock.json
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2016-02-06 23:03:34 -0800
committerAdam Soltys <[email protected]>2016-02-06 23:30:26 -0800
commit28365158a300bd1d9d7cc6882ce3b350dd1320ed (patch)
treed6338e6f5ae25903be7ea326e1e5777638d79562 /package-lock.json
parentdc762c8715cfc6f75a6deac87fef89a09b954dfa (diff)
downloadcoinos-server-28365158a300bd1d9d7cc6882ce3b350dd1320ed.tar.gz
coinos-server-28365158a300bd1d9d7cc6882ce3b350dd1320ed.zip
Adjustments
Diffstat (limited to 'package-lock.json')
0 files changed, 0 insertions, 0 deletions
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
db = require("../redis")
config = require("../config")
bcrypt = require('bcrypt')

module.exports = (sessions) ->
  exists: (req, res) ->
    db.hgetall("user:"+req.params.user, (err, obj) ->
      if obj? then res.write('true') else res.write('false')
      res.end()
    )
  
  json: (req, res) ->
    db.llen("#{req.params.user}:transactions", (err, len) ->
      db.hgetall("user:#{req.params.user}", (err, obj) ->
        delete obj['password']
        obj['index'] = len
        res.writeHead(200, {"Content-Type": "application/json"});
        res.write(JSON.stringify(obj))
        res.end()
      )
    )

  show: (req, res) ->
    db.hgetall("user:"+req.params.user, (err, obj) ->
      if obj 
        options = 
          user: req.params.user, 
          layout: 'layout',
          navigation: true,
          js: (-> global.js), 
          css: (-> global.css)

        if req.query.verified?
          options.verified = true

        res.render('users/show', options)
        delete req.session.verified
      else 
        res.render('sessions/new', 
          notice: true,
          layout: 'layout',
          js: (-> global.js), 
          css: (-> global.css)
        )
    )

  new: (req, res) ->
    res.render('users/new', 
      layout: 'layout',
      js: (-> global.js), 
      css: (-> global.css)
    )

  create: (req, res) ->
    errormsg = ""
    userkey = "user:"+req.body.username
    db.hgetall(userkey, (err, obj) ->
      if obj
        errormsg += "Username exists"

      if req.body.confirm != req.body.password
        errormsg += "Passwords must match"

      if errormsg
        return res.render('users/new',
          layout: 'layout',
          js: (-> global.js), 
          css: (-> global.css),
          error: errormsg
        )

      bcrypt.hash(req.body.password, 12, (err, hash) ->
         db.sadd("users",userkey)
         db.hmset(userkey,
           username: req.body.username,
           password: hash,
           email: req.body.email,
           pubkey: req.body.pubkey,
           privkey: req.body.privkey
          , ->
            req.session.redirect = "/#{req.body.username}/edit"
            sessions.create(req, res)
         )
      )

      require('crypto').randomBytes(48, (ex, buf) ->
        token = buf.toString('base64').replace(/\//g,'').replace(/\+/g,'')
        db.set("token:#{token}", req.body.username)
        host = req.hostname
        host += ':3000' if host is 'localhost'
        url = "#{req.protocol}://#{host}/verify/#{token}"

        res.render('users/welcome', 
          user: req.params.user, 
          layout: 'mail',
          url: url,
          privkey: req.body.privkey,
          js: (-> global.js), 
          css: (-> global.css),
          (err, html) ->
            sendgrid = require('sendgrid')(config.sendgrid_user, config.sendgrid_password)

            email = new sendgrid.Email(
              to: req.body.email
              from: '[email protected]'
              subject: 'Welcome to CoinOS'
              html: html
            )

            sendgrid.send(email)
        )
      )
    )

  edit: (req, res) ->
    res.render('users/edit', 
      user: req.params.user, 
      layout: 'layout',
      navigation: true,
      js: (-> global.js), 
      css: (-> global.css)
    )

  profile: (req, res) ->
    res.render('users/profile', 
      user: req.params.user, 
      layout: 'layout',
      navigation: true,
      js: (-> global.js), 
      css: (-> global.css)
    )

  update: (req, res) ->
    if req.body.password is ''
      delete req.body.password

    if req.body.privkey is ''
      delete req.body.privkey

    db.hmset("user:"+req.params.user, req.body, ->
      if req.body.password?
        bcrypt.hash(req.body.password, 12, (err, hash) ->
          db.hmset("user:#{req.params.user}", password: hash, ->
            res.redirect("/#{req.params.user}")
          )
        )
      else
        res.redirect("/#{req.params.user}")
    )

    if req.body.privkey? and req.body.privkey != '' and req.body.email != ''
      res.render('users/key', 
        user: req.params.user, 
        layout: 'mail',
        key: req.body.privkey,
        js: (-> global.js), 
        css: (-> global.css),
        (err, html) ->
          sendgrid = require('sendgrid')(config.sendgrid_user, config.sendgrid_password)

          email = new sendgrid.Email(
            to: req.body.email
            from: '[email protected]'
            subject: 'CoinOS Wallet Key'
            html: html
          )

          sendgrid.send(email)
      )

  verify: (req, res) ->
    db.get("token:#{req.params.token}", (err, reply) ->
      if err or !reply
        res.write("Invalid Verification Token")
        res.end()
      else
        db.hset("user:#{reply.toString()}", "verified", "true", ->
          res.redirect("/#{reply.toString()}?verified")
        )
    )