Code Monkey home page Code Monkey logo

Comments (2)

sankichi92 avatar sankichi92 commented on June 26, 2024

Database Action Scripts のバックアップ。

Login

function login(email, password, callback) {
  //this example uses the "pg" library
  //more info here: https://github.com/brianc/node-postgres

  const bcrypt = require('bcrypt');
  const postgres = require('pg');

  postgres.connect(configuration.DATABASE_URL, function(err, client, done) {
    if (err) return callback(err);

    const query = `
        select
            users.id
            , users.email
            , users.password_digest
            , users.subscribing
            , members.id as member_id
            , members.name
            , members.joined_year
        from
            users
            join members on users.member_id = members.id
        where
            users.email = $1
            and users.password_digest is not null
    `;
    client.query(query, [email], function(err, result) {
      // NOTE: always call `done()` here to close
      // the connection to the database
      done();

      if (err || result.rows.length === 0) return callback(err || new WrongUsernameOrPasswordError(email));

      const user = result.rows[0];

      bcrypt.compare(password, user.password_digest, function(err, isValid) {
        if (err || !isValid) return callback(err || new WrongUsernameOrPasswordError(email));

        return callback(null, {
          user_id: user.id.toString(),
          name: user.name,
          email: user.email,
          email_verified: true,
          user_metadata: {
            livelog_email_notifications: user.subscribing,
          },
          app_metadata: {
            joined_year: user.joined_year,
            livelog_member_id: user.member_id,
          },
        });
      });
    });
  });
}

Get User

function loginByEmail(email, callback) {
  //this example uses the "pg" library
  //more info here: https://github.com/brianc/node-postgres

  const postgres = require('pg');

  postgres.connect(configuration.DATABASE_URL, function(err, client, done) {
    if (err) return callback(err);

    const query = `
        select
            users.id
            , users.email
            , users.password_digest
            , users.subscribing
            , members.id as member_id
            , members.name
            , members.joined_year
        from
            users
            join members on users.member_id = members.id
        where
            users.email = $1
            and users.password_digest is not null
    `;
    client.query(query, [email], function(err, result) {
      // NOTE: always call `done()` here to close
      // the connection to the database
      done();

      if (err || result.rows.length === 0) return callback(err);

      const user = result.rows[0];

      return callback(null, {
        user_id: user.id.toString(),
        name: user.name,
        email: user.email,
        email_verified: true,
        user_metadata: {
          livelog_email_notifications: user.subscribing,
        },
        app_metadata: {
          joined_year: user.joined_year,
          livelog_member_id: user.member_id,
        },
      });
    });
  });
}

from livelog.

sankichi92 avatar sankichi92 commented on June 26, 2024

止めた。

from livelog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.