lib/user.js
/**
 * User methods. The example doesn't contain a DB, but for real applications you must use a
 * db here, such as MongoDB, Fauna, SQL, etc.
 */
User methods. The example doesn't contain a DB, but for real applications you must use a
db here, such as MongoDB, Fauna, SQL, etc.
User methods . The example doesn ' t contain a DB but for real applications you must use a db here such as MongoDB Fauna SQL etc .
async function createUser({ email, password, ...props }) {
   const salt = crypto.randomBytes(16).toString('hex')
   const hash = crypto.pbkdf2Sync(password, salt, 1000, 64, 'sha512').toString('hex')
   const user = await DB.createUser({ email, salt, hash })
  try {
    let resData = {
      email: email,
      ...props,
    };
    try {
      const adminQuery = await client.query(
        q.Get(q.Match(q.Index("admin_by_email"), email))
      );
      const isAdmin = !!adminQuery.data;
      if (isAdmin) {
        resData = {
          email: email,
          role: "admin",
          ...props,
        };
      }
    } catch (e) {}
    await client.query(
      q.Create(q.Collection("users"), {
        credentials: { password: password },
        data: resData,
      })
    );
    return resData;
  } catch (e) {
    return e;
  }
}
async function createUser ( { email , password , ... props } ) { try { let resData = { email : email , ... props , } ; try { const adminQuery = await client . query ( q . Get ( q . Match ( q . Index ( "admin_by_email" ) , email ) ) ) ; const isAdmin = ! ! adminQuery . data ; if ( isAdmin ) { resData = { email : email , role : "admin" , ... props , } ; } } catch ( e ) { } await client . query ( q . Create ( q . Collection ( "users" ) , { credentials : { password : password } , data : resData , } ) ) ; return resData ; } catch ( e ) { return e ; } }
User methods.
User methods .
// Here you should create the user and save the salt and hashed password (some dbs may have // authentication methods that will do it for you so you don't have to worry about it): // // const salt = crypto.randomBytes(16).toString('hex') // const hash = crypto.pbkdf2Sync(password, salt, 1000, 64, 'sha512').toString('hex') // const user = await DB.createUser({ email, salt, hash })