github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/fish_client/src/views/signup_form.js (about)

     1  /**
     2   * Copyright 2017 Intel Corporation
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   * ----------------------------------------------------------------------------
    16   */
    17  'use strict'
    18  
    19  const m = require('mithril')
    20  const _ = require('lodash')
    21  
    22  const forms = require('../components/forms')
    23  const api = require('../services/api')
    24  const transactions = require('../services/transactions')
    25  const payloads = require('../services/payloads')
    26  
    27  const passwordCard = state => {
    28    const setter = forms.stateSetter(state)
    29    const validator = forms.validator(
    30      () => state.password === state.confirm,
    31      'Passwords do not match',
    32      'confirm'
    33    )
    34    const passwordField = (id, placeholder) => {
    35      return forms.field(
    36        // Run both state setting and validation on value changes
    37        _.flow(setter(id), validator),
    38        {
    39          id,
    40          placeholder,
    41          type: 'password',
    42          class: 'border-warning'
    43        }
    44      )
    45    }
    46  
    47    return forms.group('Password', [
    48      m('.card.text-center.border-warning',
    49        m('.card-header.text-white.bg-warning', m('em', m('strong', 'WARNING!'))),
    50        m('.card-body.text-warning.bg-light',
    51          m('p.card-text',
    52            'This password will be used as a secret key to encrypt important ',
    53            'account information. Although it can be changed later, ',
    54            m('em',
    55              'if lost or forgotten it will be ',
    56              m('strong', 'impossible'),
    57              ' to recover your account.')),
    58          m('p.card-text', 'Keep it secure.'),
    59          passwordField('password', 'Enter password...'),
    60          passwordField('confirm', 'Confirm password...')))
    61    ])
    62  }
    63  
    64  const userSubmitter = state => e => {
    65    e.preventDefault()
    66  
    67    const keys = transactions.makePrivateKey(state.password)
    68    const user = _.assign(keys, _.pick(state, 'username', 'email'))
    69    user.password = api.hashPassword(state.password)
    70    const agent = payloads.createAgent(_.pick(state, 'name'))
    71  
    72    transactions.submit(agent, true)
    73      .then(() => api.post('users', user))
    74      .then(res => api.setAuth(res.authorization))
    75      .then(() => m.route.set('/'))
    76  }
    77  
    78  /**
    79   * The Form for authorizing an existing user.
    80   */
    81  const SignupForm = {
    82    view (vnode) {
    83      const setter = forms.stateSetter(vnode.state)
    84  
    85      return m('.signup-form', [
    86        m('form', { onsubmit: userSubmitter(vnode.state) },
    87        m('legend', 'Create Agent'),
    88        forms.textInput(setter('name'), 'Name'),
    89        forms.emailInput(setter('email'), 'Email'),
    90        forms.textInput(setter('username'), 'Username'),
    91        passwordCard(vnode.state),
    92        m('.container.text-center',
    93          'Or you can ',
    94          m('a[href="/login"]',
    95            { oncreate: m.route.link },
    96            'login an existing Agent')),
    97        m('.form-group',
    98          m('.row.justify-content-end.align-items-end',
    99            m('col-2',
   100              m('button.btn.btn-primary',
   101                'Create Agent')))))
   102      ])
   103    }
   104  }
   105  
   106  module.exports = SignupForm